From patchwork Wed Feb 3 18:40:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 578286 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 76B1E140AD9 for ; Thu, 4 Feb 2016 05:40:46 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=IbQdTPpQ; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :date:to:cc:subject:in-reply-to:message-id:references :mime-version:content-type; q=dns; s=default; b=qufDF+VLQNd6n8J2 GxtLNe0GxqK5lViTcB27XpGB8tXWjk414bTPXAwbzPhtqOL6O7ipqAK3ju45FpQS qwQijyxdVVOiTTBEwqYzPchX8i74JEFbrWzulclbYp/qR8vGk9QpHR/j0jtZXZIi YtcB51ta4EzScENdFgLTHXl6FqY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :date:to:cc:subject:in-reply-to:message-id:references :mime-version:content-type; s=default; bh=0Y+TGmnhhTSA6vy1cArpVX 2eZqM=; b=IbQdTPpQjzclWtgagAZTAKpJyJNACwZ/lo5V1/SSg06sHcwtUPKZ49 /k4hlctN38F/jU+a1WjIQKFpDEae3zjvije/CrHxHB/5cesuTlAj/5tGUpph5vk2 E6r8MVRVPpLdeLCJTIjt+8yFyjqQQ3LaofHl7aoFpAY0F0WobRkyg= Received: (qmail 117643 invoked by alias); 3 Feb 2016 18:40:39 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 117585 invoked by uid 89); 3 Feb 2016 18:40:34 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 spammy=Partially, bad_cast, 12277, old_expr X-HELO: mail-qg0-f50.google.com Received: from mail-qg0-f50.google.com (HELO mail-qg0-f50.google.com) (209.85.192.50) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Wed, 03 Feb 2016 18:40:33 +0000 Received: by mail-qg0-f50.google.com with SMTP id b35so22589086qge.0 for ; Wed, 03 Feb 2016 10:40:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:date:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version:content-type; bh=SalA7g6xezW0oM3v5qlm17eNtwDAKHNojFYnvHaPb/Q=; b=kz6Roc62dfwidUcOs1HkKQ1/guEPZ7PK+Mh/vGsjQcJoo+8UHS1ZxtqP1P0Sn6XzKb Examov5fMzi0g31dW60WE/cKiyrEcrvB4nuMZe0oAMTfZQP8bNXd5Wv36PHJbOiO9h9g GMjjGLcii06wZAHu8nU3vxGFjT40U6IFSX3/wQJBLZbTWEE/cq1gBZY4CMFtM5TCUeoI JYmplCFiNgnlciEF3SVap2oEwXlbtLY/XTn2dP0FciklXSUv8EcfnSgHWNujelKrqBzq A83/zqjrySwTlZLENkIKwO+WoPIHCdbHnz9o8Yri1l+aitC34UOS39Q8tMxmPv3SJenj vFZA== X-Gm-Message-State: AG10YOTnRb9K+fzPupiW6UyNKm0eQZA/n+elG1Lh21bRe+xS5GBuGD6e7M5BckB9H3TtOg== X-Received: by 10.140.180.22 with SMTP id b22mr3727184qha.49.1454524830351; Wed, 03 Feb 2016 10:40:30 -0800 (PST) Received: from [192.168.1.130] (ool-4353abbc.dyn.optonline.net. [67.83.171.188]) by smtp.gmail.com with ESMTPSA id f37sm3184382qkf.6.2016.02.03.10.40.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 Feb 2016 10:40:29 -0800 (PST) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Wed, 3 Feb 2016 13:40:28 -0500 (EST) To: Jason Merrill cc: Patrick Palka , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] Partially fix PR c++/12277 (Warn on dynamic cast with known NULL results) In-Reply-To: <56B10053.80104@redhat.com> Message-ID: References: <1447039858-9220-1-git-send-email-patrick@parcs.ath.cx> <56B10053.80104@redhat.com> User-Agent: Alpine 2.20.9 (DEB 106 2015-09-22) MIME-Version: 1.0 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); + } + + if (tc == REFERENCE_TYPE) + { + tree expr = throw_bad_cast (); + TREE_TYPE (expr) = type; + return expr; + } + else + return build_zero_cst (type); + } + td2 = get_tinfo_decl (target_type); if (!mark_used (td2, complain) && !(complain & tf_error)) return error_mark_node; diff --git a/gcc/testsuite/g++.dg/rtti/dyncast8.C b/gcc/testsuite/g++.dg/rtti/dyncast8.C new file mode 100644 index 0000000..f63ed5d --- /dev/null +++ b/gcc/testsuite/g++.dg/rtti/dyncast8.C @@ -0,0 +1,53 @@ +// PR c++/12277 +// { dg-do compile { target c++11 } } +// { dg-additional-options "-fdump-tree-original" } + +struct A1 { virtual ~A1 () { } }; +struct A2 { virtual ~A2 () { } }; + +struct B1 { virtual ~B1 () { } }; +struct B2 final : B1 { virtual ~B2 () { } }; + +struct C1 { virtual ~C1 () { } }; +struct C2 final { virtual ~C2 () { } }; + +A1 *a1; + +B1 *b1; +B2 *b2; + +C1 *c1; +C2 *c2; + +void +foo (void) +{ + { + A2 *x = dynamic_cast (a1); + } + + { + B2 *x = dynamic_cast (b1); + // The following cast may throw bad_cast. + B2 &y = dynamic_cast (*b1); + } + + { + B1 *x = dynamic_cast (b2); + B1 &y = dynamic_cast (*b2); + } + + { + C2 *x = dynamic_cast (c1); // { dg-warning "can never succeed" } + // The following cast may throw bad_cast. + C2 &y = dynamic_cast (*c1); // { dg-warning "can never succeed" } + } + + { + C1 *x = dynamic_cast (c2); // { dg-warning "can never succeed" } + // The following cast may throw bad_cast. + C1 &y = dynamic_cast (*c2); // { dg-warning "can never succeed" } + } +} + +// { dg-final { scan-tree-dump-times "bad_cast" 3 "original" } }