1# This testcase is part of GDB, the GNU debugger.
2
3# Copyright 2017-2020 Free Software Foundation, Inc.
4
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.
9#
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.
14#
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/>.
17
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.)
22
23standard_testfile
24
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
28# reflects that (see
29# http://wiki.dwarfstd.org/index.php?title=C%2B%2B0x:_New_string_literals).
30
31proc wide_char_types_program {lang} {
32    global srcfile testfile
33
34    set options {debug}
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
41    } else {
42	set out $testfile-c
43    }
44
45    if { [prepare_for_testing "failed to prepare" \
46	      ${out} [list $srcfile] $options] } {
47	return -1
48    }
49
50    if ![runto_main] then {
51	fail "can't run to main"
52	return 0
53    }
54    do_test_wide_char $lang "u16" "u32" "wchar"
55}
56
57# Test char16_t/char32_t/wchar_t in language LANG.  Use CHAR16_EXP,
58# CHAR32_EXP, and WCHAR_EXP as expression for each of the
59# corresponding types.  (E.g., CHAR16_EXP will be u16 when testing
60# against the program, and "(char16_t)-1" when testing the built-in
61# types without a program loaded.)
62
63proc do_test_wide_char {lang char16_exp char32_exp wchar_exp} {
64    global gdb_prompt
65
66    # Check that the fixed-width wide types are distinct built-in
67    # types in C++11+.  In other modes, they're instead typedefs,
68    # found in the debug info.
69    if {$lang == "c++11"} {
70	gdb_test "ptype $char16_exp" "type = char16_t" \
71	    "char16_t is distinct"
72	gdb_test "ptype $char32_exp" "type = char32_t" \
73	    "char32_t is distinct"
74    } else {
75	gdb_test "ptype $char16_exp" "type = unsigned (long|int|short)" \
76	    "char16_t is typedef"
77	gdb_test "ptype $char32_exp" "type = unsigned (long|int|short)" \
78	    "char32_t is typedef"
79    }
80
81    # wchar_t is a disctinct built-in type in C++03+.
82    if {$lang != "c"} {
83	gdb_test "ptype $wchar_exp" "type = wchar_t" \
84	    "wchar_t is distinct"
85    } else {
86	gdb_test "ptype $wchar_exp" "type = (unsigned )?(long|int|short)" \
87	    "wchar_t is typedef"
88    }
89
90    # Check that the fixed-width wide char types are unsigned.
91    gdb_test "p $char16_exp" " = 65535 u'\\\\xffff'" \
92	"char16_t is unsigned"
93    gdb_test "p $char32_exp" " = 4294967295 U'\\\\xffffffff'" \
94	"char32_t is unsigned"
95
96    # Whether wchar_t is signed is implementation-dependent.  While we
97    # ignore whether GDB got the ABI size/sign details right here,
98    # this at least verifies that the value isn't garbage, and that
99    # GDB correctly outputs the character using the "L" prefix.
100    set test "wchar_t sign"
101    gdb_test_multiple "p $wchar_exp" $test {
102	-re " = 4294967295 L'\\\\xffffffff'\r\n$gdb_prompt $" {
103	    pass "$test (unsigned)"
104	}
105	-re " = 65535 L'\\\\xffff'\r\n$gdb_prompt $" {
106	    pass "$test (unsigned)"
107	}
108	-re " = -1 L'\\\\xffffffff'\r\n$gdb_prompt $" {
109	    pass "$test (signed)"
110	}
111	-re " = -1 L'\\\\xffff'\r\n$gdb_prompt $" {
112	    pass "$test (signed)"
113	}
114    }
115
116    # Check sizeof.  These are fixed-width.
117    gdb_test "p sizeof($char16_exp)" "= 2" \
118	"sizeof($char16_exp) == 2"
119    gdb_test "p sizeof($char32_exp)" "= 4" \
120	"sizeof(char16_t) == 4"
121
122    # Size of wchar_t depends on ABI.
123    gdb_test "p sizeof($wchar_exp)" "= (2|4)" \
124	"sizeof(wchar_t)"
125
126    # Test printing wide literal strings.  Note that when testing with
127    # no program started, this relies on GDB's awareness of the
128    # built-in wide char types.
129    gdb_test {p U"hello"} {= U"hello"}
130    gdb_test {p u"hello"} {= u"hello"}
131    gdb_test {p L"hello"} {= L"hello"}
132}
133
134# Make sure that the char16_t/char32_t/wchar_t types are recognized as
135# distinct built-in types in C++ mode, even with no program loaded.
136# Check that in C mode, the types are not recognized.
137
138proc wide_char_types_no_program {} {
139    global srcfile testfile
140
141    gdb_exit
142    gdb_start
143
144    # These types are not built-in in C.
145    with_test_prefix "c" {
146	gdb_test "set language c"
147
148	gdb_test "p (char16_t) -1" "No symbol table is loaded.*" \
149	    "char16_t is not built-in"
150	gdb_test "p (char32_t) -1" "No symbol table is loaded.*" \
151	    "char32_t is not built-in"
152
153	gdb_test "p (wchar_t) -1" "No symbol table is loaded.*" \
154	    "wchar_t is not built-in"
155
156	gdb_test {p U"hello"} "No type named char32_t\\\."
157	gdb_test {p u"hello"} "No type named char16_t\\\."
158	gdb_test {p L"hello"} "No type named wchar_t\\\."
159    }
160
161    # Note GDB does not distinguish C++ dialects, so the fixed-width
162    # types are always available in C++ mode, even if they were not
163    # built-in types before C++11.
164    with_test_prefix "c++" {
165	gdb_test "set language c++"
166
167	do_test_wide_char "c++11" "(char16_t) -1" "(char32_t) -1" "(wchar_t) -1"
168    }
169}
170
171# Check wide char types with no program loaded.
172with_test_prefix "no program" {
173    wide_char_types_no_program
174}
175
176# Check types when a program is loaded.
177with_test_prefix "with program" {
178    foreach_with_prefix lang {"c" "c++03" "c++11"} {
179	wide_char_types_program $lang
180    }
181}
182