Patchwork RFC: add a testsuite for libstdc++ pretty-printers

login
register
mail settings
Submitter Tom Tromey
Date Aug. 3, 2011, 3:33 p.m.
Message ID <m3bow633xp.fsf@fleche.redhat.com>
Download mbox | patch
Permalink /patch/108285/
State New
Headers show

Comments

Tom Tromey - Aug. 3, 2011, 3:33 p.m.
I'm finally getting back to this.

This patch adds test suite support for the libstdc++ pretty-printers.

These tests require a new gdb, so following Mike Stump's advice and the
consensus of the libstdc++ list, we now check for an appropriate gdb
feature before deciding whether to run the tests.

I tested this against gdb 7.2 (=> unsupported) and 7.3 (=> all tests
pass).

Let me know what you think.

Tom

2011-08-03  Tom Tromey  <tromey@redhat.com>

	* testsuite/libstdc++-prettyprinters/simple.cc: New file.
	* testsuite/lib/gdb-test.exp: New file.
	* testsuite/libstdc++-prettyprinters/prettyprinters.exp: New
	file.
Tom Tromey - Aug. 18, 2011, 5:28 p.m.
>>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes:

Tom> I'm finally getting back to this.
Tom> This patch adds test suite support for the libstdc++ pretty-printers.

Any comments on this?

I'd like to get it in; Phil found a bug in the std::tuple printer, and
it would be nice to put in a test case along with the fix.

Tom
Benjamin Kosnik - Aug. 24, 2011, 8:54 p.m.
> Any comments on this?
> 
> I'd like to get it in; Phil found a bug in the std::tuple printer, and
> it would be nice to put in a test case along with the fix.

Hey Tom (and Phil!).

Sorry for the delay: this looks fine. Please put it in on trunk and
enjoy your vacation!

-benjamin
Tom Tromey - Aug. 25, 2011, 2:31 p.m.
Tom> Any comments on this?

Tom> I'd like to get it in; Phil found a bug in the std::tuple printer, and
Tom> it would be nice to put in a test case along with the fix.

Benjamin> Hey Tom (and Phil!).

Benjamin> Sorry for the delay: this looks fine. Please put it in on trunk and
Benjamin> enjoy your vacation!

Thanks, I checked it in.  Let me know if there are problems.

Tom

Patch

