This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Partially fix PR c++/12277 (Warn on dynamic cast with known NULL results)
- From: Patrick Palka <patrick at parcs dot ath dot cx>
- To: Jason Merrill <jason at redhat dot com>
- Cc: Patrick Palka <patrick at parcs dot ath dot cx>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Wed, 3 Feb 2016 22:07:48 -0500
- Subject: Re: [PATCH] Partially fix PR c++/12277 (Warn on dynamic cast with known NULL results)
- Authentication-results: sourceware.org; auth=none
- References: <1447039858-9220-1-git-send-email-patrick at parcs dot ath dot cx> <56B10053 dot 80104 at redhat dot com> <alpine dot DEB dot 2 dot 20 dot 9 dot 1602031330360 dot 1505 at idea>
On Wed, Feb 3, 2016 at 1:40 PM, Patrick Palka <patrick@parcs.ath.cx> wrote:
> On Tue, 2 Feb 2016, Jason Merrill wrote:
>
>> On 11/09/2015 04:30 AM, Patrick Palka wrote:
>>>
>>> + if (complain & tf_warning)
>>> + {
>>> + if (VAR_P (old_expr))
>>> + warning (0, "dynamic_cast of %q#D to %q#T can never
>>> succeed",
>>> + old_expr, type);
>>> + else
>>> + warning (0, "dynamic_cast of %q#E to %q#T can never
>>> succeed",
>>> + old_expr, type);
>>> + }
>>> + return build_zero_cst (type);
>>
>>
>> You also need to handle throwing bad_cast in the reference case.
>
>
> Oops, fixed. I also updated the test case to confirm that the expected
> number of calls to bad_cast is generated.
>
> -- >8 --
>
> gcc/cp/ChangeLog:
>
> PR c++/12277
> * rtti.c (build_dynamic_cast_1): Warn on dynamic_cast that can
> never succeed due to either the target type or the static type
> being marked final.
>
> gcc/testsuite/ChangeLog:
>
> PR c++/12277
> * g++.dg/rtti/dyncast8.C: New test.
> ---
> gcc/cp/rtti.c | 26 ++++++++++++++++++
> gcc/testsuite/g++.dg/rtti/dyncast8.C | 53
> ++++++++++++++++++++++++++++++++++++
> 2 files changed, 79 insertions(+)
> create mode 100644 gcc/testsuite/g++.dg/rtti/dyncast8.C
>
> diff --git a/gcc/cp/rtti.c b/gcc/cp/rtti.c
> index a43ff85..b1454d9 100644
> --- a/gcc/cp/rtti.c
> +++ b/gcc/cp/rtti.c
> @@ -694,6 +694,32 @@ build_dynamic_cast_1 (tree type, tree expr,
> tsubst_flags_t complain)
>
> target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
> static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
> +
> + if ((CLASSTYPE_FINAL (static_type)
> + && !DERIVED_FROM_P (target_type, static_type))
> + || (CLASSTYPE_FINAL (target_type)
> + && !DERIVED_FROM_P (static_type, target_type)))
> + {
> + if (complain & tf_warning)
> + {
> + if (VAR_P (old_expr))
> + warning (0, "dynamic_cast of %q#D to %q#T can never
> succeed",
> + old_expr, type);
> + else
> + warning (0, "dynamic_cast of %q#E to %q#T can never
> succeed",
> + old_expr, type);
> + }
It just occurred to me that issuing this warning during template
instantiation may be undesirable if the dynamic_cast being built was
originally dependent (for the same reasons it is undesirable to issue
-Wuseless-cast warnings during instantiation, which we already avoid
doing). Especially so since this particular warning is not associated
with any warning flag. I'll try to come up with a more careful patch
for stage 1.