Fixup testcases outputting own name as a test name and standardize failed compilation...
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-type.exp
index b997c51d65e8de67a157d774a15c73f30b13410b..4a63f73df8c0fba760433ed488eb8dcf8b2253b1 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2009-2012 Free Software Foundation, Inc.
+# Copyright (C) 2009-2016 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
@@ -21,7 +21,7 @@ load_lib gdb-python.exp
 standard_testfile
 
 if [get_compiler_info c++] {
-    return -1;
+    return -1
 }
 
 # Build inferior to language specification.
@@ -29,9 +29,11 @@ proc build_inferior {exefile lang} {
   global srcdir subdir srcfile testfile hex
 
   if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${exefile}" executable "debug $lang"] != "" } {
-      untested "Couldn't compile ${srcfile} in $lang mode"
+      untested "failed to compile in $lang mode"
       return -1
   }
+
+  return 0
 }
 
 # Restart GDB.
@@ -56,131 +58,177 @@ proc runto_bp {bp} {
 }
 
 proc test_fields {lang} {
-  global gdb_prompt
+  with_test_prefix "test_fields" {
+    global gdb_prompt
 
-  # .fields() of a typedef should still return the underlying field list
-  gdb_test "python print len(gdb.parse_and_eval('ts').type.fields())" "2" \
-      "$lang typedef field list"
+    # .fields() of a typedef should still return the underlying field list
+    gdb_test "python print (len(gdb.parse_and_eval('ts').type.fields()))" "2" \
+        "$lang typedef field list"
 
-  if {$lang == "c++"} {
+    if {$lang == "c++"} {
       # Test usage with a class
-      gdb_py_test_silent_cmd "print c" "print value" 1
-      gdb_py_test_silent_cmd "python c = gdb.history (0)" "get value from history" 1
-      gdb_py_test_silent_cmd "python fields = c.type.fields()" "get fields" 1
-      gdb_test "python print len(fields)" "2" "Check number of fields"
-      gdb_test "python print fields\[0\].name" "c" "Check class field c name"
-      gdb_test "python print fields\[1\].name" "d" "Check class field d name"
-
-      gdb_test "python print c.type == gdb.parse_and_eval('d').type" "False"
-      gdb_test "python print c.type == gdb.parse_and_eval('d').type.fields()\[0\].type" \
+      gdb_py_test_silent_cmd "print (c)" "print value (c)" 1
+      gdb_py_test_silent_cmd "python c = gdb.history (0)" "get value (c) from history" 1
+      gdb_py_test_silent_cmd "python fields = c.type.fields()" "get fields from c.type" 1
+      gdb_test "python print (len(fields))" "2" "check number of fields (c)"
+      gdb_test "python print (fields\[0\].name)" "c" "check class field c name"
+      gdb_test "python print (fields\[1\].name)" "d" "check class field d name"
+
+      gdb_test "python print (c.type == gdb.parse_and_eval('d').type)" "False"
+      gdb_test "python print (c.type == gdb.parse_and_eval('d').type.fields()\[0\].type)" \
          "True"
-  }
 
-  # Test normal fields usage in structs.
-  gdb_py_test_silent_cmd "print st" "print value" 1
-  gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
-  gdb_py_test_silent_cmd "python fields = st.type.fields()" "get fields" 1
-  gdb_test "python print len(fields)" "2" "Check number of fields"
-  gdb_test "python print fields\[0\].name" "a" "Check structure field a name"
-  gdb_test "python print fields\[1\].name" "b" "Check structure field b name"
-
-  # Regression test for
-  # http://sourceware.org/bugzilla/show_bug.cgi?id=12070.
-  gdb_test "python print 'type' in dir(fields\[0\])" "True" \
-    "Check that dir includes name"
-
-  # Test Python mapping behavior of gdb.Type for structs/classes
-  gdb_test "python print len(st.type)" "2" "Check number of fields"
-  gdb_test "python print st.type\['a'\].name" "a" "Check fields lookup by name"
-    gdb_test "python print \[v.bitpos for v in st.type.itervalues()\]" {\[0L, 32L\]} "Check fields iteration over values"
-    gdb_test "python print \[(n, v.bitpos) for (n, v) in st.type.items()\]" {\[\('a', 0L\), \('b', 32L\)\]} "Check fields items list"
-  gdb_test "python print 'a' in st.type" "True" "Check field name exists test"
-  gdb_test "python print 'nosuch' in st.type" "False" "Check field name nonexists test"
-  gdb_test "python print not not st.type" "True" "Check conversion to bool"
-
-  # Test rejection of mapping operations on scalar types
-  gdb_test "python print len (st.type\['a'\].type)" "TypeError: Type is not a structure, union, or enum type.*"
-  gdb_test "python print st.type\['a'\].type.has_key ('x')" "TypeError: Type is not a structure, union, or enum type.*"
-  gdb_test "python print st.type\['a'\].type.keys ()" "TypeError: Type is not a structure, union, or enum type.*"
-  gdb_test "python print st.type\['a'\].type\['x'\]" "TypeError: Type is not a structure, union, or enum type.*"
-
-  # Test conversion to bool on scalar types
-  gdb_test "python print not not st.type\['a'\].type" "True"
+      # Test fields of a method (its parameters)
+      gdb_test "python print (len (gdb.parse_and_eval ('C::a_method').type.fields ()))" "3"
+      gdb_test "python print (gdb.parse_and_eval ('C::a_method').type.fields ()\[0\].type)" "C \\* const"
+      gdb_test "python print (gdb.parse_and_eval ('C::a_method').type.fields ()\[1\].type)" "int"
+      gdb_test "python print (gdb.parse_and_eval ('C::a_method').type.fields ()\[2\].type)" "char"
+
+      gdb_test "python print (len (gdb.parse_and_eval ('C::a_const_method').type.fields ()))" "3"
+      gdb_test "python print (gdb.parse_and_eval ('C::a_const_method').type.fields ()\[0\].type)" "const C \\* const"
+      gdb_test "python print (gdb.parse_and_eval ('C::a_const_method').type.fields ()\[1\].type)" "int"
+      gdb_test "python print (gdb.parse_and_eval ('C::a_const_method').type.fields ()\[2\].type)" "char"
+
+      gdb_test "python print (len (gdb.parse_and_eval ('C::a_static_method').type.fields ()))" "2"
+      gdb_test "python print (gdb.parse_and_eval ('C::a_static_method').type.fields ()\[0\].type)" "int"
+      gdb_test "python print (gdb.parse_and_eval ('C::a_static_method').type.fields ()\[1\].type)" "char"
+    }
+
+    # Test normal fields usage in structs.
+    gdb_py_test_silent_cmd "print (st)" "print value (st)" 1
+    gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value (st) from history" 1
+    gdb_py_test_silent_cmd "python fields = st.type.fields()" "get fields from st.type" 1
+    gdb_test "python print (len(fields))" "2" "check number of fields (st)"
+    gdb_test "python print (fields\[0\].name)" "a" "check structure field a name"
+    gdb_test "python print (fields\[1\].name)" "b" "check structure field b name"
+
+    # Test that unamed fields have 'None' for name.
+    gdb_py_test_silent_cmd "python ss = gdb.parse_and_eval('ss')" "init ss" 1
+    gdb_py_test_silent_cmd "python ss_fields = ss.type.fields()" \
+      "get fields from ss.type" 1
+    gdb_test "python print(len(ss_fields))" "2" "check length of ss_fields"
+    gdb_test "python print(ss_fields\[0\].name is None)" "True" \
+      "Check ss_fields\[0\].name"
+    gdb_test "python print(ss_fields\[1\].name is None)" "True" \
+      "Check ss_fields\[1\].name"
+    # Regression test for
+    # http://sourceware.org/bugzilla/show_bug.cgi?id=12070.
+    gdb_test "python print ('type' in dir(fields\[0\]))" "True" \
+      "Check that dir includes name"
+
+    # Test Python mapping behavior of gdb.Type for structs/classes
+    gdb_test "python print (len(st.type))" "2" "check number of fields (st.type)"
+    gdb_test "python print (st.type\['a'\].name)" "a" "check fields lookup by name"
+    gdb_test "python print (\[v.bitpos for v in st.type.itervalues()\])" {\[0L?, 32L?\]} "Check fields iteration over values"
+    gdb_test "python print (\[(n, v.bitpos) for (n, v) in st.type.items()\])" {\[\('a', 0L?\), \('b', 32L?\)\]} "Check fields items list"
+    gdb_test "python print ('a' in st.type)" "True" "check field name exists test"
+    gdb_test "python print ('nosuch' in st.type)" "False" "check field name nonexists test"
+    gdb_test "python print (not not st.type)" "True" "check conversion to bool"
+
+    # Test rejection of mapping operations on scalar types
+    gdb_test "python print (len (st.type\['a'\].type))" "TypeError: Type is not a structure, union, enum, or function type.*"
+    gdb_test "python print (st.type\['a'\].type.has_key ('x'))" "TypeError: Type is not a structure, union, enum, or function type.*"
+    gdb_test "python print (st.type\['a'\].type\['x'\])" "TypeError: Type is not a structure, union, enum, or function type.*"
+    gdb_test "python print (st.type\['a'\].type.keys ())" "TypeError: Type is not a structure, union, enum, or function type.*"
+
+    # Test conversion to bool on scalar types
+    gdb_test "python print (not not st.type\['a'\].type)" "True"
   
-  # Test regression PR python/10805
-  gdb_py_test_silent_cmd "print ar" "print value" 1
-  gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from  history" 1
-  gdb_test "python fields = ar.type.fields()"
-  gdb_test "python print len(fields)" "1" "Check the number of fields"
-  gdb_test "python print fields\[0\].type" "<range type>" "Check array field type"
-
-  # Test gdb.Type.array.
-  gdb_test "python print ar\[0\].cast(ar\[0\].type.array(1))" \
-      ".1, 2." "cast to array with one argument"
-  gdb_test "python print ar\[0\].cast(ar\[0\].type.array(0, 1))" \
-      ".1, 2." "cast to array with two arguments"
-
-  gdb_test "python print ar\[0\].type == ar\[0\].type" "True"
-
-  # Test gdb.Type.vector.
-  # Note: vectors cast differently than arrays.  Here ar[0] is replicated
-  # for the size of the vector.
-  gdb_py_test_silent_cmd \
-      "python vec1 = ar\[0\].cast(ar\[0\].type.vector(1))" "set vec1" 1
-  gdb_test "python print vec1" ".1, 1." "cast to vector with one argument"
-  gdb_py_test_silent_cmd \
-      "python vec2 = ar\[0\].cast(ar\[0\].type.vector(0, 1))" "set vec2" 1
-  gdb_test "python print vec2" ".1, 1." "cast to vector with two arguments"
-  gdb_test "python print vec1 == vec2" "True"
-  gdb_py_test_silent_cmd \
-      "python vec3 = ar\[1\].cast(ar\[1\].type.vector(1))" "set vec3" 1
-  gdb_test "python print vec1 == vec3" "False"
+    # Test regression PR python/10805
+    gdb_py_test_silent_cmd "print (ar)" "print value (ar)" 1
+    gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value (ar) from history" 1
+    gdb_test "python fields = ar.type.fields()"
+    gdb_test "python print (len(fields))" "1" "check the number of fields"
+    gdb_test "python print (fields\[0\].type)" "<range type>" "check array field type"
+
+    # Test gdb.Type.array.
+    gdb_test "python print (ar\[0\].cast(ar\[0\].type.array(1)))" \
+        ".1, 2." "cast to array with one argument"
+    gdb_test "python print (ar\[0\].cast(ar\[0\].type.array(0, 1)))" \
+        ".1, 2." "cast to array with two arguments"
+
+    gdb_test "python print (ar\[0\].type == ar\[0\].type)" "True"
+
+    # Test gdb.Type.vector.
+    # Note: vectors cast differently than arrays.  Here ar[0] is replicated
+    # for the size of the vector.
+    gdb_py_test_silent_cmd "print (vec_data_1)" "print value (vec_data_1)" 1
+    gdb_py_test_silent_cmd "python vec_data_1 = gdb.history (0)" "get value (vec_data_1) from history" 1
+
+    gdb_py_test_silent_cmd "print (vec_data_2)" "print value (vec_data_2)" 1
+    gdb_py_test_silent_cmd "python vec_data_2 = gdb.history (0)" "get value (vec_data_2) from history" 1
+
+    gdb_py_test_silent_cmd "python vec1 = vec_data_1.cast(ar\[0\].type.vector(1))" "set vec1" 1
+    gdb_test "python print (vec1)" ".1, 1." "cast to vector with one argument"
+    gdb_py_test_silent_cmd "python vec2 = vec_data_1.cast(ar\[0\].type.vector(0, 1))" "set vec2" 1
+    gdb_test "python print (vec2)" ".1, 1." "cast to vector with two arguments"
+    gdb_test "python print (vec1 == vec2)" "True"
+    gdb_py_test_silent_cmd "python vec3 = vec_data_2.cast(ar\[0\].type.vector(1))" "set vec3" 1
+    gdb_test "python print (vec1 == vec3)" "False"
+
+    # Test fields of a function (its parameters)
+    gdb_test "python print (len (gdb.parse_and_eval ('a_function').type.fields ()))" "2"
+    gdb_test "python print (gdb.parse_and_eval ('a_function').type.fields ()\[0\].type)" "int"
+    gdb_test "python print (gdb.parse_and_eval ('a_function').type.fields ()\[1\].type)" "char"
+  }
 }
 
 proc test_enums {} {
-  gdb_py_test_silent_cmd "print e" "print value" 1
-  gdb_py_test_silent_cmd "python e = gdb.history (0)" "get value from  history" 1
-  gdb_py_test_silent_cmd "python fields = e.type.fields()" "get value from history" 1
-  gdb_test "python print len(fields)" "3" "Check the number of enum fields"
-  gdb_test "python print fields\[0\].name" "v1" "Check enum field name"
-  gdb_test "python print fields\[1\].name" "v2" "Check enum field name"
-
-  # Ditto but by mapping operations
-  gdb_test "python print len(e.type)" "3" "Check the number of enum fields"
-  gdb_test "python print e.type\['v1'\].name" "v1" "Check enum field lookup by name"
-  gdb_test "python print e.type\['v3'\].name" "v3" "Check enum field lookup by name"
-    gdb_test "python print \[v.enumval for v in e.type.itervalues()\]" {\[0L, 1L, 2L\]} "Check num fields iteration over values"
-    gdb_test "python print \[(n, v.enumval) for (n, v) in e.type.items()\]" {\[\('v1', 0L\), \('v2', 1L\), \('v3', 2L\)\]} "Check enum fields items list"
+  with_test_prefix "test_enum" {
+    gdb_py_test_silent_cmd "print (e)" "print value (e)" 1
+    gdb_py_test_silent_cmd "python (e) = gdb.history (0)" "get value (e) from history" 1
+    gdb_py_test_silent_cmd "python fields = e.type.fields()" "extract type fields from e" 1
+    gdb_test "python print (len(fields))" "3" "check the number of enum fields"
+    gdb_test "python print (fields\[0\].name)" "v1" "check enum field\[0\] name"
+    gdb_test "python print (fields\[1\].name)" "v2" "check enum field\[1\]name"
+
+    # Ditto but by mapping operations
+    gdb_test "python print (len(e.type))" "3" "check the number of type fields"
+    gdb_test "python print (e.type\['v1'\].name)" "v1" "check enum field lookup by name (v1)"
+    gdb_test "python print (e.type\['v3'\].name)" "v3" "check enum field lookup by name (v3)"
+    gdb_test "python print (\[v.enumval for v in e.type.itervalues()\])" {\[0L?, 1L?, 2L?\]} "Check num fields iteration over values"
+    gdb_test "python print (\[(n, v.enumval) for (n, v) in e.type.items()\])" {\[\('v1', 0L?\), \('v2', 1L?\), \('v3', 2L?\)\]} "Check enum fields items list"
+  }
 }
