From patchwork Sat May 18 20:18:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 1936743 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=sCowCJGI; 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 4VhZt73TgFz20d9 for ; Sun, 19 May 2024 06:19:10 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 583123858CDB for ; Sat, 18 May 2024 20:19:08 +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 EC9163858C53 for ; Sat, 18 May 2024 20:18:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EC9163858C53 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 EC9163858C53 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=1716063530; cv=none; b=XNV+xGGx7RAMS2p6C3JaDjAam5tqN6WR9afoAB20WZlj9gyDE4ApmgK2LjtyUBo2ufJnp3e3fi9DH/IcakbBzN9nVt8oDJ7BJv6Q/6PT8ZQ6M5AH7Fu7of3VqagWaPbZKA2Q3bfz2V7IzxYpDYZUcupRd6I37AZlJid5nRbwUx0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716063530; c=relaxed/simple; bh=O51m0lEwwQF6Wk37bUIPM0D9xVlkLgxqwImzWLERFS8=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=ISNLGJfuoBbvi5L4HDkTIpUcgQfKxmampln6sBkFMXEwxIqvcwpf9JyM8p/KshkkkwyDnohoHC9x4s61q5i/7WTXMB+2RV4mte4LR1Ho8Ygn45KqV7+5ntaxdezfa1mk4pghrWanyDewyoJvK1RlXXvcC2PyLqYiXNPzmrm1qO0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from vra-169-100.tugraz.at (vra-169-100.tugraz.at [129.27.169.100]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4VhZsT2tt2z1LLyr; Sat, 18 May 2024 22:18:37 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 mailrelay.tugraz.at 4VhZsT2tt2z1LLyr DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1716063517; bh=hB/gk+eqZQNX1PEcp7s27cK5EthvEZfstZuNTQaibco=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=sCowCJGIZYWuFtyhHAptTSJW27WiEvwL//jkxbDwOXLctmFD3BtQAln6PCxs0xAUj J4WXKL5YC9Hx9pnQjRsM2pyPXw+OkISrsTCz3AVvcK4Kt/H8tSQ9l3EH1y0jrzp26J z4dhYeXvXSY1RJFw8aMUIUSwKWLZaPmhNoxIAQbA= Message-ID: <45067a3258e0ec1677ed95f62ac21c1353d66355.camel@tugraz.at> Subject: [C PATCH] Fix for redeclared enumerator initialized with different type [PR115109] From: Martin Uecker To: gcc-patches@gcc.gnu.org Cc: Joseph Myers Date: Sat, 18 May 2024 22:18:36 +0200 In-Reply-To: <02a9b94e4d653b6f1b9f89a1b62187f46e871738.camel@tugraz.at> References: <02a9b94e4d653b6f1b9f89a1b62187f46e871738.camel@tugraz.at> 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 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 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 add -Woverflow warning. 2024-05-18 Martin Uecker PR c/115109 gcc/c/ * c-decl.cc (build_enumerator): When redeclaring an enumerator convert value to previous type. gcc/testsuite/ * gcc.dg/pr115109.c: New test. * gcc.dg/c23-tag-enum-6.c: New test. diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index b691b91b3db..08a51c7ad50 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -10209,6 +10209,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. */ @@ -10268,6 +10269,25 @@ 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) + { + tree previous_type = TREE_TYPE (TREE_TYPE (old_decl)); + + if (!int_fits_type_p (value, previous_type)) + { + warning_at (loc, OPT_Woverflow, + "value of redeclared enumerator outside the range of " + "the previous type %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 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..ff9ec89775e --- /dev/null +++ b/gcc/testsuite/gcc.dg/c23-tag-enum-6.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c23" } */ + +#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-warning "outside the range" } */ + +enum K : int { z = 1 }; +enum K : int { z = 2UL + UINT_MAX }; /* { dg-error "outside the range" } */ + diff --git a/gcc/testsuite/gcc.dg/pr115109.c b/gcc/testsuite/gcc.dg/pr115109.c new file mode 100644 index 00000000000..0c327ce1697 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr115109.c @@ -0,0 +1,6 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c23" } */ + +enum E { a = 1L, b = 2 }; +enum E { a = 1L, b = _Generic(a, enum E: 2) }; +