Skip to content

Commit 53e710a

Browse files
committed
Fix PR c++/21323: GDB thinks char16_t and char32_t are signed in C++
While the C++ standard says that char16_t and char32_t are unsigned types: Types char16_t and char32_t denote distinct types with the same size, signedness, and alignment as uint_least16_t and uint_least32_t, respectively, in <cstdint>, called the underlying types. ... gdb treats them as signed currently: (gdb) p (char16_t)-1 $1 = -1 u'\xffff' There are actually two places in gdb that hardcode these types: - gdbtypes.c:gdbtypes_post_init, when creating the built-in types, seemingly used by the "x /s" command (judging from commit 9a22f0d). - dwarf2read.c, when reading base types with DW_ATE_UTF encoding (which is what is used for these types, when compiling for C++11 and up). Despite the comment, the type created does end up used. Both places need fixing. But since I couldn't tell why dwarf2read.c needs to create a new type, I've made it use the per-arch built-in types instead, so that the types are only created once per arch instead of once per objfile. That seems to work fine. While writting the test, I noticed that the C++ language parser isn't actually aware of these built-in types, so if you try to use them without a program that uses them, you get: (gdb) set language c++ (gdb) ptype char16_t No symbol table is loaded. Use the "file" command. (gdb) ptype u"hello" No type named char16_t. (gdb) p u"hello" No type named char16_t. That's fixed by simply adding a couple entries to C++'s built-in types array in c-lang.c. With that, we get the expected: (gdb) ptype char16_t type = char16_t (gdb) ptype u"hello" type = char16_t [6] (gdb) p u"hello" $1 = u"hello" gdb/ChangeLog: 2017-04-12 Pedro Alves <[email protected]> PR c++/21323 * c-lang.c (cplus_primitive_types) <cplus_primitive_type_char16_t, cplus_primitive_type_char32_t>: New enum values. (cplus_language_arch_info): Register cplus_primitive_type_char16_t and cplus_primitive_type_char32_t. * dwarf2read.c (read_base_type) <DW_ATE_UTF>: If bit size is 16 or 32, use the archtecture's built-in type for char16_t and char32_t, respectively. Otherwise, fallback to init_integer_type as before, but make the type unsigned, and issue a complaint. * gdbtypes.c (gdbtypes_post_init): Make char16_t and char32_t unsigned. gdb/testsuite/ChangeLog: 2017-04-12 Pedro Alves <[email protected]> PR c++/21323 * gdb.cp/wide_char_types.c: New file. * gdb.cp/wide_char_types.exp: New file.
1 parent 5e0e042 commit 53e710a

File tree

7 files changed

+214
-6
lines changed

7 files changed

+214
-6
lines changed

gdb/ChangeLog

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
2017-04-12 Pedro Alves <[email protected]>
2+
3+
PR c++/21323
4+
* c-lang.c (cplus_primitive_types) <cplus_primitive_type_char16_t,
5+
cplus_primitive_type_char32_t>: New enum values.
6+
(cplus_language_arch_info): Register cplus_primitive_type_char16_t
7+
and cplus_primitive_type_char32_t.
8+
* dwarf2read.c (read_base_type) <DW_ATE_UTF>: If bit size is 16 or
9+
32, use the archtecture's built-in type for char16_t and char32_t,
10+
respectively. Otherwise, fallback to init_integer_type as before,
11+
but make the type unsigned, and issue a complaint.
12+
* gdbtypes.c (gdbtypes_post_init): Make char16_t and char32_t unsigned.
13+
114
2017-04-12 Alan Hayward <[email protected]>
215

316
* m32r-tdep.c (M32R_ARG_REGISTER_SIZE): Added.

gdb/c-lang.c

+6
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,8 @@ enum cplus_primitive_types {
895895
cplus_primitive_type_decfloat,
896896
cplus_primitive_type_decdouble,
897897
cplus_primitive_type_declong,
898+
cplus_primitive_type_char16_t,
899+
cplus_primitive_type_char32_t,
898900
nr_cplus_primitive_types
899901
};
900902

