Per recent Coverity feedback on PR126207, I'd suggest a small refactor: add a nonnull out-parameter to typeid_evaluated_p. This:
1. avoids calling resolves_to_fixed_type_p twice (once inside typeid_evaluated_p, once again in build_typeid), and 2. removes the unchecked return value Coverity flagged in the second call.
From 61d04aae2e87e5310212589b2358c0561684795d Mon Sep 17 00:00:00 2001 From: Vladislav Semykin <[email protected]> Date: Mon, 13 Jul 2026 10:04:28 +0300 Subject: [PATCH] c++: avoid double resolves_to_fixed_type_p call [PR126207] To: [email protected] gcc/cp/ChangeLog: PR c++/126207 * cp-tree.h (typeid_evaluated_p): Add nonnull out-parameter. * rtti.cc (typeid_evaluated_p): Likewise; avoid calling resolves_to_fixed_type_p twice. (build_typeid): Adjust. Signed-off-by: Vladislav Semykin <[email protected]> --- gcc/cp/cp-tree.h | 2 +- gcc/cp/rtti.cc | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 8dac8c98b7a..d5363a14d4e 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7486,7 +7486,7 @@ extern tree current_nonlambda_class_type (void); extern tree finish_struct (tree, tree); extern void finish_struct_1 (tree); extern int resolves_to_fixed_type_p (tree, int * = NULL); -extern bool typeid_evaluated_p (tree); +extern bool typeid_evaluated_p (tree, int * = NULL); extern void init_class_processing (void); extern int is_empty_class (tree); extern bool is_really_empty_class (tree, bool); diff --git a/gcc/cp/rtti.cc b/gcc/cp/rtti.cc index 48e4dfde458..ea6c4b53154 100644 --- a/gcc/cp/rtti.cc +++ b/gcc/cp/rtti.cc @@ -345,7 +345,7 @@ typeid_ok_p (void) evaluated per ([expr.typeid]/4). */ bool -typeid_evaluated_p (tree exp) +typeid_evaluated_p (tree exp, int *nonnull) { if (exp == error_mark_node) return false; @@ -356,9 +356,12 @@ typeid_evaluated_p (tree exp) t = TREE_TYPE (t); if (TREE_CODE (t) != RECORD_TYPE && TREE_CODE (t) != UNION_TYPE) return false; - int nonnull = 0; + int nn = 0; + bool fixed = resolves_to_fixed_type_p (exp, &nn); + if (nonnull) + *nonnull = nn; return (TYPE_POLYMORPHIC_P (t) - && !resolves_to_fixed_type_p (exp, &nonnull) + && !fixed /* Only a glvalue operand is evaluated ([expr.typeid]/4). The following check is only necessary because resolves_to_fixed_type_p does not handle all @@ -380,10 +383,9 @@ build_typeid (tree exp, tsubst_flags_t complain) if (processing_template_decl) return build_min (TYPEID_EXPR, const_type_info_type_node, exp); - if (typeid_evaluated_p (exp)) + int nonnull = 0; + if (typeid_evaluated_p (exp, &nonnull)) { - int nonnull = 0; - resolves_to_fixed_type_p (exp, &nonnull); if (!nonnull) { /* Make sure it isn't a null lvalue; evaluate it once. */ base-commit: 6df03d626ffe309c4ca1e36fb08441325dc1c46e -- 2.43.0
