daily update
[external/binutils.git] / gdb / testsuite / gdb.base / charset.exp
index 8e765aa..1ce275d 100644 (file)
@@ -1,21 +1,22 @@
-# Copyright 2001 Free Software Foundation, Inc.
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2001, 2004, 2007, 2008, 2009 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 2 of the License, or
+# 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; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # Please email any bugs, comments, and/or additions to this file to:
-# bug-gdb@prep.ai.mit.edu
+# bug-gdb@gnu.org
 
 # Test GDB's character set support.
 
@@ -30,7 +31,8 @@ set testfile "charset"
 set srcfile ${testfile}.c
 set binfile ${objdir}/${subdir}/${testfile}
 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
-     gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
+    untested "couldn't compile ${srcdir}/${subdir}/${srcfile}"
+    return -1
 }
 
 # Start with a fresh gdb.
@@ -45,14 +47,20 @@ proc parse_show_charset_output {testname} {
     global gdb_prompt
 
     gdb_expect {
-        -re "The current host and target character set is `(.*)'\\.\[\r\n\]+$gdb_prompt $" {
+        -re "The host character set is \"(.*)\"\\.\[\r\n\]+The target character set is \"(.*)\"\\.\[\r\n\]+The target wide character set is \"(.*)\"\\.\[\r\n\]+$gdb_prompt $" {
             set host_charset $expect_out(1,string)
-            set target_charset $expect_out(1,string)
+            set target_charset $expect_out(2,string)
+           set retlist [list $host_charset $target_charset]
             pass $testname
         }
-        -re "The current host character set is `(.*)'\\.\[\r\n\]+The current target character set is `(.*)'\\.\[\r\n\]+$gdb_prompt $" {
+        -re "The host character set is \"(.*)\"\\.\[\r\n\]+$gdb_prompt $" {
             set host_charset $expect_out(1,string)
-            set target_charset $expect_out(2,string)
+           set retlist [list $host_charset]
+            pass $testname
+        }
+        -re "The target character set is \"(.*)\"\\.\[\r\n\]+$gdb_prompt $" {
+            set target_charset $expect_out(1,string)
+           set retlist [list $target_charset]
             pass $testname
         }
         -re ".*$gdb_prompt $" {
@@ -63,44 +71,49 @@ proc parse_show_charset_output {testname} {
         }
     }
 
-    return [list $host_charset $target_charset]
+    return $retlist
 }
 
 
-# Try the various `show charset' commands.  These are all aliases of each
-# other; `show target-charset' and `show host-charset' actually print
-# both the host and target charsets.
+# Try the various `show charset' commands.
 
 send_gdb "show charset\n"
 set show_charset [parse_show_charset_output "show charset"]
 
 send_gdb "show target-charset\n"
-set show_target_charset [parse_show_charset_output "show target-charset"]
+set show_target_charset \
+  [lindex [parse_show_charset_output "show target-charset"] 0]
 
-if {! [string compare $show_charset $show_target_charset]} {
+if {[lsearch -exact $show_charset $show_target_charset] >= 0} {
     pass "check `show target-charset' against `show charset'"
 } else {
     fail "check `show target-charset' against `show charset'"
 }
 
 send_gdb "show host-charset\n"
-set show_host_charset [parse_show_charset_output "show host-charset"]
+set show_host_charset \
+  [lindex [parse_show_charset_output "show host-charset"] 0]
 
-if {! [string compare $show_charset $show_host_charset]} {
+if {[lsearch -exact $show_charset $show_host_charset] >= 0} {
     pass "check `show host-charset' against `show charset'"
 } else {
     fail "check `show host-charset' against `show charset'"
 }
 
+# Try a malformed `set charset'.
+gdb_test "set charset" \
+         "Requires an argument. Valid arguments are.*" \
+         "try malformed `set charset'"
 
-# Get the list of supported charsets.
-send_gdb "set charset\n"
-
-# True iff we've seen the "Valid character sets are:" message.
-set seen_valid 0
+# Try using `set host-charset' on an invalid character set.
+gdb_test "set host-charset my_grandma_bonnie" \
+         "Undefined item: \"my_grandma_bonnie\"." \
+         "try `set host-charset' with invalid charset"
 
-# True iff we've seen the "can be used as a host character set" message.
-set seen_can_host 0
+# Try using `set target-charset' on an invalid character set.
+gdb_test "set target-charset my_grandma_bonnie" \
+         "Undefined item: \"my_grandma_bonnie\"." \
+         "try `set target-charset' with invalid charset"
 
 # A Tcl array mapping the names of all the character sets we've seen
 # to "1" if the character set can be used as a host character set, or
@@ -113,76 +126,89 @@ proc all_charset_names {} {
     return [array names charsets]
 }
 
-proc charset_exists {charset} {
+proc valid_host_charset {charset} {
     global charsets
-    return [info exists charsets($charset)]
+    return [expr {[info exists charsets($charset)] && $charsets($charset)}]
 }
 
-proc valid_host_charset {charset} {
+proc valid_target_charset {charset} {
     global charsets
-    return $charsets($charset)
+    return [info exists charsets($charset)]
 }
 
+send_gdb "set host-charset\n"
 gdb_expect {
-    -re "Valid character sets are:\[\r\n\]+" {
-        # There's no ^ at the beginning of the pattern above, so that
-        # expect can skip the echoed `set charset' command.
-        set seen_valid 1
-        exp_continue
+    -re "Requires an argument. Valid arguments are (\[^ \t\n\r,.\]*)" {
+       #set host_charset_list $expect_out(1,string)
+       set charsets($expect_out(1,string)) 1
+       exp_continue
+       #pass "capture valid host charsets"
     }
-    -re "^  (\[^ \t\n\]*) \\*\[\r\n\]+" {
-        set charsets($expect_out(1,string)) 1
-        exp_continue
-    }
-    -re "^  (\[^ \t\n\]*)\[ \t\]*\[\r\n\]+" {
-        set charsets($expect_out(1,string)) 0
-        exp_continue
+
+    -re ", (\[^ \t\n\r,.\]*)" {
+       #set host_charset_list $expect_out(1,string)
+       set charsets($expect_out(1,string)) 1
+       exp_continue
+       #pass "capture valid host charsets"
     }
-    -re "^\\* - can be used as a host character set\[\r\n\]+" {
-        set seen_can_host 1
-        exp_continue
+
+    -re "\\.\r\n$gdb_prompt $" {
+       #set host_charset_list $expect_out(1,string)
+       pass "capture valid host charsets"
     }
-    -re ".*${gdb_prompt} $" {
-        # We don't do an exp_continue here.
+
+    -re ".*$gdb_prompt $" {
+       fail "capture valid host charsets"
     }
     timeout {
-        fail "get valid character sets (timeout)"
+       fail "(timeout) capture valid host charsets"
     }
 }
 
+# If gdb was built with a phony iconv, it will only have two character
+# sets: "auto" and the default.  In this situation, this set of tests
+# is pointless.
+if {[llength [array names charsets]] < 3} {
+    untested charset.exp
+    return -1
+}
 
-# Check that we've seen all the right pieces of the output, and that
-# we can at least use ASCII as a host character set.
-if {$seen_valid && $seen_can_host && [charset_exists ascii]} {
-    # We can't do the below as part of the test above, since all the
-    # [] substitution takes place before any expression evaluation
-    # takes place; && doesn't really short circuit things the way
-    # you'd like.  We'd get an "can't read $charsets(ascii)" error
-    # even when `info exists' had returned zero.
-    if {[valid_host_charset ascii]} {
-        pass "get valid character sets"
-    } else {
-        fail "get valid character sets"
+send_gdb "set target-charset\n"
+gdb_expect {
+    -re "Requires an argument. Valid arguments are (\[^ \t\n\r,.\]*)" {
+       set target_charset $expect_out(1,string)
+       if {! [info exists charsets($target_charset)]} {
+           set charsets($target_charset) 0
+       }
+       exp_continue
     }
-} else {
-    fail "get valid character sets (no ascii charset)"
-}
 
+    -re ", (\[^ \t\n\r,.\]*)" {
+       set target_charset $expect_out(1,string)
+       if {! [info exists charsets($target_charset)]} {
+           set charsets($target_charset) 0
+       }
+       exp_continue
+    }
 
-# Try using `set host-charset' on an invalid character set.
-gdb_test "set host-charset my_grandma_bonnie" \
-         "GDB doesn't know of any character set named `my_grandma_bonnie'." \
-         "try `set host-charset' with invalid charset"
+    -re "\\.\r\n$gdb_prompt $" {
+       pass "capture valid target charsets"
 
+    }
 
-# Try using `set target-charset' on an invalid character set.
-gdb_test "set target-charset my_grandma_bonnie" \
-         "GDB doesn't know of any character set named `my_grandma_bonnie'." \
-         "try `set target-charset' with invalid charset"
+    -re ".*$gdb_prompt $" {
+       fail "capture valid target charsets"
+    }
 
+    timeout {
+       fail "(timeout) capture valid target charsets"
+    }
+}
 
-# Make sure that GDB supports every host/target charset combination.
-foreach host_charset [all_charset_names] {
+# We don't want to test all the charset names here, since that would
+# be too many combinations.  We we pick a subset.
+set charset_subset {ASCII ISO-8859-1 EBCDIC-US IBM1047}
+foreach host_charset $charset_subset {
     if {[valid_host_charset $host_charset]} {
 
         set testname "try `set host-charset $host_charset'"
@@ -216,7 +242,10 @@ foreach host_charset [all_charset_names] {
 
         # Now try setting every possible target character set,
         # given that host charset.
-        foreach target_charset [all_charset_names] {
+        foreach target_charset $charset_subset {
+           if {![valid_target_charset $target_charset]} {
+               continue
+           }
             set testname "try `set target-charset $target_charset'"
             send_gdb "set target-charset $target_charset\n"
             gdb_expect {
@@ -324,7 +353,7 @@ foreach host_charset [all_charset_names] {
 # some strings in various target character sets.  We need to run the
 # test program to the point at which the strings have been
 # initialized.
-gdb_test "break [gdb_get_line_number "all strings initialized"]" \
+gdb_test "break ${srcfile}:[gdb_get_line_number "all strings initialized"]" \
          ".*Breakpoint.* at .*" \
          "set breakpoint after all strings have been initialized"
 gdb_run_cmd
@@ -341,23 +370,46 @@ gdb_expect {
 }
 
 
-gdb_test "set host-charset ascii" ""
-foreach target_charset [all_charset_names] {
-    send_gdb "set target-charset $target_charset\n" 
+# We only try the wide character tests on machines where the wchar_t
+# typedef in the test case has the right size.
+set wchar_size [get_sizeof wchar_t 99]
+set wchar_ok 0
+if {$wchar_size == 2} {
+    lappend charset_subset UCS-2
+    set wchar_ok 1
+} elseif {$wchar_size == 4} {
+    lappend charset_subset UCS-4
+    set wchar_ok 1
+}
+
+gdb_test "set host-charset ASCII" ""
+foreach target_charset $charset_subset {
+    if {![valid_target_charset $target_charset]} {
+       continue
+    }
+
+    if {$target_charset == "UCS-4" || $target_charset == "UCS-2"} {
+       set param target-wide-charset
+       set L L
+    } else {
+       set param target-charset
+       set L ""
+    }
+    send_gdb "set $param $target_charset\n" 
     gdb_expect {
         -re "$gdb_prompt $" {
-            pass "set target-charset $target_charset"
+            pass "set $param $target_charset"
         }
         timeout {
-            fail "set target-charset $target_charset (timeout)"
+            fail "set $param $target_charset (timeout)"
         }
     }
 
     # Try printing the null character.  There seems to be a bug in
     # gdb_test that requires us to use gdb_expect here.
-    send_gdb "print '\\0'\n"
+    send_gdb "print $L'\\0'\n"
     gdb_expect {
-        -re "\\\$${decimal} = 0 '\\\\0'\[\r\n\]+$gdb_prompt $" {
+        -re "\\\$${decimal} = 0 $L'\\\\0'\[\r\n\]+$gdb_prompt $" {
             pass "print the null character in ${target_charset}"
         }
         -re "$gdb_prompt $" {
@@ -372,21 +424,27 @@ foreach target_charset [all_charset_names] {
     # a string in $target_charset.  The variable's name is the
     # character set's name, in lower-case, with all non-identifier
     # characters replaced with '_', with "_string" stuck on the end.
-    set var_name [string tolower "${target_charset}_string"]
-    regsub -all -- "\[^a-z0-9_\]" $var_name "_" var_name
+    if {$target_charset == "UCS-2"} {
+       # We still use the ucs_4_string variable -- but the size is
+       # correct for UCS-2.
+       set var_name ucs_4_string
+    } else {
+       set var_name [string tolower "${target_charset}_string"]
+       regsub -all -- "\[^a-z0-9_\]" $var_name "_" var_name
+    }
     
     # Compute a regexp matching the results we expect.  This is static,
     # but it's easier than writing it out.
-    regsub -all "." "abefnrtv" "(\\\\&|x)" escapes
+    regsub -all "." "abfnrtv" "(\\\\&|x)" escapes
     set uppercase "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     set lowercase "abcdefghijklmnopqrstuvwxyz"
     set digits "0123456789"
-    set octal_escape "\\\\\[0-9\]\[0-9\]\[0-9\]"
+    set octal_escape "\\\\\[0-9\]+"
 
     send_gdb "print $var_name\n"
     # ${escapes}${uppercase}${lowercase}${digits}${octal}${octal}
     gdb_expect {
-        -re ".* = \"(\\\\a|x)(\\\\b|x)(\\\\e|x)(\\\\f|x)(\\\\n|x)(\\\\r|x)(\\\\t|x)(\\\\v|x)${uppercase}${lowercase}${digits}(\\\\\[0-9\]\[0-9\]\[0-9\]|x)(\\\\\[0-9\]\[0-9\]\[0-9\]|x).*\"\[\r\n\]+$gdb_prompt $" {
+        -re ".* = $L\"(\\\\a|x)(\\\\b|x)(\\\\f|x)(\\\\n|x)(\\\\r|x)(\\\\t|x)(\\\\v|x)${uppercase}${lowercase}${digits}(${octal_escape}|x)+\"\[\r\n\]+$gdb_prompt $" {
             pass "print string in $target_charset"
         }
         -re "$gdb_prompt $" {
@@ -398,29 +456,29 @@ foreach target_charset [all_charset_names] {
     }
 
     # Try entering a character literal, and see if it comes back unchanged.
-    gdb_test "print 'A'" \
-             " = \[0-9-\]+ 'A'" \
+    gdb_test "print $L'A'" \
+             " = \[0-9-\]+ $L'A'" \
              "parse character literal in ${target_charset}"
 
     # Check that the character literal was encoded correctly.
-    gdb_test "print 'A' == $var_name\[8\]" \
+    gdb_test "print $L'A' == $var_name\[7\]" \
              " = 1" \
              "check value of parsed character literal in ${target_charset}"
 
     # Try entering a string literal, and see if it comes back unchanged.
-    gdb_test "print \"abcdefABCDEF012345\"" \
-             " = \"abcdefABCDEF012345\"" \
+    gdb_test "print $L\"abcdefABCDEF012345\"" \
+             " = $L\"abcdefABCDEF012345\"" \
              "parse string literal in ${target_charset}"
 
     # Check that the string literal was encoded correctly.
-    gdb_test "print \"q\"\[0\] == $var_name\[50\]" \
+    gdb_test "print $L\"q\"\[0\] == $var_name\[49\]" \
              " = 1" \
              "check value of parsed string literal in ${target_charset}"
 
     # Test handling of characters in the target charset which
     # can't be translated into the host charset.
     if {! [string compare $target_charset iso-8859-1]} {
-        gdb_test "print iso_8859_1_string\[70\]" \
+        gdb_test "print iso_8859_1_string\[69\]" \
                  " = \[0-9-\]+ '\\\\242'" \
                  "print character with no equivalent in host character set"
         gdb_test "print iso_8859_1_string + 70" \
@@ -431,7 +489,7 @@ foreach target_charset [all_charset_names] {
     # Make sure that we don't apply the ISO-8859-1 `print_literally'
     # function to ASCII.
     if {! [string compare $target_charset ascii]} {
-        gdb_test "print iso_8859_1_string\[70\]" \
+        gdb_test "print iso_8859_1_string\[69\]" \
                  " = \[0-9-\]+ '\\\\242'" \
                  "print ASCII unprintable character"
         gdb_test "print iso_8859_1_string + 70" \
@@ -440,13 +498,13 @@ foreach target_charset [all_charset_names] {
     }
 
     # Try printing characters with backslash escape equivalents.
-    set escapees {a b f n r t v}
+    set escapees {a b f n r t v}
     for {set i 0} {$i < [llength $escapees]} {incr i} {
         set escape [lindex $escapees $i]
         send_gdb "print $var_name\[$i\]\n"
         set have_escape 1
         gdb_expect {
-            -re "= \[0-9-\]+ '\\\\${escape}'\[\r\n\]+$gdb_prompt $" {
+            -re "= \[0-9-\]+ $L'\\\\${escape}'\[\r\n\]+$gdb_prompt $" {
                 pass "try printing '\\${escape}' in ${target_charset}"
             }
             -re "= \[0-9-\]+ 'x'\[\r\n\]+$gdb_prompt $" {
@@ -464,12 +522,12 @@ foreach target_charset [all_charset_names] {
         if {$have_escape} {
 
             # Try parsing a backslash escape in a character literal.
-            gdb_test "print '\\${escape}' == $var_name\[$i\]" \
+            gdb_test "print $L'\\${escape}' == $var_name\[$i\]" \
                      " = 1" \
                      "check value of '\\${escape}' in ${target_charset}"
 
             # Try parsing a backslash escape in a string literal.
-            gdb_test "print \"\\${escape}\"\[0\] == $var_name\[$i\]" \
+            gdb_test "print $L\"\\${escape}\"\[0\] == $var_name\[$i\]" \
                      " = 1" \
                      "check value of \"\\${escape}\" in ${target_charset}"
         }
@@ -477,10 +535,73 @@ foreach target_charset [all_charset_names] {
 
     # Try printing a character escape that doesn't exist.  We should 
     # get the unescaped character, in the target character set.
-    gdb_test "print '\\q'" " = \[0-9-\]+ 'q'" \
+    gdb_test "print $L'\\q'" " = \[0-9-\]+ $L'q'" \
              "print escape that doesn't exist in $target_charset"
-    gdb_test "print '\\q' == $var_name\[50\]" " = 1" \
+    gdb_test "print $L'\\q' == $var_name\[49\]" " = 1" \
              "check value of escape that doesn't exist in $target_charset"
 }
 
+# Reset the target charset.
+gdb_test "set target-charset UTF-8" ""
+
+# \242 is not a valid UTF-8 character.
+gdb_test "print \"\\242\"" " = \"\\\\242\"" \
+  "non-representable target character"
+
+gdb_test "print '\\x'" "\\\\x escape without a following hex digit."
+gdb_test "print '\\u'" "\\\\u escape without a following hex digit."
+gdb_test "print '\\9'" " = \[0-9\]+ '9'"
+
+# Tests for wide- or unicode- strings.  L is the prefix letter to use,
+# either "L" (for wide strings), "u" (for UCS-2), or "U" (for UCS-4).
+# NAME is used in the test names and should be related to the prefix
+# letter in some easy-to-undestand way.
+proc test_wide_or_unicode {L name} {
+    gdb_test "print $L\"ab\" $L\"c\"" " = $L\"abc\"" \
+      "basic $name string concatenation"
+    gdb_test "print $L\"ab\" \"c\"" " = $L\"abc\"" \
+      "narrow and $name string concatenation"
+    gdb_test "print \"ab\" $L\"c\"" " = $L\"abc\"" \
+      "$name and narrow string concatenation"
+    gdb_test "print $L\"\\xe\" $L\"c\"" " = $L\"\\\\16c\"" \
+      "$name string concatenation with escape"
+    gdb_test "print $L\"\" \"abcdef\" \"g\"" \
+      "$L\"abcdefg\"" \
+      "concatenate three strings with empty $name string"
+
+    gdb_test "print $L'a'" "= \[0-9\]+ $L'a'" \
+      "basic $name character"
+}
+
+if {$wchar_ok} {
+    test_wide_or_unicode L wide
+}
+
+set ucs2_ok [expr {[get_sizeof char16_t 99] == 2}]
+if {$ucs2_ok} {
+    test_wide_or_unicode u UCS-2
+}
+
+set ucs4_ok [expr {[get_sizeof char32_t 99] == 4}]
+if {$ucs4_ok} {
+    test_wide_or_unicode U UCS-4
+}
+
+# Test an invalid string combination.
+proc test_combination {L1 name1 L2 name2} {
+    gdb_test "print $L1\"abc\" $L2\"def\"" \
+      "Undefined string concatenation." \
+      "undefined concatenation of $name1 and $name2"
+}
+
+if {$wchar_ok && $ucs2_ok} {
+    test_combination L wide u UCS-2
+}
+if {$wchar_ok && $ucs4_ok} {
+    test_combination L wide U UCS-4
+}
+if {$ucs2_ok && $ucs4_ok} {
+    test_combination u UCS-2 U UCS-4
+}
+
 gdb_exit