+
 proc test_base_class {} {
-  gdb_py_test_silent_cmd "print d" "print value" 1
-  gdb_py_test_silent_cmd "python d = gdb.history (0)" "get value from  history" 1
-  gdb_py_test_silent_cmd "python fields = d.type.fields()" "get value from history" 1
-  gdb_test "python print len(fields)" "3" "Check the number of fields"
-  gdb_test "python print fields\[0\].is_base_class" "True" "Check base class"
-  gdb_test "python print fields\[1\].is_base_class" "False" "Check base class"
+  with_test_prefix "test_base_class" {
+    gdb_py_test_silent_cmd "print (d)" "print value (d)" 1
+    gdb_py_test_silent_cmd "python d = gdb.history (0)" "get value (d) from history" 1
+    gdb_py_test_silent_cmd "python fields = d.type.fields()" "extract type fields from d" 1
+    gdb_test "python print (len(fields))" "3" "check the number of fields"
+    gdb_test "python print (fields\[0\].is_base_class)" "True" "check base class (fields\[0\])"
+    gdb_test "python print (fields\[1\].is_base_class)" "False" "check base class (fields\[1\])"
+  }
 }
 
 proc test_range {} {
+  with_test_prefix "test_range" {
+    with_test_prefix "on ranged value" {
+      # Test a valid range request.
+      gdb_py_test_silent_cmd "print (ar)" "print value (ar)" 1
+      gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value (ar) from history" 1
+      gdb_test "python print (len(ar.type.range()))" "2" "check correct tuple length"
+      gdb_test "python print (ar.type.range()\[0\])" "0" "check range low bound"
+      gdb_test "python print (ar.type.range()\[1\])" "1" "check range high bound"
+    }
 
-  # Test a valid range request.
-  gdb_py_test_silent_cmd "print ar" "print value" 1
-  gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from history" 1
-  gdb_test "python print len(ar.type.range())" "2" "Check correct tuple length"
-  gdb_test "python print ar.type.range()\[0\]" "0" "Check low range"
-  gdb_test "python print ar.type.range()\[1\]" "1" "Check high range"
-
-  # Test a range request on a ranged type.
-  gdb_py_test_silent_cmd "print ar" "print value" 1
-  gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value from  history" 1
-  gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1
-  gdb_test "python print fields\[0\].type.range()\[0\]" "0" "Check range type low bound"
-  gdb_test "python print fields\[0\].type.range()\[1\]" "1" "Check range type high bound"
-
-  # Test where a range does not exist.
-  gdb_py_test_silent_cmd "print st" "print value" 1
-  gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value from history" 1
-  gdb_test "python print st.type.range()" "RuntimeError: This type does not have a range.*" "Check range for non ranged type."
+    with_test_prefix "on ranged type" {
+      # Test a range request on a ranged type.
+      gdb_py_test_silent_cmd "print (ar)" "print value (ar)" 1
+      gdb_py_test_silent_cmd "python ar = gdb.history (0)" "get value (ar) from history" 1
+      gdb_py_test_silent_cmd "python fields = ar.type.fields()" "get fields" 1
+      gdb_test "python print (fields\[0\].type.range()\[0\])" "0" "check range low bound"
+      gdb_test "python print (fields\[0\].type.range()\[1\])" "1" "check range high bound"
+    }
+
+    with_test_prefix "on unranged value" {
+      # Test where a range does not exist.
+      gdb_py_test_silent_cmd "print (st)" "print value (st)" 1
+      gdb_py_test_silent_cmd "python st = gdb.history (0)" "get value (st) from history" 1
+      gdb_test "python print (st.type.range())" "RuntimeError: This type does not have a range.*" "check range for non ranged type."
+    }
+  }
 }
 
 # Some tests of template arguments.