@@ -950,6 +952,10 @@ cplus_language_arch_info (struct gdbarch *gdbarch,
950952
= builtin->builtin_decdouble;
951953
lai->primitive_type_vector [cplus_primitive_type_declong]
952954
= builtin->builtin_declong;
955+
lai->primitive_type_vector [cplus_primitive_type_char16_t]
956+
= builtin->builtin_char16;
957+
lai->primitive_type_vector [cplus_primitive_type_char32_t]
958+
= builtin->builtin_char32;
953959

954960
lai->bool_type_symbol = "bool";
955961
lai->bool_type_default = builtin->builtin_bool;

gdb/dwarf2read.c

+16-3
Original file line numberDiff line numberDiff line change
@@ -15125,9 +15125,22 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
1512515125
type = init_integer_type (objfile, bits, 1, name);
1512615126
break;
1512715127
case DW_ATE_UTF:
15128-
/* We just treat this as an integer and then recognize the
15129-
type by name elsewhere. */
15130-
type = init_integer_type (objfile, bits, 0, name);
15128+
{
15129+
gdbarch *arch = get_objfile_arch (objfile);
15130+
15131+
if (bits == 16)
15132+
type = builtin_type (arch)->builtin_char16;
15133+
else if (bits == 32)
15134+
type = builtin_type (arch)->builtin_char32;
15135+
else
15136+
{
15137+
complaint (&symfile_complaints,
15138+
_("unsupported DW_ATE_UTF bit size: '%d'"),
15139+
bits);
15140+
type = init_integer_type (objfile, bits, 1, name);
15141+
}
15142+
return set_die_type (die, type, cu);
15143+
}
1513115144
break;
1513215145

1513315146
default:

gdb/gdbtypes.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -5204,10 +5204,9 @@ gdbtypes_post_init (struct gdbarch *gdbarch)
52045204

52055205
/* Wide character types. */
52065206
builtin_type->builtin_char16
5207-
= arch_integer_type (gdbarch, 16, 0, "char16_t");
5207+
= arch_integer_type (gdbarch, 16, 1, "char16_t");
52085208
builtin_type->builtin_char32
5209-
= arch_integer_type (gdbarch, 32, 0, "char32_t");
5210-
5209+
= arch_integer_type (gdbarch, 32, 1, "char32_t");
52115210

52125211
/* Default data/code pointer types. */
52135212
builtin_type->builtin_data_ptr

gdb/testsuite/ChangeLog

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2017-04-12 Pedro Alves <[email protected]>
2+
3+
PR c++/21323
4+
* gdb.cp/wide_char_types.c: New file.
5+
* gdb.cp/wide_char_types.exp: New file.
6+
17
2017-04-05 Sergio Durigan Junior <[email protected]>
28

39
PR gdb/21352
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* This testcase is part of GDB, the GNU debugger.
2+
3+
Copyright 2017 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+
19+
#include <uchar.h>
20+
21+
char16_t u16 = -1;
22+
char32_t u32 = -1;
23+
24+
int
25+
main ()
26+
{
27+
return 0;
28+
}
+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# This testcase is part of GDB, the GNU debugger.
2+
3+
# Copyright 2017 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 char16_t, char32_t (C++11+) built-in
19+
# types. We also run most tests here in C mode, and check whether the
20+
# built-ins are disabled (gdb uses the typedefs in the debug info
21+
# instead.)
22+
23+
standard_testfile
24+
25+
# Test char16_t/char32_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+
31+
proc 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"
55+
}
56+
57+
# Test char16_t/char32_t in language LANG. Use CHAR16_EXP and
58+
# CHAR32_EXP as expression for each of the corresponding types.
59+
# (E.g., CHAR16_EXP will be u16 when testing against the program, and
60+
# "(char16_t)-1" when testing the built-in types without a program
61+
# loaded.)
62+
63+
proc do_test_wide_char {lang char16_exp char32_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+
# Check that the fixed-width wide char types are unsigned.
82+
gdb_test "p $char16_exp" " = 65535 u'\\\\xffff'" \
83+
"char16_t is unsigned"
84+
gdb_test "p $char32_exp" " = 4294967295 U'\\\\xffffffff'" \
85+
"char32_t is unsigned"
86+
87+
# Check sizeof. These are fixed-width.
88+
gdb_test "p sizeof($char16_exp)" "= 2" \
89+
"sizeof($char16_exp) == 2"
90+
gdb_test "p sizeof($char32_exp)" "= 4" \
91+
"sizeof(char16_t) == 4"
92+
93+
# Test printing wide literal strings. Note that when testing with
94+
# no program started, this relies on GDB's awareness of the
95+
# built-in wide char types.
96+
gdb_test {p U"hello"} {= U"hello"}
97+
gdb_test {p u"hello"} {= u"hello"}
98+
}
99+
100+
# Make sure that the char16_t/char32_t types are recognized as
101+
# distinct built-in types in C++ mode, even with no program loaded.
102+
# Check that in C mode, the types are not recognized.
103+
104+
proc wide_char_types_no_program {} {
105+
global srcfile testfile
106+
107+
gdb_exit
108+
gdb_start
109+
110+
# These types are not built-in in C.
111+
with_test_prefix "c" {
112+
gdb_test "set language c"
113+
114+
gdb_test "p (char16_t) -1" "No symbol table is loaded.*" \
115+
"char16_t is not built-in"
116+
gdb_test "p (char32_t) -1" "No symbol table is loaded.*" \
117+
"char32_t is not built-in"
118+
119+
gdb_test {p U"hello"} "No type named char32_t\\\."
120+
gdb_test {p u"hello"} "No type named char16_t\\\."
121+
}
122+
123+
# Note GDB does not distinguish C++ dialects, so the fixed-width
124+
# types are always available in C++ mode, even if they were not
125+
# built-in types before C++11.
126+
with_test_prefix "c++" {
127+
gdb_test "set language c++"
128+
129+
do_test_wide_char "c++11" "(char16_t) -1" "(char32_t) -1"
130+
}
131+
}
132+
133+
# Check wide char types with no program loaded.
134+
with_test_prefix "no program" {
135+
wide_char_types_no_program
136+
}
137+
138+
# Check types when a program is loaded.
139+
with_test_prefix "with program" {
140+
foreach_with_prefix lang {"c" "c++03" "c++11"} {
141+
wide_char_types_program $lang
142+
}
143+
}

0 commit comments

Comments
 (0)