Skip to content

Commit 9b9a989

Browse files
committed
c++: Fix up handling of references to anon union members in initializers [PR53932]
For anonymous union members we create artificial VAR_DECLs which have DECL_VALUE_EXPR for the actual COMPONENT_REF. That works just fine inside of functions (including global dynamic constructors), because during gimplification such VAR_DECLs are gimplified as their DECL_VALUE_EXPR. This is also done during regimplification. But references to these artificial vars in DECL_INITIAL expressions aren't ever replaced by the DECL_VALUE_EXPRs, so we end up either with link failures like on the testcase below, or worse ICEs with LTO. The following patch fixes those during cp_fully_fold_init where we already walk all the trees (!data->genericize means that function rather than cp_fold_function). 2023-01-19 Jakub Jelinek <[email protected]> PR c++/53932 * cp-gimplify.cc (cp_fold_r): During cp_fully_fold_init replace DECL_ANON_UNION_VAR_P VAR_DECLs with their corresponding DECL_VALUE_EXPR. * g++.dg/init/pr53932.C: New test.
1 parent c81e68a commit 9b9a989

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

gcc/cp/cp-gimplify.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,16 @@ cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_)
10101010
}
10111011
break;
10121012

1013+
case VAR_DECL:
1014+
/* In initializers replace anon union artificial VAR_DECLs
1015+
with their DECL_VALUE_EXPRs, as nothing will do it later. */
1016+
if (DECL_ANON_UNION_VAR_P (stmt) && !data->genericize)
1017+
{
1018+
*stmt_p = stmt = unshare_expr (DECL_VALUE_EXPR (stmt));
1019+
break;
1020+
}
1021+
break;
1022+
10131023
default:
10141024
break;
10151025
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// PR c++/53932
2+
// { dg-do link }
3+
4+
static union { int i; };
5+
int &r = i;
6+
int s = i;
7+
int *t = &i;
8+
9+
void
10+
foo (int **p, int *q)
11+
{
12+
static int &u = i;
13+
static int v = i;
14+
static int *w = &i;
15+
int &x = i;
16+
int y = i;
17+
int *z = &i;
18+
*p = &i;
19+
*q = i;
20+
}
21+
22+
int
23+
main ()
24+
{
25+
}

0 commit comments

Comments
 (0)