public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Uecker <uecker@tugraz.at>
To: gcc-patches@gcc.gnu.org
Cc: Joseph Myers <joseph@codesourcery.com>
Subject: [C PATCH] c: flag for tag compatibility rules
Date: Sat, 26 Aug 2023 18:26:55 +0200	[thread overview]
Message-ID: <157a7b1266c6d3b46b2be59f878d8469abdd21bc.camel@tugraz.at> (raw)
In-Reply-To: <4b3866a8cc9b48f3be97c004dedbac8e9149da63.camel@tugraz.at>


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<c_struct_hasher>::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 <jsm28@cam.ac.uk> */
 /* { 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 <jsm@polyomino.org.uk> */
 /* { 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 <jsm@polyomino.org.uk> */
 /* { 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 <pinskia@gcc.gnu.org> */
 
+/* { 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" } */
-- 
2.30.2



      parent reply	other threads:[~2023-08-26 16:27 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-26 16:19 c23 type compatibility rules, v2 Martin Uecker
2023-08-26 16:20 ` [C PATCH 1/6] c: reorganize recursive type checking Martin Uecker
2023-09-06 20:59   ` Joseph Myers
2023-09-10  8:17     ` [C PATCH 1/6 v2] " Martin Uecker
2023-09-11 20:28       ` Joseph Myers
2023-08-26 16:22 ` [C PATCH 2/6] c23: recursive type checking of tagged type Martin Uecker
2023-11-07 23:06   ` Joseph Myers
2023-08-26 16:23 ` [C PATCH 3/6] c23: tag compatibility rules for struct and unions Martin Uecker
2023-11-07 23:18   ` Joseph Myers
2023-08-26 16:24 ` [C PATCH 4/6] c23: tag compatibility rules for enums Martin Uecker
2023-11-07 23:20   ` Joseph Myers
2023-08-26 16:25 ` [C PATCH 5/6] c23: aliasing of compatible tagged types Martin Uecker
2023-08-26 16:26 ` [C PATCH 6/6] c23: construct composite type for " Martin Uecker
2023-11-07 23:45   ` Joseph Myers
2023-08-26 16:26 ` Martin Uecker [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=157a7b1266c6d3b46b2be59f878d8469abdd21bc.camel@tugraz.at \
    --to=uecker@tugraz.at \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).