From patchwork Sat Aug 26 16:26:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 1826421 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=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=wZ9eLEYg; 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 4RY2Kc0n9Kz1yg5 for ; Sun, 27 Aug 2023 02:27:32 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0B677385841D for ; Sat, 26 Aug 2023 16:27:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0B677385841D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693067250; bh=PfRmvSYT/h2mK8v700ZxAHsU85P2lilESsFUw++OHDw=; h=Subject:To:Cc:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=wZ9eLEYggsvaz0lELCRNnV2cYtciWW05PoEU8DGx1KviwMRjGfbPbbIpOIhemnzP/ qXE7aiudocFTUF/ifZAW8xyZGiqNjILM5PIwFrI0xQs5vbqdDvjEW7LlL09nUsa4AX NbnX90wsNm+5TUZ4Im8TqkN5wag//1N/n2E7m/00= 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 2818A385841F for ; Sat, 26 Aug 2023 16:27:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2818A385841F Received: from vra-173-28.tugraz.at (vra-173-28.tugraz.at [129.27.173.28]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4RY2Jv4pGVz1LM0B; Sat, 26 Aug 2023 18:26:55 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 mailrelay.tugraz.at 4RY2Jv4pGVz1LM0B Message-ID: <157a7b1266c6d3b46b2be59f878d8469abdd21bc.camel@tugraz.at> Subject: [C PATCH] c: flag for tag compatibility rules To: gcc-patches@gcc.gnu.org Cc: Joseph Myers Date: Sat, 26 Aug 2023 18:26:55 +0200 In-Reply-To: <4b3866a8cc9b48f3be97c004dedbac8e9149da63.camel@tugraz.at> References: <4b3866a8cc9b48f3be97c004dedbac8e9149da63.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: -0.4 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.116 X-Spam-Status: No, score=-11.8 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Martin Uecker via Gcc-patches From: Martin Uecker Reply-To: Martin Uecker Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Add a flag to turn tag compatibility rules on or off independent from the language version. gcc/c-family: * c.opt (flag_tag_compat): New flag. gcc/c: * c-decl.cc (diagnose_mismatched_decls, start_struct, finish_struct, start_enum, finish_enum): Support flag. * c-typeck.cc (composite_type_internal): Support flag. gcc/doc: * invoke.texi: Document flag. gcc/testsuite: * gcc.dg/asan/pr81470.c: Turn off tag compatibility. * gcc.dg/c99-tag-1.c: Turn off tag compatibility. * gcc.dg/c99-tag-2.c: Turn off tag compatibility. * gcc.dg/decl-3.c: Turn off tag compatibility. * gcc.dg/enum-redef-1.c: Turn off tag compatibility. * gcc.dg/pr17188-1.c: Turn off tag compatibility. * gcc.dg/pr18809-1.c: Turn off tag compatibility. * gcc.dg/pr39084.c: Turn off tag compatibility. * gcc.dg/pr79983.c: Turn off tag compatibility. --- gcc/c-family/c.opt | 3 +++ gcc/c/c-decl.cc | 12 ++++++------ gcc/c/c-typeck.cc | 2 +- gcc/doc/invoke.texi | 5 +++++ gcc/testsuite/gcc.dg/asan/pr81460.c | 1 + gcc/testsuite/gcc.dg/c99-tag-1.c | 2 +- gcc/testsuite/gcc.dg/c99-tag-2.c | 2 +- gcc/testsuite/gcc.dg/decl-3.c | 1 + gcc/testsuite/gcc.dg/enum-redef-1.c | 2 ++ gcc/testsuite/gcc.dg/pr17188-1.c | 2 +- gcc/testsuite/gcc.dg/pr18809-1.c | 1 + gcc/testsuite/gcc.dg/pr39084.c | 2 +- gcc/testsuite/gcc.dg/pr79983.c | 2 +- 13 files changed, 25 insertions(+), 12 deletions(-) diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 2242524cd3e..f95f12ba249 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -2214,6 +2214,9 @@ Enum(strong_eval_order) String(some) Value(1) EnumValue Enum(strong_eval_order) String(all) Value(2) +ftag-compat +C Var(flag_tag_compat) Init(1) + ftemplate-backtrace-limit= C++ ObjC++ Joined RejectNegative UInteger Var(template_backtrace_limit) Init(10) Set the maximum number of template instantiation notes for a single warning or error. diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 2137ba8b845..6d1e0d5c382 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -2094,7 +2094,7 @@ diagnose_mismatched_decls (tree newdecl, tree olddecl, given scope. */ if (TREE_CODE (olddecl) == CONST_DECL) { - if (flag_isoc2x + if ((flag_isoc2x || flag_tag_compat) && TYPE_NAME (DECL_CONTEXT (newdecl)) && DECL_CONTEXT (newdecl) != DECL_CONTEXT (olddecl) && TYPE_NAME (DECL_CONTEXT (newdecl)) == TYPE_NAME (DECL_CONTEXT (olddecl))) @@ -8723,7 +8723,7 @@ start_struct (location_t loc, enum tree_code code, tree name, /* For C2X, even if we already have a completed definition, we do not use it. We will check for consistency later. */ - if (flag_isoc2x && ref && TYPE_SIZE (ref)) + if ((flag_isoc2x || flag_tag_compat) && ref && TYPE_SIZE (ref)) ref = NULL_TREE; if (ref && TREE_CODE (ref) == code) @@ -9515,7 +9515,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, } /* Check for consistency with previous definition */ - if (flag_isoc2x) + if (flag_isoc2x || flag_tag_compat) { tree vistype = previous_tag (t); if (vistype @@ -9534,7 +9534,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes, C_TYPE_BEING_DEFINED (t) = 0; /* Set type canonical based on equivalence class. */ - if (flag_isoc2x) + if (flag_isoc2x || flag_tag_compat) { if (NULL == c_struct_htab) c_struct_htab = hash_table::create_ggc (61); @@ -9672,7 +9672,7 @@ start_enum (location_t loc, struct c_enum_contents *the_enum, tree name, if (name != NULL_TREE) enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc); - if (flag_isoc2x && enumtype != NULL_TREE + if ((flag_isoc2x || flag_tag_compat) && enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE && TYPE_VALUES (enumtype) != NULL_TREE) enumtype = NULL_TREE; @@ -9941,7 +9941,7 @@ finish_enum (tree enumtype, tree values, tree attributes) struct_parse_info->struct_types.safe_push (enumtype); /* Check for consistency with previous definition */ - if (flag_isoc2x) + if (flag_isoc2x || flag_tag_compat) { tree vistype = previous_tag (enumtype); if (vistype diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 357367eab09..b99f0c3e2fd 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -512,7 +512,7 @@ composite_type_internal (tree t1, tree t2, struct composite_cache* cache) case RECORD_TYPE: case UNION_TYPE: - if (flag_isoc2x && !comptypes_same_p (t1, t2)) + if ((flag_isoc2x || flag_tag_compat) && !comptypes_same_p (t1, t2)) { gcc_checking_assert (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)); gcc_checking_assert (comptypes (t1, t2)); diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index a32dabf0405..d7a21ca8678 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -2911,6 +2911,11 @@ the target (the default). This option is not supported for C++. @strong{Warning:} the @option{-fsso-struct} switch causes GCC to generate code that is not binary compatible with code generated without it if the specified endianness is not the native endianness of the target. + +@item -ftag-compat +@opindex ftag-compat +This option makes tagged types that are structurally equivalent compatible +and allows identical redeclarations of tagged types in the same scope. @end table @node C++ Dialect Options diff --git a/gcc/testsuite/gcc.dg/asan/pr81460.c b/gcc/testsuite/gcc.dg/asan/pr81460.c index 00c1bb7c9f2..98ea40edb56 100644 --- a/gcc/testsuite/gcc.dg/asan/pr81460.c +++ b/gcc/testsuite/gcc.dg/asan/pr81460.c @@ -1,5 +1,6 @@ /* PR sanitizer/80460 */ /* { dg-do compile } */ +/* { dg-options "-fno-tag-compat" } */ int f (int a, struct { int b[a]; } c) /* { dg-warning "anonymous struct declared inside parameter list will not be visible outside of this definition or declaration" } */ diff --git a/gcc/testsuite/gcc.dg/c99-tag-1.c b/gcc/testsuite/gcc.dg/c99-tag-1.c index d7011d2cbec..1b52234ee3f 100644 --- a/gcc/testsuite/gcc.dg/c99-tag-1.c +++ b/gcc/testsuite/gcc.dg/c99-tag-1.c @@ -1,7 +1,7 @@ /* Test for handling of tags (6.7.2.3). */ /* Origin: Joseph Myers */ /* { dg-do compile } */ -/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */ +/* { dg-options "-std=iso9899:1999 -pedantic-errors -fno-tag-compat" } */ void foo (void) diff --git a/gcc/testsuite/gcc.dg/c99-tag-2.c b/gcc/testsuite/gcc.dg/c99-tag-2.c index 22cf90e27d3..b12c7bcd964 100644 --- a/gcc/testsuite/gcc.dg/c99-tag-2.c +++ b/gcc/testsuite/gcc.dg/c99-tag-2.c @@ -2,7 +2,7 @@ not match one declared in an outer scope. */ /* Origin: Joseph Myers */ /* { dg-do compile } */ -/* { dg-options "-std=iso9899:1999 -pedantic-errors" } */ +/* { dg-options "-std=iso9899:1999 -pedantic-errors -fno-tag-compat" } */ struct s; struct t { struct s *p; } x; diff --git a/gcc/testsuite/gcc.dg/decl-3.c b/gcc/testsuite/gcc.dg/decl-3.c index cba0b906db3..66204270337 100644 --- a/gcc/testsuite/gcc.dg/decl-3.c +++ b/gcc/testsuite/gcc.dg/decl-3.c @@ -1,5 +1,6 @@ /* PR c/9928 */ /* { dg-do compile } */ +/* { dg-options "-fno-tag-compat" } */ enum { CODES }; /* { dg-message "note: previous definition" } */ enum { CODES }; /* { dg-error "conflicting types|redeclaration of enumerator" } */ diff --git a/gcc/testsuite/gcc.dg/enum-redef-1.c b/gcc/testsuite/gcc.dg/enum-redef-1.c index b3fa6cbf8f1..837992f7441 100644 --- a/gcc/testsuite/gcc.dg/enum-redef-1.c +++ b/gcc/testsuite/gcc.dg/enum-redef-1.c @@ -1,3 +1,5 @@ +/* { dg-options "-fno-tag-compat" } */ + enum a { A }; enum a { B }; /* { dg-bogus "nested redefinition" } */ /* { dg-error "redeclaration of 'enum a'" "" { target *-*-* } .-1 } */ diff --git a/gcc/testsuite/gcc.dg/pr17188-1.c b/gcc/testsuite/gcc.dg/pr17188-1.c index 522a14f7d75..bb31ba30b5d 100644 --- a/gcc/testsuite/gcc.dg/pr17188-1.c +++ b/gcc/testsuite/gcc.dg/pr17188-1.c @@ -3,7 +3,7 @@ diagnosed. Bug 17188. */ /* Origin: Joseph Myers */ /* { dg-do compile } */ -/* { dg-options "" } */ +/* { dg-options "-fno-tag-compat" } */ struct s0 { }; /* { dg-message "note: originally defined here" } */ struct s0; diff --git a/gcc/testsuite/gcc.dg/pr18809-1.c b/gcc/testsuite/gcc.dg/pr18809-1.c index 5be41809da6..d7f55feae61 100644 --- a/gcc/testsuite/gcc.dg/pr18809-1.c +++ b/gcc/testsuite/gcc.dg/pr18809-1.c @@ -1,6 +1,7 @@ /* PR c/18809 */ /* Origin: Andrew Pinski */ +/* { dg-options "-pedantic-errors -fno-tag-compat" } */ /* { dg-do compile } */ void foo(enum E e) {} /* { dg-error "forward ref" "forward" } */ diff --git a/gcc/testsuite/gcc.dg/pr39084.c b/gcc/testsuite/gcc.dg/pr39084.c index ff731492154..776fbea6750 100644 --- a/gcc/testsuite/gcc.dg/pr39084.c +++ b/gcc/testsuite/gcc.dg/pr39084.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-O2" } */ +/* { dg-options "-O2 -fno-tag-compat" } */ struct color { int i; }; /* { dg-message "note: originally defined here" } */ static const struct color col; diff --git a/gcc/testsuite/gcc.dg/pr79983.c b/gcc/testsuite/gcc.dg/pr79983.c index 1e292d42108..c9fe72f9169 100644 --- a/gcc/testsuite/gcc.dg/pr79983.c +++ b/gcc/testsuite/gcc.dg/pr79983.c @@ -1,6 +1,6 @@ /* PR c/79983 */ /* { dg-do compile } */ -/* { dg-options "" } */ +/* { dg-options "-fno-tag-compat" } */ struct S; struct S { int i; }; /* { dg-message "originally defined here" } */