@@ -190,8 +238,8 @@ proc test_template {} {
        "get type of temvar" \
        1
 
-    gdb_test "python print ttype.template_argument(0)" "D"
-    gdb_test "python print isinstance(ttype.template_argument(0), gdb.Type)" \
+    gdb_test "python print (ttype.template_argument(0))" "D"
+    gdb_test "python print (isinstance(ttype.template_argument(0), gdb.Type))" \
        "True"
 
     # The next two tests require a GCC that emits DW_TAG_template_*.
@@ -202,35 +250,51 @@ proc test_template {} {
        set have_older_gcc 1
     }
     if $have_older_gcc { setup_xfail *-*-* }
-    gdb_test "python print ttype.template_argument(1)" "23"
+    gdb_test "python print (ttype.template_argument(1))" "23"
     if $have_older_gcc { setup_xfail *-*-* }
-    gdb_test "python print isinstance(ttype.template_argument(1), gdb.Value)" \
+    gdb_test "python print (isinstance(ttype.template_argument(1), gdb.Value))" \
        "True"
 
     if {[test_compiler_info {gcc-[0-3]-*}]
        || [test_compiler_info {gcc-4-[0-5]-*}]} {
        setup_xfail "gcc/46955" *-*-*
     }
-    gdb_test "python print ttype.template_argument(2)" "&C::c"
+    gdb_test "python print (ttype.template_argument(2))" "&C::c"
 }
 
 # Perform C Tests.