Index: testsuite/libstdc++-prettyprinters/prettyprinters.exp
===================================================================
--- testsuite/libstdc++-prettyprinters/prettyprinters.exp	(revision 0)
+++ testsuite/libstdc++-prettyprinters/prettyprinters.exp	(revision 0)
@@ -0,0 +1,51 @@ 
+#   Copyright (C) 2011 Free Software Foundation, Inc.
+
+# This program 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 of the License, or
+# (at your option) any later version.
+# 
+# This program 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 program; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+load_lib gdb-test.exp
+
+dg-init
+v3-build_support
+
+global GDB
+if ![info exists ::env(GUALITY_GDB_NAME)] {
+    if [info exists GDB] {
+	set guality_gdb_name "$GDB"
+    } else {
+	set guality_gdb_name "[transform gdb]"
+    }
+    setenv GUALITY_GDB_NAME "$guality_gdb_name"
+}
+
+if {! [gdb_version_check]} {
+    unsupported "prettyprinters.exp"
+    return
+}
+
+# This can be used to keep the .exe around.  dg-test has an option for
+# this but there is no way to pass it through dg-runtest.
+global dg-interpreter-batch-mode
+set dg-interpreter-batch-mode 1
+
+global DEFAULT_CXXFLAGS
+global PCH_CXXFLAGS
+dg-runtest [lsort [glob $srcdir/$subdir/*.cc]] \
+  "" "$DEFAULT_CXXFLAGS $PCH_CXXFLAGS"
+
+if [info exists guality_gdb_name] {
+    unsetenv GUALITY_GDB_NAME
+}
+
+dg-finish
Index: testsuite/libstdc++-prettyprinters/simple.cc
===================================================================
--- testsuite/libstdc++-prettyprinters/simple.cc	(revision 0)
+++ testsuite/libstdc++-prettyprinters/simple.cc	(revision 0)
@@ -0,0 +1,87 @@ 
+// { dg-do run }
+// { dg-options "-g" }
+
+// Copyright (C) 2011 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
+// <http://www.gnu.org/licenses/>.
+
+#include <string>
+#include <deque>
+#include <bitset>
+#include <iostream>
+#include <list>
+#include <map>
+
+template<class T>
+void
+placeholder(const T &s)
+{
+  std::cout << s;
+}
+
+template<class T, class S>
+void
+placeholder(const std::pair<T,S> &s)
+{
+  std::cout << s.first;
+}
+
+template<class T>
+void
+use(const T &container)
+{
+  for (typename T::const_iterator i = container.begin();
+       i != container.end();
+       ++i)
+    placeholder(*i);
+}
+
+int
+main()
+{
+  std::string str = "zardoz";
+// { dg-final { note-test str "\"zardoz\"" } }
+
+  std::bitset<10> bs;
+  bs[0] = 1;
+  bs[5] = 1;
+  bs[7] = 1;
+// { dg-final { note-test bs {std::bitset = {[0] = 1, [5] = 1, [7] = 1}} } }
+
+  std::deque<std::string> deq;
+  deq.push_back("one");
+  deq.push_back("two");
+// { dg-final { note-test deq {std::deque with 2 elements = {"one", "two"}} } }
+
+  std::list<std::string> lst;
+  lst.push_back("one");
+  lst.push_back("two");
+// { dg-final { note-test lst {std::list = {[0] = "one", [1] = "two"}} } }
+
+  std::map<std::string, int> mp;
+  mp["zardoz"] = 23;
+// { dg-final { note-test mp {std::map with 1 elements = {["zardoz"] = 23}} } }
+
+  placeholder(str); // Mark SPOT
+  std::cout << bs;
+  use(deq);
+  use(lst);
+  use(mp);
+
+  return 0;
+}
+
+// { dg-final { gdb-test SPOT } }
Index: testsuite/lib/gdb-test.exp
===================================================================
--- testsuite/lib/gdb-test.exp	(revision 0)
+++ testsuite/lib/gdb-test.exp	(revision 0)
@@ -0,0 +1,198 @@ 
+#   Copyright (C) 2009, 2011 Free Software Foundation, Inc.
+
+# This program 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 of the License, or
+# (at your option) any later version.
+#
+# This program 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 GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+global gdb_tests
+set gdb_tests {}
+
+# Scan a file for markers and fill in the gdb_marker array for that
+# file.  Any error in this script is simply thrown; errors here are
+# programming errors in the test suite itself and should not be
+# caught.
+proc scan_gdb_markers {filename} {
+    global gdb_markers
+
+    if {[info exists gdb_markers($filename,-)]} {
+	return
+    }
+
+    set fd [open $filename]
+    set lineno 1
+    while {! [eof $fd]} {
+	set line [gets $fd]
+	if {[regexp -- "Mark (\[a-zA-Z0-9\]+)" $line ignore marker]} {
+	    set gdb_markers($filename,$marker) $lineno
+	}
+	incr lineno
+    }
+    close $fd
+
+    set gdb_markers($filename,-) {}
+}
+
+# Find a marker in a source file, and return the marker's line number.
+proc get_line_number {filename marker} {
+    global gdb_markers
+
+    scan_gdb_markers $filename
+    return $gdb_markers($filename,$marker)
+}
+
+# Make note of a gdb test.  A test consists of a variable name and an
+# expected result.
+proc note-test {var result} {
+    global gdb_tests
+
+    lappend gdb_tests $var $result
+}
+
+# Utility for testing variable values using gdb, invoked via dg-final.
+# Tests all tests indicated by note-test.
+#
+# Argument 0 is the marker on which to put a breakpoint
+# Argument 2 handles expected failures and the like
+proc gdb-test { marker {selector {}} } {
+    if { ![isnative] || [is_remote target] } { return }
+
+    if {[string length $selector] > 0} {
+	switch [dg-process-target $selector] {
+	    "S" { }
+	    "N" { return }
+	    "F" { setup_xfail "*-*-*" }
+	    "P" { }
+	}
+    }
+
+    # This assumes that we are three frames down from dg-test, and that
+    # it still stores the filename of the testcase in a local variable "name".
+    # A cleaner solution would require a new DejaGnu release.
+    upvar 2 name testcase
+    upvar 2 prog prog
+
+    set line [get_line_number $prog $marker]
+
+    set gdb_name $::env(GUALITY_GDB_NAME)
+    set testname "$testcase"
+    set output_file "[file rootname [file tail $prog]].exe"
+    set cmd_file "[file rootname [file tail $prog]].gdb"
+
+    global srcdir
+    set pycode [file join $srcdir .. python libstdcxx v6 printers.py]
+
+    global gdb_tests
+
+    set fd [open $cmd_file "w"]
+    puts $fd "source $pycode"
+    puts $fd "python register_libstdcxx_printers(None)"
+    puts $fd "break $line"
+    puts $fd "run"
+
+    set count 0
+    foreach {var result} $gdb_tests {
+	puts $fd "print $var"
+	incr count
+	set gdb_var($count) $var
+	set gdb_expected($count) $result
+    }
+    set gdb_tests {}
+
+    puts $fd "quit"
+    close $fd
+
+    send_log "Spawning: $gdb_name -nx -nw -quiet -batch -x $cmd_file ./$output_file\n"
+    set res [remote_spawn target "$gdb_name -nx -nw -quiet -batch -x $cmd_file ./$output_file"]
+    if { $res < 0 || $res == "" } {
+	unsupported "$testname"
+	return
+    }
+
+    remote_expect target [timeout_value] {
+	-re {^\$([0-9]+) = ([^\n\r]*)[\n\r]+} {
+	    set num $expect_out(1,string)
+	    set first $expect_out(2,string)
+	    if { ![string compare $first $gdb_expected($num)] } {
+		pass "$testname print $gdb_var($num)"
+	    } else {
+		fail "$testname print $gdb_var($num)"
+		verbose "     got =>$first<="
+		verbose "expected =>$gdb_expected($num)<="
+	    }
+
+	    if {$num == $count} {
+		remote_close target
+		return
+	    } else {
+		exp_continue
+	    }
+	}
+
+	-re {Python scripting is not supported in this copy of GDB.[\n\r]+} {
+	    unsupported "$testname"
+	    remote_close target
+	    return
+	}
+
+	-re {^[^$][^\n\r]*[\n\r]+} {
+	    verbose "skipping: $expect_out(buffer)"
+	    exp_continue
+	}
+
+	timeout {
+	    unsupported "$testname"
+	    remote_close target
+	    return
+	}
+    }
+
+    remote_close target
+    unsupported "$testname"
+    return
+}
+
+# Check for a new-enough version of gdb.  The pretty-printer tests
+# require gdb 7.3, but we don't want to test versions, so instead we
+# check for the python "lookup_global_symbol" method, which is in 7.3
+# but not earlier versions.
+# Return 1 if the version is ok, 0 otherwise.
+proc gdb_version_check {} {
+    global gdb_version
+
+    set gdb_name $::env(GUALITY_GDB_NAME)
+    set cmd "$gdb_name -nw -nx -quiet -batch -ex \"python print gdb.lookup_global_symbol\""
+    send_log "Spawning: $cmd\n"
+    set res [remote_spawn target "$cmd"]
+    if { $res < 0 || $res == "" } {
+	return 0
+    }
+
+    remote_expect target [timeout_value] {
+	-re "<built-in function lookup_global_symbol>" {
+	    return 1
+	}
+
+	-re {^[^\n\r]*[\n\r]+} {
+	    verbose "skipping: $expect_out(buffer)"
+	    exp_continue
+	}
+
+	timeout {
+	    remote_close target
+	    return 0
+	}
+    }
+
+    remote_close target
+    return 0
+}