From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id B68FF3858D3C; Mon, 17 Jan 2022 17:11:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B68FF3858D3C MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-6643] c++: Fix cp_genericize_target_expr for TARGET_EXPRs created for global initialization [PR104031] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: d3a57993359c9759990fe8f2aa4088684ed82190 X-Git-Newrev: aeca44768d54b089243004d1ef00d34dfa9f6530 Message-Id: <20220117171116.B68FF3858D3C@sourceware.org> Date: Mon, 17 Jan 2022 17:11:16 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 17 Jan 2022 17:11:16 -0000 https://gcc.gnu.org/g:aeca44768d54b089243004d1ef00d34dfa9f6530 commit r12-6643-gaeca44768d54b089243004d1ef00d34dfa9f6530 Author: Jakub Jelinek Date: Mon Jan 17 18:10:34 2022 +0100 c++: Fix cp_genericize_target_expr for TARGET_EXPRs created for global initialization [PR104031] The following patch is miscompiled, cp_genericize_target_expr expects that for the constant part split_nonconstant_init will emit an INIT_EXPR that will initialize it, but that doesn't happen and instead we get DECL_INITIAL on the TARGET_EXPR_SLOT that isn't initialized anywhere in the IL. The problem is that the TARGET_EXPR has been created while current_function_decl was NULL, it is inside a global var initializer. That means the build_local_temp created VAR_DECL has NULL DECL_CONTEXT. Later on when genericizing the ssdf (current_function_decl is already non-NULL), the new cp_genericize_target_expr is called and during split_nonconstant_init it checks is_local_temp, but that due to the NULL DECL_CONTEXT returns false. DECL_CONTEXT is set only later on during gimplification. The following patch fixes it by setting DECL_CONTEXT also inside of cp_genericize_target_expr, which fixes the testcase. But if there are better spots to do that, please let me know... 2022-01-17 Jakub Jelinek PR c++/104031 * cp-gimplify.c (cp_genericize_target_expr): Set DECL_CONTEXT of TARGET_EXPR_SLOT to current_function_decl if it was NULL. * g++.dg/cpp1y/pr104031.C: New test. Diff: --- gcc/cp/cp-gimplify.c | 9 +++++++-- gcc/testsuite/g++.dg/cpp1y/pr104031.C | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index b80010e0983..ad610309953 100644 --- a/gcc/cp/cp-gimplify.c +++ b/gcc/cp/cp-gimplify.c @@ -930,9 +930,14 @@ cp_genericize_init_expr (tree *stmt_p) static void cp_genericize_target_expr (tree *stmt_p) { + tree slot = TARGET_EXPR_SLOT (*stmt_p); + /* If TARGET_EXPR is created for some global var initializer, the slot + will have NULL and so is_local_temp will return false for it. If + this is a ssdf, set DECL_CONTEXT now. */ + if (DECL_CONTEXT (slot) == NULL_TREE) + DECL_CONTEXT (slot) = current_function_decl; cp_genericize_init (&TARGET_EXPR_INITIAL (*stmt_p), - TARGET_EXPR_INITIAL (*stmt_p), - TARGET_EXPR_SLOT (*stmt_p)); + TARGET_EXPR_INITIAL (*stmt_p), slot); } /* Genericization context. */ diff --git a/gcc/testsuite/g++.dg/cpp1y/pr104031.C b/gcc/testsuite/g++.dg/cpp1y/pr104031.C new file mode 100644 index 00000000000..5f736621ae1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/pr104031.C @@ -0,0 +1,23 @@ +// PR c++/104031 +// { dg-do run { target c++14 } } +// { dg-options "-O2" } + +struct A { + A () {} + ~A () {} +}; +struct B { + A a; + int b = 0; +}; +struct C +{ + [[gnu::noipa]] + C (B x) { if (x.b != 42) __builtin_abort (); } +}; +static C c ({ .a = A{}, .b = 42 }); + +int +main () +{ +}