From patchwork Tue Apr 8 01:09:24 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siva Chandra Reddy X-Patchwork-Id: 424 Return-Path: X-Original-To: siddhesh@wilcox.dreamhost.com Delivered-To: siddhesh@wilcox.dreamhost.com Received: from homiemail-mx22.g.dreamhost.com (mx2.sub5.homie.mail.dreamhost.com [208.113.200.128]) by wilcox.dreamhost.com (Postfix) with ESMTP id 15DFE360072 for ; Mon, 7 Apr 2014 18:09:34 -0700 (PDT) Received: by homiemail-mx22.g.dreamhost.com (Postfix, from userid 14314964) id ABFA0460F39A; Mon, 7 Apr 2014 18:09:33 -0700 (PDT) X-Original-To: gdb@patchwork.siddhesh.in Delivered-To: x14314964@homiemail-mx22.g.dreamhost.com 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 homiemail-mx22.g.dreamhost.com (Postfix) with ESMTPS id 8100E460F38C for ; Mon, 7 Apr 2014 18:09:33 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:date:message-id:subject:from:to :content-type; q=dns; s=default; b=YKnE8/U1J8Gbgzc2ZfS6cch0mMXYD 67mSPBKOTwCXUFPI3QzH5sypjul2fDYrdL0Cy2DFAn7x1GLO2Ywc7DQIBMxHAU1N Mf6BvhUoGl7QAhk1MjS8AWIrbaEcL50x2T38ZZ+B/DKPajlSiawpUGHCxUAfdRyz a1w64HEwBGldrs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:mime-version:date:message-id:subject:from:to :content-type; s=default; bh=e0fbx5S+Dxm6qzjM3ki4McOBenM=; b=xvs kn7PeppISkQI0C3JpUGuMNtXayZttSLGsbZsVA8igjE2NIhEyqzlN4IZYv8l9nRx c4bsUdYK92OcZOgL/oOtC6A5LLRbEi8eN23hYTDUXlh6Gil3b+mOnWgTKDwCGNzw JUCmUo+kgJg4sCC4b93l0Tm8ngaLJrnmMes0ZSdY= Received: (qmail 14904 invoked by alias); 8 Apr 2014 01:09:31 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 14886 invoked by uid 89); 8 Apr 2014 01:09:29 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.7 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-ee0-f52.google.com Received: from mail-ee0-f52.google.com (HELO mail-ee0-f52.google.com) (74.125.83.52) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Tue, 08 Apr 2014 01:09:28 +0000 Received: by mail-ee0-f52.google.com with SMTP id e49so93519eek.39 for ; Mon, 07 Apr 2014 18:09:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:date:message-id:subject:from:to :content-type; bh=ekJpZNn9+amMkA+XLrIs6gOQOvn6d7MaSraIdPuBbS4=; b=O4zUKX8hCNKB8tECLHeUJc16MLRYKlaGxxqKLQjKrF+ifXNQ/HVIw7eLjbSNrkooow vDTB1gZHhhVsARGo4x+qxDqjGc8J6TvRvChXBzhHzWpG9j4M21TtyseBZdJEtCH6Ezks qFkNiSMnneTy0RfkzpotkAN1IWemiORDMqaATyfvLVEn3LAT2A80aX2ZJ7gycbuy1C2P zO0tqJUTTrVbk5edYrJYBzbjMowqBDEn4cVHKnm2fxFnMpvf7np20XLyFefnk/mlA5Vy BSkiFLURXMvs4H59/71L+QkOLr/Z8J8hGZnFYqq8h1PMz68ipKlJP66DEiwR+DCYMCaV oeAA== X-Gm-Message-State: ALoCoQk1oejY4qIfI95czyd1z8dt73jgkMHxs3Rhj+Q8CjH8k2nRoRX6ZmbZ0H2wLTrFi7853ZZ9f/2t+h0+Hp0w3J2sNAvYuctIMtcIBLNngp6qvT+0E8MQz2podhEaHAz2nMwqo4kcX+xHM281Dmnrg+zhzZZbwEUqa+NI8ERGzt4RTAOmMvEY1bVNPmPxd/Jybp4uBNQl1I6LXeKXTdfZcwnPtBd4CQ== MIME-Version: 1.0 X-Received: by 10.15.102.74 with SMTP id bq50mr319989eeb.21.1396919364621; Mon, 07 Apr 2014 18:09:24 -0700 (PDT) Received: by 10.15.48.194 with HTTP; Mon, 7 Apr 2014 18:09:24 -0700 (PDT) Date: Mon, 7 Apr 2014 18:09:24 -0700 Message-ID: Subject: [PATCH] Fix dynamic_cast operator From: Siva Chandra To: gdb-patches X-IsSubscribed: yes X-DH-Original-To: gdb@patchwork.siddhesh.in For my debug methods patch, I have used value_cast instead of value_dyamic_cast at a few places as the latter was spewing errors. Digging a bit, I discovered that value_dynamic_cast has been broken since it was added. The tests have been passing because, 1) some tests had a strange expected result pattern and, 2) tests are not thorough enough IMO. The attached patch fixes the dynamic_cast operator and addresses the shortcoming #1 of the tests. Consider the following GDB session to understand #2: (gdb) p dynamic_cast(ap) $1 = (B *) 0x7fffffffdf00 The tests do not verify the hex addresses printed by the dynamic_cast operator; they merely verify that a hex number is being printed. I think the tests should verify the address printed (in some direct or indirect manner). I feel this is important because the target subobject could be present at different locations in/around the most derived object based on the type of inheritance. I do not know of a straight forward way to add this check, but I think it can addressed in a different patch if required. ChangeLog: 2014-04-07 Siva Chandra Reddy * valops.c (value_dynamic_cast): Fix mixup between rtti_type and resolved_type. testsuite/ * gdb.cp/casts.exp: Adjust expected result for few tests. diff --git a/gdb/testsuite/gdb.cp/casts.exp b/gdb/testsuite/gdb.cp/casts.exp index 9122450..dd848db 100644 --- a/gdb/testsuite/gdb.cp/casts.exp +++ b/gdb/testsuite/gdb.cp/casts.exp @@ -151,8 +151,7 @@ gdb_test "print dynamic_cast (derived)" \ "dynamic_cast simple upcast to reference" gdb_test "print dynamic_cast (ad)" \ - " = \\(Derived \\*\\) ${nonzero_hex}( )?" \ - "dynamic_cast simple downcast" + " = \\(Derived \\*\\) ${nonzero_hex}" "dynamic_cast simple downcast" gdb_test "print dynamic_cast (add)" \ " = \\(VirtuallyDerived \\*\\) $nonzero_hex" \ @@ -167,8 +166,7 @@ gdb_test "print dynamic_cast (*ad)" \ "dynamic_cast to reference to non-existing base" gdb_test "print dynamic_cast (add)" \ - " = \\(DoublyDerived \\*\\) ${nonzero_hex}( )?" \ - "dynamic_cast unique downcast" + " = \\(DoublyDerived \\*\\) ${nonzero_hex}" "dynamic_cast unique downcast" gdb_test "print dynamic_cast (add)" \ " = \\(Gamma \\*\\) $nonzero_hex" \ diff --git a/gdb/valops.c b/gdb/valops.c index cf195a3..bd4b963 100644 --- a/gdb/valops.c +++ b/gdb/valops.c @@ -800,13 +800,14 @@ value_dynamic_cast (struct type *type, struct value *arg) && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) == TYPE_CODE_VOID) return value_at_lazy (type, addr); - tem = value_at (type, addr); + tem = value_at (rtti_type, addr); /* The first dynamic check specified in 5.2.7. */ if (is_public_ancestor (arg_type, TYPE_TARGET_TYPE (resolved_type))) { if (class_types_same_p (rtti_type, TYPE_TARGET_TYPE (resolved_type))) - return tem; + return value_from_pointer (type, addr); + result = NULL; if (dynamic_cast_check_1 (TYPE_TARGET_TYPE (resolved_type), value_contents_for_printing (tem),