diff options
author | Jakub Jelinek <jakub@redhat.com> | 2021-11-26 10:11:13 +0100 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2021-11-26 10:16:20 +0100 |
commit | 8dedf065aff22b09bd4e48e1be0b77c58d297100 (patch) | |
tree | f9063b4c4b6aad9b441a7916cfdce755ae1eaab7 /gcc/cp/cp-gimplify.c | |
parent | 90cb088ece8d8cc1019d25629d1585e5b0234179 (diff) | |
download | gcc-8dedf065aff22b09bd4e48e1be0b77c58d297100.zip gcc-8dedf065aff22b09bd4e48e1be0b77c58d297100.tar.gz gcc-8dedf065aff22b09bd4e48e1be0b77c58d297100.tar.bz2 |
c++: Fix up taking address of an immediate function diagnostics [PR102753]
On Wed, Oct 20, 2021 at 07:16:44PM -0400, Jason Merrill wrote:
> or an unevaluated operand, or a subexpression of an immediate invocation.
>
> Hmm...that suggests that in consteval23.C, bar(foo) should also be OK,
The following patch handles that by removing the diagnostics about taking
address of immediate function from cp_build_addr_expr_1, and instead diagnoses
it in cp_fold_r. To do that with proper locations, the patch attempts to
ensure that ADDR_EXPRs of immediate functions get EXPR_LOCATION set and
adds a PTRMEM_CST_LOCATION for PTRMEM_CSTs. Also, evaluation of
std::source_location::current() is moved from genericization to cp_fold.
2021-11-26 Jakub Jelinek <jakub@redhat.com>
PR c++/102753
* cp-tree.h (struct ptrmem_cst): Add locus member.
(PTRMEM_CST_LOCATION): Define.
* tree.c (make_ptrmem_cst): Set PTRMEM_CST_LOCATION to input_location.
(cp_expr_location): Return PTRMEM_CST_LOCATION for PTRMEM_CST.
* typeck.c (build_x_unary_op): Overwrite PTRMEM_CST_LOCATION for
PTRMEM_CST instead of calling maybe_wrap_with_location.
(cp_build_addr_expr_1): Don't diagnose taking address of
immediate functions here. Instead when taking their address make
sure the returned ADDR_EXPR has EXPR_LOCATION set.
(expand_ptrmemfunc_cst): Copy over PTRMEM_CST_LOCATION to ADDR_EXPR's
EXPR_LOCATION.
(convert_for_assignment): Use cp_expr_loc_or_input_loc instead of
EXPR_LOC_OR_LOC.
* pt.c (tsubst_copy): Use build1_loc instead of build1. Ensure
ADDR_EXPR of immediate function has EXPR_LOCATION set.
* cp-gimplify.c (cp_fold_r): Diagnose taking address of immediate
functions here. For consteval if don't walk THEN_CLAUSE.
(cp_genericize_r): Move evaluation of calls to
std::source_location::current from here to...
(cp_fold): ... here. Don't assert calls to immediate functions must
be source_location_current_p, instead only constant evaluate
calls to source_location_current_p.
* g++.dg/cpp2a/consteval20.C: Add some extra tests.
* g++.dg/cpp2a/consteval23.C: Likewise.
* g++.dg/cpp2a/consteval25.C: New test.
* g++.dg/cpp2a/srcloc20.C: New test.
Diffstat (limited to 'gcc/cp/cp-gimplify.c')
-rw-r--r-- | gcc/cp/cp-gimplify.c | 77 |
1 files changed, 64 insertions, 13 deletions
diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index c1691c3..2fbb423 100644 --- a/gcc/cp/cp-gimplify.c +++ b/gcc/cp/cp-gimplify.c @@ -900,8 +900,39 @@ struct cp_genericize_data static tree cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data) { - tree stmt; - enum tree_code code; + tree stmt = *stmt_p; + enum tree_code code = TREE_CODE (stmt); + + switch (code) + { + case PTRMEM_CST: + if (TREE_CODE (PTRMEM_CST_MEMBER (stmt)) == FUNCTION_DECL + && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (stmt))) + { + if (!((hash_set<tree> *) data)->add (stmt)) + error_at (PTRMEM_CST_LOCATION (stmt), + "taking address of an immediate function %qD", + PTRMEM_CST_MEMBER (stmt)); + stmt = *stmt_p = build_zero_cst (TREE_TYPE (stmt)); + break; + } + break; + + case ADDR_EXPR: + if (TREE_CODE (TREE_OPERAND (stmt, 0)) == FUNCTION_DECL + && DECL_IMMEDIATE_FUNCTION_P (TREE_OPERAND (stmt, 0))) + { + error_at (EXPR_LOCATION (stmt), + "taking address of an immediate function %qD", + TREE_OPERAND (stmt, 0)); + stmt = *stmt_p = build_zero_cst (TREE_TYPE (stmt)); + break; + } + break; + + default: + break; + } *stmt_p = stmt = cp_fold (*stmt_p); @@ -917,12 +948,16 @@ cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data) } code = TREE_CODE (stmt); - if (code == OMP_FOR || code == OMP_SIMD || code == OMP_DISTRIBUTE - || code == OMP_LOOP || code == OMP_TASKLOOP || code == OACC_LOOP) + switch (code) { tree x; int i, n; - + case OMP_FOR: + case OMP_SIMD: + case OMP_DISTRIBUTE: + case OMP_LOOP: + case OMP_TASKLOOP: + case OACC_LOOP: cp_walk_tree (&OMP_FOR_BODY (stmt), cp_fold_r, data, NULL); cp_walk_tree (&OMP_FOR_CLAUSES (stmt), cp_fold_r, data, NULL); cp_walk_tree (&OMP_FOR_INIT (stmt), cp_fold_r, data, NULL); @@ -961,6 +996,22 @@ cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data) } cp_walk_tree (&OMP_FOR_PRE_BODY (stmt), cp_fold_r, data, NULL); *walk_subtrees = 0; + return NULL; + + case IF_STMT: + if (IF_STMT_CONSTEVAL_P (stmt)) + { + /* Don't walk THEN_CLAUSE (stmt) for consteval if. IF_COND is always + boolean_false_node. */ + cp_walk_tree (&ELSE_CLAUSE (stmt), cp_fold_r, data, NULL); + cp_walk_tree (&IF_SCOPE (stmt), cp_fold_r, data, NULL); + *walk_subtrees = 0; + return NULL; + } + break; + + default: + break; } return NULL; @@ -1476,14 +1527,6 @@ cp_genericize_r (tree *stmt_p, int *walk_subtrees, void *data) break; } - if (tree fndecl = cp_get_callee_fndecl_nofold (stmt)) - if (DECL_IMMEDIATE_FUNCTION_P (fndecl)) - { - gcc_assert (source_location_current_p (fndecl)); - *stmt_p = cxx_constant_value (stmt); - break; - } - if (!wtd->no_sanitize_p && sanitize_flags_p ((SANITIZE_NULL | SANITIZE_ALIGNMENT | SANITIZE_VPTR))) @@ -2629,6 +2672,14 @@ cp_fold (tree x) int sv = optimize, nw = sv; tree callee = get_callee_fndecl (x); + if (tree fndecl = cp_get_callee_fndecl_nofold (x)) + if (DECL_IMMEDIATE_FUNCTION_P (fndecl) + && source_location_current_p (fndecl)) + { + x = cxx_constant_value (x); + break; + } + /* Some built-in function calls will be evaluated at compile-time in fold (). Set optimize to 1 when folding __builtin_constant_p inside a constexpr function so that fold_builtin_1 doesn't fold it to 0. */ |