While fixing the other issue, I realized that the way the
equivalence classes are computed for TYPE_CANONICAL did
not take into account that completion of struct types
also affectes compatibility of types that contain pointers
to them. So the algorithm must be more conservative
creating bigger equivalence classes.
Bootstrapped and regession tested on x86_64
[C23]fix aliasing for structures/unions with incomplete types
When incomplete structure/union types are completed later, compatibility
of struct types that contain pointers to such types changes. When forming
equivalence classes for TYPE_CANONICAL, we therefor need to be conservative
and treat all structs with the same tag which are pointer targets as
equivalent.
gcc/c/
* c-typeck.cc (comptypes_internal): Add flag to track
whether a struct is the target of a pointer.
(tagged_types_tu_compatible): When forming equivalence
classes, treat pointed-to structs as equivalent.
gcc/testsuite/
* gcc.dg/c23-tag-incomplate-alias-1.c: New test.
---
gcc/c/c-typeck.cc | 11 ++++++
.../gcc.dg/c23-tag-incomplete-alias-1.c | 34 +++++++++++++++++++
2 files changed, 45 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/c23-tag-incomplete-alias-1.c
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index ddeab1e2a8a..b86450580ad 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -1170,6 +1170,7 @@ struct comptypes_data {
bool different_types_p;
bool warning_needed;
bool anon_field;
+ bool pointedto;
bool equiv;
const struct tagged_tu_seen_cache* cache;
@@ -1355,6 +1356,7 @@ comptypes_internal (const_tree type1, const_tree type2,
/* Do not remove mode information. */
if (TYPE_MODE (t1) != TYPE_MODE (t2))
return false;
+ data->pointedto = true;
return comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2), data);
case FUNCTION_TYPE:
@@ -1513,6 +1515,14 @@ tagged_types_tu_compatible_p (const_tree t1, const_tree
t2,
if (TYPE_NAME (t1) != TYPE_NAME (t2))
return false;
+ /* When forming equivalence classes for TYPE_CANONICAL in C23, we
+ have to treat structs with the same tag as equivalent, when they
+ are targets of pointers inside other structs. This is necessary
+ so that the relationship of types does not change when incomplete
+ types are completed. */
+ if (data->equiv && data->pointedto)
+ return true;
+
if (!data->anon_field && NULL_TREE == TYPE_NAME (t1))
return false;
@@ -1608,6 +1618,7 @@ tagged_types_tu_compatible_p (const_tree t1, const_tree
t2,
return false;
data->anon_field = !DECL_NAME (s1);
+ data->pointedto = false;
data->cache = &entry;
if (!comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2), data))
diff --git a/gcc/testsuite/gcc.dg/c23-tag-incomplete-alias-1.c
b/gcc/testsuite/gcc.dg/c23-tag-incomplete-alias-1.c
new file mode 100644
index 00000000000..7fb6a8513b2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/c23-tag-incomplete-alias-1.c
@@ -0,0 +1,34 @@
+/* { dg-do run }
+ * { dg-options "-std=c23 -O2" } */
+
+[[gnu::noinline]]
+void *alias(void *ap, void *b, void *x, void *y)
+{
+ struct foo { struct bar *f; } *a = ap;
+ struct bar { long x; };
+
+ a->f = x;
+
+ {
+ struct bar;
+ struct foo { struct bar *f; };
+ struct bar { long x; };
+
+ ((struct foo*)b)->f = y;
+ }
+
+
+ return a->f;
+}
+
+int main()
+{
+ struct bar { long x; };
+ struct foo { struct bar *f; } a;
+ struct bar x, y;
+ if (&y != alias(&a, &a, &x, &y))
+ __builtin_abort();
+
+ return 0;
+}
+
--
2.39.2