]> git.ipfire.org Git - thirdparty/gcc.git/commit
c: fix incorrect TBAA for tagged types across translation units [PR117490]
authorMartin Uecker <uecker@tugraz.at>
Fri, 8 Nov 2024 17:46:10 +0000 (18:46 +0100)
committerMartin Uecker <uecker@gcc.gnu.org>
Tue, 19 Nov 2024 18:34:31 +0000 (19:34 +0100)
commit8b02cc9a4f2b8db48da2a3460655b062eaa147ba
treeb7bd5954c57ff5cbda28a2e580b5212ed81b480d
parent51d12cc4b608960469f6d36b03c45f9a39f7bdaa
c: fix incorrect TBAA for tagged types across translation units [PR117490]

Two different declarations of tagged types in the same translation unit
are incompatible in C before C23 and without tag also in C23.  Still,
two such types can be compatible to the same tagged type in a different
translation unit, but this means that pointers can alias.

typedef struct { int i; } s1;
typedef struct { int i; } s2;

int f(s1 *p1, s2 *p2)
{
  p1->i = 2;
  p2->i = 3; // p2->i can alias p1->i
  return p1->i;
}

We need to assign the same TYPE_CANONICAL to both types.  This patch fixes
this for C23 and types without tag by also forming equivalence classes for
such types based on their structure as already done for types with tag.
Because this change exposes checking errors related to flexible array
members (cf. PR113688), one test is restricted to C17 for now.

PR c/117490

gcc/c/ChangeLog:
* c-typeck.cc (tagged_types_tu_compatible): Form equivalence
classed for tagless types in C23.

gcc/testsuite/ChangeLog:
* gcc.dg/gnu23-tag-alias-4.c: Adapt test.
* gcc.dg/gnu23-tag-alias-7.c: Adapt test.
* gcc.dg/guality/zero-length-array.c: Restrict to c17.
* gcc.dg/pr117490.c: New test.
gcc/c/c-typeck.cc
gcc/testsuite/gcc.dg/gnu23-tag-alias-4.c
gcc/testsuite/gcc.dg/gnu23-tag-alias-7.c
gcc/testsuite/gcc.dg/guality/zero-length-array.c
gcc/testsuite/gcc.dg/pr117490.c [new file with mode: 0644]