aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMartin Uecker <uecker@tugraz.at>2024-11-08 18:46:10 +0100
committerMartin Uecker <uecker@gcc.gnu.org>2024-11-19 19:34:31 +0100
commit8b02cc9a4f2b8db48da2a3460655b062eaa147ba (patch)
treeb7bd5954c57ff5cbda28a2e580b5212ed81b480d /gcc
parent51d12cc4b608960469f6d36b03c45f9a39f7bdaa (diff)
downloadgcc-8b02cc9a4f2b8db48da2a3460655b062eaa147ba.zip
gcc-8b02cc9a4f2b8db48da2a3460655b062eaa147ba.tar.gz
gcc-8b02cc9a4f2b8db48da2a3460655b062eaa147ba.tar.bz2
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.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/c/c-typeck.cc4
-rw-r--r--gcc/testsuite/gcc.dg/gnu23-tag-alias-4.c8
-rw-r--r--gcc/testsuite/gcc.dg/gnu23-tag-alias-7.c6
-rw-r--r--gcc/testsuite/gcc.dg/guality/zero-length-array.c4
-rw-r--r--gcc/testsuite/gcc.dg/pr117490.c25
5 files changed, 39 insertions, 8 deletions
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index a701dd0..61145d7 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -1812,7 +1812,9 @@ tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
if (data->equiv && data->pointedto)
return true;
- if (!data->anon_field && NULL_TREE == TYPE_NAME (t1))
+ /* Different types without tag are incompatible except as an anonymous
+ field or when forming equivalence classes for TYPE_CANONICAL. */
+ if (!data->anon_field && !data->equiv && NULL_TREE == TYPE_NAME (t1))
return false;
if (!data->anon_field && TYPE_STUB_DECL (t1) != TYPE_STUB_DECL (t2))
diff --git a/gcc/testsuite/gcc.dg/gnu23-tag-alias-4.c b/gcc/testsuite/gcc.dg/gnu23-tag-alias-4.c
index 1ea3a88..b1f4e7c 100644
--- a/gcc/testsuite/gcc.dg/gnu23-tag-alias-4.c
+++ b/gcc/testsuite/gcc.dg/gnu23-tag-alias-4.c
@@ -2,12 +2,12 @@
* { dg-options "-std=gnu23 -O2" }
*/
-/* This test checks that an incompatible definition of
+/* This used to check that an incompatible definition of
* a tagged type without tag can be assumed not to alias.
- * and that this is exploited during optimization. */
+ * and that this is exploited during optimization.
+ * Because PR117490 we now check the opposite. */
-// not sure this is wise, but this was already like this before
typedef struct { int x; } foo_t;
@@ -27,7 +27,7 @@ int main()
{
foo_t y;
- if (1 != test_foo(&y, &y))
+ if (2 != test_foo(&y, &y))
__builtin_abort();
return 0;
diff --git a/gcc/testsuite/gcc.dg/gnu23-tag-alias-7.c b/gcc/testsuite/gcc.dg/gnu23-tag-alias-7.c
index d3fc4bd..d35514a 100644
--- a/gcc/testsuite/gcc.dg/gnu23-tag-alias-7.c
+++ b/gcc/testsuite/gcc.dg/gnu23-tag-alias-7.c
@@ -83,10 +83,12 @@ int main()
__builtin_abort();
struct bar4 z4;
-
+#if 0
+ // we used to test this, but this would be incorrect
+ // if there is a declaration in another TU cf. PR117490
if (1 != test_bar4(&z4, &z4))
__builtin_abort();
-
+#endif
return 0;
}
diff --git a/gcc/testsuite/gcc.dg/guality/zero-length-array.c b/gcc/testsuite/gcc.dg/guality/zero-length-array.c
index 33f34d9..83d5a76 100644
--- a/gcc/testsuite/gcc.dg/guality/zero-length-array.c
+++ b/gcc/testsuite/gcc.dg/guality/zero-length-array.c
@@ -1,6 +1,8 @@
/* PR debug/86985 */
/* { dg-do run } */
-/* { dg-options "-g" } */
+/* { dg-options "-std=c17 -g" } */
+
+/* FIXME: Use -std=c17 until PR113688 if fixed. */
struct {
int foo;
diff --git a/gcc/testsuite/gcc.dg/pr117490.c b/gcc/testsuite/gcc.dg/pr117490.c
new file mode 100644
index 0000000..80a0cd6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr117490.c
@@ -0,0 +1,25 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -std=c23" } */
+
+typedef struct {
+ int i1;
+} s1;
+
+typedef struct {
+ int i1;
+} s2_alt;
+
+[[gnu::noinline,gnu::noipa]]
+int f2(s1 *s1p, s2_alt *s2p) {
+ s1p->i1 = 2;
+ s2p->i1 = 3;
+ return s1p->i1 * 3;
+}
+
+int main()
+{
+ s1 a;
+ if (9 != f2(&a, (void*)&a))
+ __builtin_abort();
+}
+