From patchwork Sun Feb 5 19:10:35 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 139659 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]) by ozlabs.org (Postfix) with SMTP id 914611007D2 for ; Mon, 6 Feb 2012 06:11:03 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1329073864; h=Comment: DomainKey-Signature:Received:Received:Received:Received: MIME-Version:Received:Received:Date:Message-ID:Subject:From:To: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=hsPU/dx oE+aGPIB5c1VCAUktwTo=; b=qRZ2zZDnplobLRw34C2B/GLYlD8zuIF5GqTUqNe toVWK7E7VuOLjo/ykkvmD90iKLVZVU4yED5iqF5g9rJFXlHAipc56SMH9kfF6W8P PXaLu8OroIP1vbuvYYx5odOATCNNcZ0lcElMBvDS/bkHrATpxOBruNIKWAXlQGkH TSXQ= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:MIME-Version:Received:Received:Date:Message-ID:Subject:From:To:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=Gn9Z5PI/Szbmgw+y168FMuhQhm1lSAIHXY2Qa5+oeS7NUNwv36C+MjvJYMIqF/ EJvwavhEa1B6kj8kB17nvki0tSZVdA9rA4DXdaCHyDfwj/dPBD7dbJLP8dosiCnn /gABFUv85FOG6ESd9ij2gKnw4niGfCidpR+xmcvLKTrVk=; Received: (qmail 5074 invoked by alias); 5 Feb 2012 19:10:55 -0000 Received: (qmail 4803 invoked by uid 22791); 5 Feb 2012 19:10:51 -0000 X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW X-Spam-Check-By: sourceware.org Received: from mail-lpp01m010-f47.google.com (HELO mail-lpp01m010-f47.google.com) (209.85.215.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sun, 05 Feb 2012 19:10:37 +0000 Received: by lahc1 with SMTP id c1so2748288lah.20 for ; Sun, 05 Feb 2012 11:10:35 -0800 (PST) MIME-Version: 1.0 Received: by 10.152.123.10 with SMTP id lw10mr8615759lab.35.1328469035144; Sun, 05 Feb 2012 11:10:35 -0800 (PST) Received: by 10.112.39.42 with HTTP; Sun, 5 Feb 2012 11:10:35 -0800 (PST) Date: Sun, 5 Feb 2012 19:10:35 +0000 Message-ID: Subject: [v3] libstdc++/51956 improve pretty printers for shared_ptr and weak_ptr From: Jonathan Wakely To: "libstdc++" , gcc-patches 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 PR libstdc++/51956 * python/libstdcxx/v6/printers.py (StdPointerPrinter): Rename to... (SharedPointerPrinter): This. Also show weak count. * testsuite/libstdc++-prettyprinters/shared_ptr.cc: New. tested x86_64-linux, committed to trunk. diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py index 4f34733..f47da61 100644 --- a/libstdc++-v3/python/libstdcxx/v6/printers.py +++ b/libstdc++-v3/python/libstdcxx/v6/printers.py @@ -45,19 +45,24 @@ def find_type(orig, name): raise ValueError, "Cannot find type %s::%s" % (str(orig), name) typ = field.type -class StdPointerPrinter: - "Print a smart pointer of some kind" +class SharedPointerPrinter: + "Print a shared_ptr or weak_ptr" def __init__ (self, typename, val): self.typename = typename self.val = val def to_string (self): - if self.val['_M_refcount']['_M_pi'] == 0: - return '%s (empty) %s' % (self.typename, self.val['_M_ptr']) - return '%s (count %d) %s' % (self.typename, - self.val['_M_refcount']['_M_pi']['_M_use_count'], - self.val['_M_ptr']) + state = 'empty' + refcounts = self.val['_M_refcount']['_M_pi'] + if refcounts != 0: + usecount = refcounts['_M_use_count'] + weakcount = refcounts['_M_weak_count'] + if usecount == 0: + state = 'expired, weak %d' % weakcount + else: + state = 'count %d, weak %d' % (usecount, weakcount - 1) + return '%s (%s) %s' % (self.typename, state, self.val['_M_ptr']) class UniquePointerPrinter: "Print a unique_ptr" @@ -862,8 +867,8 @@ def build_libstdcxx_dictionary (): # These are the TR1 and C++0x printers. # For array - the default GDB pretty-printer seems reasonable. - libstdcxx_printer.add_version('std::', 'shared_ptr', StdPointerPrinter) - libstdcxx_printer.add_version('std::', 'weak_ptr', StdPointerPrinter) + libstdcxx_printer.add_version('std::', 'shared_ptr', SharedPointerPrinter) + libstdcxx_printer.add_version('std::', 'weak_ptr', SharedPointerPrinter) libstdcxx_printer.add_container('std::', 'unordered_map', Tr1UnorderedMapPrinter) libstdcxx_printer.add_container('std::', 'unordered_set', @@ -875,8 +880,8 @@ def build_libstdcxx_dictionary (): libstdcxx_printer.add_container('std::', 'forward_list', StdForwardListPrinter) - libstdcxx_printer.add_version('std::tr1::', 'shared_ptr', StdPointerPrinter) - libstdcxx_printer.add_version('std::tr1::', 'weak_ptr', StdPointerPrinter) + libstdcxx_printer.add_version('std::tr1::', 'shared_ptr', SharedPointerPrinter) + libstdcxx_printer.add_version('std::tr1::', 'weak_ptr', SharedPointerPrinter) libstdcxx_printer.add_version('std::tr1::', 'unordered_map', Tr1UnorderedMapPrinter) libstdcxx_printer.add_version('std::tr1::', 'unordered_set', diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/shared_ptr.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/shared_ptr.cc new file mode 100644 index 0000000..9328c5f9 --- /dev/null +++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/shared_ptr.cc @@ -0,0 +1,83 @@ +// { dg-do run } +// { dg-options "-std=gnu++11 -g" } + +// Copyright (C) 2012 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +template +void +placeholder(const T &s) +{ + std::cout << s; +} + +template +void +use(const T &p) +{ + placeholder(&p); +} + +struct deleter { void operator()(int*) {} }; + +std::shared_ptr make(uintptr_t p) +{ + return std::shared_ptr(reinterpret_cast(p), deleter()); +} + +int +main() +{ + typedef std::shared_ptr shared; + typedef std::weak_ptr weak; + + shared esp; +// { dg-final { note-test esp "std::shared_ptr (empty) 0x0" } } + weak ewp1; +// { dg-final { note-test ewp1 "std::weak_ptr (empty) 0x0" } } + weak ewp2 = esp; +// { dg-final { note-test ewp2 "std::weak_ptr (empty) 0x0" } } + + shared sp1 = make(0x12345678); + shared sp2 = sp1; +// { dg-final { note-test sp1 "std::shared_ptr (count 2, weak 0) 0x12345678" } } + + shared sp3 = make(0x12344321); + weak sp4 = sp3; + weak wp1 = sp3; +// { dg-final { note-test wp1 "std::weak_ptr (count 1, weak 2) 0x12344321" } } + + shared sp5 = make(0x56788765); + weak wp2 = sp5; + sp5.reset(); +// { dg-final { note-test wp2 "std::weak_ptr (expired, weak 1) 0x56788765" } } + + placeholder(""); // Mark SPOT + use(esp); + use(ewp1); + use(ewp2); + use(sp1); + use(wp1); + use(wp2); + + return 0; +} + +// { dg-final { gdb-test SPOT } }