1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2017-2023 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Test GDB's awareness of the wchar_t (C++98+) and char16_t, char32_t
19 # (C++11+) built-in types. We also run most tests here in C mode, and
20 # check whether the built-ins are disabled (gdb uses the typedefs in
21 # the debug info instead.)
25 # Test char16_t/char32_t/wchar_t in language LANG, against symbols in
26 # a program. Lang can be "c", "c++03" or "c++11". In C++11,
27 # char16_t/char32_t are built-in types, and the debug information
29 # http://wiki.dwarfstd.org/index.php?title=C%2B%2B0x:_New_string_literals).
31 proc wide_char_types_program {lang} {
32 global srcfile testfile
35 if {$lang == "c++03"} {
36 lappend options c++ additional_flags=-std=c++03
37 set out $testfile-cxx03
38 } elseif {$lang == "c++11"} {
39 lappend options c++ additional_flags=-std=c++11
40 set out $testfile-cxx11
45 if { [prepare_for_testing "failed to prepare" \
46 ${out} [list $srcfile] $options] } {
53 do_test_wide_char $lang "u16" "u32" "wchar"
56 # Test char16_t/char32_t/wchar_t in language LANG. Use CHAR16_EXP,
57 # CHAR32_EXP, and WCHAR_EXP as expression for each of the
58 # corresponding types. (E.g., CHAR16_EXP will be u16 when testing
59 # against the program, and "(char16_t)-1" when testing the built-in
60 # types without a program loaded.)
62 proc do_test_wide_char {lang char16_exp char32_exp wchar_exp} {
65 # Check that the fixed-width wide types are distinct built-in
66 # types in C++11+. In other modes, they're instead typedefs,
67 # found in the debug info.
68 if {$lang == "c++11"} {
69 gdb_test "ptype $char16_exp" "type = char16_t" \
70 "char16_t is distinct"
71 gdb_test "ptype $char32_exp" "type = char32_t" \
72 "char32_t is distinct"
74 gdb_test "ptype $char16_exp" "type = unsigned (long|int|short)" \
76 gdb_test "ptype $char32_exp" "type = unsigned (long|int|short)" \
80 # wchar_t is a disctinct built-in type in C++03+.
82 gdb_test "ptype $wchar_exp" "type = wchar_t" \
85 gdb_test "ptype $wchar_exp" "type = (unsigned )?(long|int|short)" \
89 # Check that the fixed-width wide char types are unsigned.
90 gdb_test "p $char16_exp" " = 65535 u'\\\\xffff'" \
91 "char16_t is unsigned"
92 gdb_test "p $char32_exp" " = 4294967295 U'\\\\xffffffff'" \
93 "char32_t is unsigned"
95 # Whether wchar_t is signed is implementation-dependent. While we
96 # ignore whether GDB got the ABI size/sign details right here,
97 # this at least verifies that the value isn't garbage, and that
98 # GDB correctly outputs the character using the "L" prefix.
99 set test "wchar_t sign"
100 gdb_test_multiple "p $wchar_exp" $test {
101 -re " = 4294967295 L'\\\\xffffffff'\r\n$gdb_prompt $" {
102 pass "$test (unsigned)"
104 -re " = 65535 L'\\\\xffff'\r\n$gdb_prompt $" {
105 pass "$test (unsigned)"
107 -re " = -1 L'\\\\xffffffff'\r\n$gdb_prompt $" {
108 pass "$test (signed)"
110 -re " = -1 L'\\\\xffff'\r\n$gdb_prompt $" {
111 pass "$test (signed)"
115 # Check sizeof. These are fixed-width.
116 gdb_test "p sizeof($char16_exp)" "= 2" \
117 "sizeof($char16_exp) == 2"
118 gdb_test "p sizeof($char32_exp)" "= 4" \
119 "sizeof(char16_t) == 4"
121 # Size of wchar_t depends on ABI.
122 gdb_test "p sizeof($wchar_exp)" "= (2|4)" \
125 # Test printing wide literal strings. Note that when testing with
126 # no program started, this relies on GDB's awareness of the
127 # built-in wide char types.
128 gdb_test {p U"hello"} {= U"hello"}
129 gdb_test {p u"hello"} {= u"hello"}
130 gdb_test {p L"hello"} {= L"hello"}
133 # Make sure that the char16_t/char32_t/wchar_t types are recognized as
134 # distinct built-in types in C++ mode, even with no program loaded.
135 # Check that in C mode, the types are not recognized.
137 proc wide_char_types_no_program {} {
138 global srcfile testfile
143 # These types are not built-in in C.
144 with_test_prefix "c" {
145 gdb_test "set language c"
147 gdb_test "p (char16_t) -1" "No symbol table is loaded.*" \
148 "char16_t is not built-in"
149 gdb_test "p (char32_t) -1" "No symbol table is loaded.*" \
150 "char32_t is not built-in"
152 gdb_test "p (wchar_t) -1" "No symbol table is loaded.*" \
153 "wchar_t is not built-in"
155 gdb_test {p U"hello"} "No type named char32_t\\\."
156 gdb_test {p u"hello"} "No type named char16_t\\\."
157 gdb_test {p L"hello"} "No type named wchar_t\\\."
160 # Note GDB does not distinguish C++ dialects, so the fixed-width
161 # types are always available in C++ mode, even if they were not
162 # built-in types before C++11.
163 with_test_prefix "c++" {
164 gdb_test "set language c++"
166 do_test_wide_char "c++11" "(char16_t) -1" "(char32_t) -1" "(wchar_t) -1"
170 # Check wide char types with no program loaded.
171 with_test_prefix "no program" {
172 wide_char_types_no_program
175 # Check types when a program is loaded.
176 with_test_prefix "with program" {
177 foreach_with_prefix lang {"c" "c++03" "c++11"} {
178 wide_char_types_program $lang