From accbc1b90bd942aa36ac1485a21056b774ce02df Mon Sep 17 00:00:00 2001 From: Martin Uecker Date: Sun, 16 Mar 2025 10:54:17 +0100 Subject: c: Fix tagname confusion for typedef redefinitions [PR118765] When we redefine a typedef for a tagged type that has just been redefined, merge_decls may produce invalid TYPE_DECLS that are not consistent with what set_underlying_type produces. This is fixed by updating DECL_ORIGINAL_TYPE. PR c/118765 gcc/c/ChangeLog: * c-decl.cc (merge_decls): For TYPE_DECLS copy DECL_ORIGINAL_TYPE from the old declaration. * c-typeck.cc (tagged_types_tu_compatible_p): Add checking assertions. gcc/testsuite/ChangeLog: * gcc.dg/pr118765-2.c: New test. * gcc.dg/pr118765-3.c: New test. * gcc.dg/typedef-redecl3.c: New test. --- gcc/c/c-decl.cc | 3 +++ gcc/c/c-typeck.cc | 4 ++++ 2 files changed, 7 insertions(+) (limited to 'gcc/c') diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 1ae5208..c778c7f 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -2788,6 +2788,9 @@ merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype) break; } } + + /* Make sure we refer to the same type as the olddecl. */ + DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl); } /* Merge the data types specified in the two decls. */ diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 71782bc..aaf8e54 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -1812,6 +1812,10 @@ tagged_types_tu_compatible_p (const_tree t1, const_tree t2, to go look at the original type. */ t1 = c_type_original (t1); t2 = c_type_original (t2); + gcc_checking_assert (!TYPE_NAME (t1) + || TREE_CODE (TYPE_NAME (t1)) == IDENTIFIER_NODE); + gcc_checking_assert (!TYPE_NAME (t2) + || TREE_CODE (TYPE_NAME (t2)) == IDENTIFIER_NODE); if (TYPE_NAME (t1) != TYPE_NAME (t2)) return false; -- cgit v1.1