-build_inferior "${binfile}" "c"
-restart_gdb "${binfile}"
+if { [build_inferior "${binfile}" "c"] == 0 } {
+  restart_gdb "${binfile}"
 
-# Skip all tests if Python scripting is not enabled.
-if { [skip_python_tests] } { continue }
+  # Skip all tests if Python scripting is not enabled.
+  if { [skip_python_tests] } { continue }
+
+  gdb_test "python print(gdb.lookup_type('char').array(1, 0))" \
+      "char \\\[0\\\]"
+
+  gdb_test "python print(gdb.lookup_type('char').array(1, -1))" \
+      "Array length must not be negative.*"
+
+  gdb_test "python print(gdb.lookup_type('int').optimized_out())" \
+      "<optimized out>"
+
+  with_test_prefix "lang_c" {
+      runto_bp "break to inspect struct and array."
+      test_fields "c"
+      test_enums
+  }
+}
 
-runto_bp "break to inspect struct and array."
-test_fields "c"
-test_enums
 
 # Perform C++ Tests.
-build_inferior "${binfile}-cxx" "c++"
-restart_gdb "${binfile}-cxx"
-runto_bp "break to inspect struct and array."
-test_fields "c++"
-test_base_class
-test_range
-test_template
-test_enums
+if { [build_inferior "${binfile}-cxx" "c++"] == 0 } {
+  restart_gdb "${binfile}-cxx"
+  with_test_prefix "lang_cpp" {
+      runto_bp "break to inspect struct and array."
+      test_fields "c++"
+      test_base_class
+      test_range
+      test_template
+      test_enums
+  }
+}
This page took 0.031057 seconds and 4 git commands to generate.