From patchwork Mon Jun 17 21:52:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 1948870 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=tugraz.at header.i=@tugraz.at header.a=rsa-sha256 header.s=mailrelay header.b=eMzn8i9l; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4W33Xf0yn1z20KL for ; Tue, 18 Jun 2024 07:53:05 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DCFD73882072 for ; Mon, 17 Jun 2024 21:53:03 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id 923CB3858C50 for ; Mon, 17 Jun 2024 21:52:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 923CB3858C50 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=tugraz.at Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tugraz.at ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 923CB3858C50 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=129.27.2.202 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718661164; cv=none; b=o5IIrZIFOBF7zlc3qxlxqKqzX1DaD5UWC0ki8FV7kJeJnVnJ2twg54fWkAfJddIHxkhYalj0Cf1zUre9AWM3IQ4dV5DaHVBZfA4X1mQi1s8rWvuNbPT7rtd1/GHM0ub27eZ3duMYtEEG9Vk7hH8tK//CLJhMoWCSiLrWY5UVoj4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718661164; c=relaxed/simple; bh=1lIZqgC6DFiXAw2W11En1SdoG/3PDwbLRC+kKuS4bx8=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=Rt96H725hKN/Pdm+n0qPz+xD3Mj0z6/x4uveBivS9BL1slt55UvGDwKrcQ/hUEPpYp6ADuelTvRUhjgJDV+XuJi+Wjx5gsEc3b7laWZ67HplDNNsEqPHyS/801hXUUrBPZHKrtE+MUxfWGHVIbG18yHMTzGPtfw1surA6SD1aV8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from vra-174-63.tugraz.at (vra-174-63.tugraz.at [129.27.174.63]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4W33Wy481Cz3wCX; Mon, 17 Jun 2024 23:52:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1718661150; bh=WmSKnnl+m+EVgtyHaRa+n1Tp1qbvdJd5YOPM2m42/cY=; h=Subject:From:To:Cc:Date; b=eMzn8i9lzYar9Q6nQKHvpbmdss56iAf0gSRaKEmE5VNDem5HHWrv9gEZnjXwZmprr AtWlUAxzmqDKPbAfhBQVXQAXvWzjMyyg1abPC5gYji5I9a13F/H10d9TUanEVuaN8+ ByvJAlrA8Ceg7AZwBsJOrKn7IEwel9q7VEgJyHIQ= Message-ID: Subject: [C PATCH, v4] Fix for redeclared enumerator initialized with different type [PR115109] From: Martin Uecker To: gcc-patches@gcc.gnu.org Cc: Joseph Myers Date: Mon, 17 Jun 2024 23:52:29 +0200 User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.116 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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 is a new version of the patch. This adds the -fno-short-enums flagĀ  to the tests. I will commit it if the CI for am does not claim this time. Bootstrapped and regression tested on x86_64. c23: Fix for redeclared enumerator initialized with different type [PR115109] c23 specifies that the type of a redeclared enumerator is the one of the previous declaration. Convert initializers with different type accordingly and emit an error when the value does not fit. 2024-06-01 Martin Uecker PR c/115109 gcc/c/ * c-decl.cc (build_enumerator): When redeclaring an enumerator convert value to previous type. For redeclared enumerators use underlying type for computing the next value. gcc/testsuite/ * gcc.dg/pr115109.c: New test. * gcc.dg/c23-tag-enum-6.c: New test. * gcc.dg/c23-tag-enum-7.c: New test. commit c8a0ec5150299689e6e36b0044ea811b82d90b2f Author: Martin Uecker Date: Sat May 18 22:00:04 2024 +0200 c23: Fix for redeclared enumerator initialized with different type [PR115109] c23 specifies that the type of a redeclared enumerator is the one of the previous declaration. Convert initializers with different type accordingly and emit an error when the value does not fit. 2024-06-01 Martin Uecker PR c/115109 gcc/c/ * c-decl.cc (build_enumerator): When redeclaring an enumerator convert value to previous type. For redeclared enumerators use underlying type for computing the next value. gcc/testsuite/ * gcc.dg/pr115109.c: New test. * gcc.dg/c23-tag-enum-6.c: New test. * gcc.dg/c23-tag-enum-7.c: New test. diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 6c09eb73128..01326570e2b 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -10277,6 +10277,7 @@ build_enumerator (location_t decl_loc, location_t loc, struct c_enum_contents *the_enum, tree name, tree value) { tree decl; + tree old_decl; /* Validate and default VALUE. */ @@ -10336,6 +10337,23 @@ build_enumerator (location_t decl_loc, location_t loc, definition. */ value = convert (the_enum->enum_type, value); } + else if (flag_isoc23 + && (old_decl = lookup_name_in_scope (name, current_scope)) + && old_decl != error_mark_node + && TREE_TYPE (old_decl) + && TREE_TYPE (TREE_TYPE (old_decl)) + && TREE_CODE (old_decl) == CONST_DECL) + { + /* Enumeration constants in a redeclaration have the previous type. */ + tree previous_type = TREE_TYPE (DECL_INITIAL (old_decl)); + if (!int_fits_type_p (value, previous_type)) + { + error_at (loc, "value of redeclared enumerator outside the range " + "of %qT", previous_type); + locate_old_decl (old_decl); + } + value = convert (previous_type, value); + } else { /* Even though the underlying type of an enum is unspecified, the @@ -10402,9 +10420,14 @@ build_enumerator (location_t decl_loc, location_t loc, false); } else - the_enum->enum_next_value - = build_binary_op (EXPR_LOC_OR_LOC (value, input_location), - PLUS_EXPR, value, integer_one_node, false); + { + /* In a redeclaration the type can already be the enumeral type. */ + if (TREE_CODE (TREE_TYPE (value)) == ENUMERAL_TYPE) + value = convert (ENUM_UNDERLYING_TYPE (TREE_TYPE (value)), value); + the_enum->enum_next_value + = build_binary_op (EXPR_LOC_OR_LOC (value, input_location), + PLUS_EXPR, value, integer_one_node, false); + } the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value); if (the_enum->enum_overflow && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type)) diff --git a/gcc/testsuite/gcc.dg/c23-tag-enum-6.c b/gcc/testsuite/gcc.dg/c23-tag-enum-6.c new file mode 100644 index 00000000000..29aef7ee3fd --- /dev/null +++ b/gcc/testsuite/gcc.dg/c23-tag-enum-6.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c23 -fno-short-enums" } */ + +#include + +enum E : int { a = 1, b = 2 }; +enum E : int { b = _Generic(a, enum E: 2), a = 1 }; + +enum H { x = 1 }; +enum H { x = 2UL + UINT_MAX }; /* { dg-error "outside the range" } */ + +enum K : int { z = 1 }; +enum K : int { z = 2UL + UINT_MAX }; /* { dg-error "outside the range" } */ + +enum F { A = 0, B = UINT_MAX }; +enum F { B = UINT_MAX, A }; /* { dg-error "outside the range" } */ + +enum G : unsigned int { C = 0, D = UINT_MAX }; +enum G : unsigned int { D = UINT_MAX, C }; /* { dg-error "overflow" } */ + diff --git a/gcc/testsuite/gcc.dg/c23-tag-enum-7.c b/gcc/testsuite/gcc.dg/c23-tag-enum-7.c new file mode 100644 index 00000000000..d4c787c8f71 --- /dev/null +++ b/gcc/testsuite/gcc.dg/c23-tag-enum-7.c @@ -0,0 +1,41 @@ +/* { dg-do compile } + * { dg-options "-std=c23 -fno-short-enums" } */ + +#include + +// enumerators are all representable in int +enum E { a = 1UL, b = _Generic(a, int: 2) }; +static_assert(_Generic(a, int: 1)); +static_assert(_Generic(b, int: 1)); +enum E { a = 1UL, b = _Generic(a, int: 2) }; +static_assert(_Generic(a, int: 1)); +static_assert(_Generic(b, int: 1)); + +// enumerators are not representable in int +enum H { c = 1UL << (UINT_WIDTH + 1), d = 2 }; +static_assert(_Generic(c, enum H: 1)); +static_assert(_Generic(d, enum H: 1)); +enum H { c = 1UL << (UINT_WIDTH + 1), d = _Generic(c, enum H: 2) }; +static_assert(_Generic(c, enum H: 1)); +static_assert(_Generic(d, enum H: 1)); + +// there is an overflow in the first declaration +enum K { e = UINT_MAX, f, g = _Generic(e, unsigned int: 0) + _Generic(f, unsigned long: 1) }; +static_assert(_Generic(e, enum K: 1)); +static_assert(_Generic(f, enum K: 1)); +static_assert(_Generic(g, enum K: 1)); +enum K { e = UINT_MAX, f, g = _Generic(e, enum K: 0) + _Generic(f, enum K: 1) }; +static_assert(_Generic(e, enum K: 1)); +static_assert(_Generic(f, enum K: 1)); +static_assert(_Generic(g, enum K: 1)); + +// there is an overflow in the first declaration +enum U { k = INT_MAX, l, m = _Generic(k, int: 0) + _Generic(l, long: 1) }; +static_assert(_Generic(k, enum U: 1)); +static_assert(_Generic(l, enum U: 1)); +static_assert(_Generic(m, enum U: 1)); +enum U { k = INT_MAX, l, m = _Generic(k, enum U: 0) + _Generic(l, enum U: 1) }; +static_assert(_Generic(k, enum U: 1)); +static_assert(_Generic(l, enum U: 1)); +static_assert(_Generic(m, enum U: 1)); + diff --git a/gcc/testsuite/gcc.dg/pr115109.c b/gcc/testsuite/gcc.dg/pr115109.c new file mode 100644 index 00000000000..4baee0f3445 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr115109.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c23 -fno-short-enums" } */ + +#include + +enum E { a = 1UL << (ULONG_WIDTH - 5), b = 2 }; +enum E { a = 1ULL << (ULONG_WIDTH - 5), b = _Generic(a, enum E: 2) }; +