Teach GDB that wchar_t is a built-in type in C++ mode
authorPedro Alves <palves@redhat.com>
Wed, 12 Apr 2017 13:00:49 +0000 (14:00 +0100)
committerPedro Alves <palves@redhat.com>
Wed, 12 Apr 2017 13:06:40 +0000 (14:06 +0100)
GDB is currently not aware that wchar_t is a built-in type in C++
mode.  This is usually not a problem because the debug info describes
the type, so when you have a program loaded, you don't notice this.
However, if you try expressions involving wchar_t before a program is
loaded, gdb errors out:

 (gdb) p (wchar_t)-1
 No symbol table is loaded.  Use the "file" command.
 (gdb) p L"hello"
 No type named wchar_t.
 (gdb) ptype L"hello"
 No type named wchar_t.

This commit teaches gdb about the type.  After:

 (gdb) p (wchar_t)-1
 $1 = -1 L'\xffffffff'
 (gdb) p L"hello"
 $2 = L"hello"
 (gdb) ptype L"hello"
 type = wchar_t [6]

Unlike char16_t/char32_t, unfortunately, the underlying type of
wchar_t is implementation dependent, both size and signness.  So this
requires adding a couple new gdbarch hooks.

I grepped the GCC code base for WCHAR_TYPE and WCHAR_TYPE_SIZE, and it
seems to me that the majority of the ABIs have a 4-byte signed
wchar_t, so that's what I made the default for GDB too.  And then I
looked for which ports have a 16-bit and/or unsigned wchar_t, and made
GDB follow suit.

gdb/ChangeLog:
2017-04-12  Pedro Alves  <palves@redhat.com>

PR gdb/21323
* c-lang.c (cplus_primitive_types) <cplus_primitive_type_wchar_t>:
New enum value.
(cplus_language_arch_info): Register cplus_primitive_type_wchar_t.
* gdbtypes.h (struct builtin_type) <builtin_wchar>: New field.
* gdbtypes.c (gdbtypes_post_init): Create the "wchar_t" type.
* gdbarch.sh (wchar_bit, wchar_signed): New per-arch values.
* gdbarch.h, gdbarch.c: Regenerate.
* aarch64-tdep.c (aarch64_gdbarch_init): Override
gdbarch_wchar_bit and gdbarch_wchar_signed.
* alpha-tdep.c (alpha_gdbarch_init): Likewise.
* arm-tdep.c (arm_gdbarch_init): Likewise.
* avr-tdep.c (avr_gdbarch_init): Likewise.
* h8300-tdep.c (h8300_gdbarch_init): Likewise.
* i386-nto-tdep.c (i386nto_init_abi): Likewise.
* i386-tdep.c (i386_go32_init_abi): Likewise.
* m32r-tdep.c (m32r_gdbarch_init): Likewise.
* moxie-tdep.c (moxie_gdbarch_init): Likewise.
* nds32-tdep.c (nds32_gdbarch_init): Likewise.
* rs6000-aix-tdep.c (rs6000_aix_init_osabi): Likewise.
* sh-tdep.c (sh_gdbarch_init): Likewise.
* sparc-tdep.c (sparc32_gdbarch_init): Likewise.
* sparc64-tdep.c (sparc64_init_abi): Likewise.
* windows-tdep.c (windows_init_abi): Likewise.
* xstormy16-tdep.c (xstormy16_gdbarch_init): Likewise.

gdb/testsuite/ChangeLog:
2017-04-12  Pedro Alves  <palves@redhat.com>

PR gdb/21323
* gdb.cp/wide_char_types.c: Include <wchar.h>.
(wchar): New global.
* gdb.cp/wide_char_types.exp (wide_char_types_program)
(do_test_wide_char, wide_char_types_no_program, top level): Add
wchar_t testing.

27 files changed:
gdb/ChangeLog
gdb/aarch64-tdep.c
gdb/alpha-tdep.c
gdb/arm-tdep.c
gdb/avr-tdep.c
gdb/c-lang.c
gdb/gdbarch.c
gdb/gdbarch.h
gdb/gdbarch.sh
gdb/gdbtypes.c
gdb/gdbtypes.h
gdb/h8300-tdep.c
gdb/i386-nto-tdep.c
gdb/i386-tdep.c
gdb/m32r-tdep.c
gdb/moxie-tdep.c
gdb/nds32-tdep.c
gdb/rs6000-aix-tdep.c
gdb/sh-tdep.c
gdb/sparc-tdep.c
gdb/sparc64-tdep.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.cp/wide_char_types.c
gdb/testsuite/gdb.cp/wide_char_types.exp
gdb/windows-tdep.c
gdb/xstormy16-tdep.c
gdb/xtensa-tdep.c

index bdcbd1e2f96586328d84bcc9a205ac3bf2c9e152..f404b74f92b99d783e2e677ffd63fce1fcf8ed89 100644 (file)
@@ -1,3 +1,31 @@
+2017-04-12  Pedro Alves  <palves@redhat.com>
+
+       PR gdb/21323
+       * c-lang.c (cplus_primitive_types) <cplus_primitive_type_wchar_t>:
+       New enum value.
+       (cplus_language_arch_info): Register cplus_primitive_type_wchar_t.
+       * gdbtypes.h (struct builtin_type) <builtin_wchar>: New field.
+       * gdbtypes.c (gdbtypes_post_init): Create the "wchar_t" type.
+       * gdbarch.sh (wchar_bit, wchar_signed): New per-arch values.
+       * gdbarch.h, gdbarch.c: Regenerate.
+       * aarch64-tdep.c (aarch64_gdbarch_init): Override
+       gdbarch_wchar_bit and gdbarch_wchar_signed.
+       * alpha-tdep.c (alpha_gdbarch_init): Likewise.
+       * arm-tdep.c (arm_gdbarch_init): Likewise.
+       * avr-tdep.c (avr_gdbarch_init): Likewise.
+       * h8300-tdep.c (h8300_gdbarch_init): Likewise.
+       * i386-nto-tdep.c (i386nto_init_abi): Likewise.
+       * i386-tdep.c (i386_go32_init_abi): Likewise.
+       * m32r-tdep.c (m32r_gdbarch_init): Likewise.
+       * moxie-tdep.c (moxie_gdbarch_init): Likewise.
+       * nds32-tdep.c (nds32_gdbarch_init): Likewise.
+       * rs6000-aix-tdep.c (rs6000_aix_init_osabi): Likewise.
+       * sh-tdep.c (sh_gdbarch_init): Likewise.
+       * sparc-tdep.c (sparc32_gdbarch_init): Likewise.
+       * sparc64-tdep.c (sparc64_init_abi): Likewise.
+       * windows-tdep.c (windows_init_abi): Likewise.
+       * xstormy16-tdep.c (xstormy16_gdbarch_init): Likewise.
+
 2017-04-12  Pedro Alves  <palves@redhat.com>
 
        PR c++/21323
index f340d57df2aee6531b93baa00158cc8e443a8662..28c2573d18d23a41e863875d0fdf4472767df6b8 100644 (file)
@@ -2977,6 +2977,8 @@ aarch64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_long_long_bit (gdbarch, 64);
   set_gdbarch_ptr_bit (gdbarch, 64);
   set_gdbarch_char_signed (gdbarch, 0);
+  set_gdbarch_wchar_bit (gdbarch, 64);
+  set_gdbarch_wchar_signed (gdbarch, 0);
   set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
   set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
   set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
index 4dd65c58cc2896ea6038399128e3eb376d1ee672..7c521db24c4c41454d99c39749f4d0ae8f82b00f 100644 (file)
@@ -1771,6 +1771,8 @@ alpha_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_int_bit (gdbarch, 32);
   set_gdbarch_long_bit (gdbarch, 64);
   set_gdbarch_long_long_bit (gdbarch, 64);
+  set_gdbarch_wchar_bit (gdbarch, 64);
+  set_gdbarch_wchar_signed (gdbarch, 0);
   set_gdbarch_float_bit (gdbarch, 32);
   set_gdbarch_double_bit (gdbarch, 64);
   set_gdbarch_long_double_bit (gdbarch, 64);
index 5cc3f402e7eb3f1a3559a1b11d52a4d825f4cf05..c8fabfb31ee7a8b68bc790c11696915ba1f2d26c 100644 (file)
@@ -9404,6 +9404,13 @@ arm_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   /* On ARM targets char defaults to unsigned.  */
   set_gdbarch_char_signed (gdbarch, 0);
 
+  /* wchar_t is unsigned under the AAPCS.  */
+  if (tdep->arm_abi == ARM_ABI_AAPCS)
+    set_gdbarch_wchar_signed (gdbarch, 0);
+  else
+    set_gdbarch_wchar_signed (gdbarch, 1);
+  set_gdbarch_wchar_bit (gdbarch, 32);
+
   /* Note: for displaced stepping, this includes the breakpoint, and one word
      of additional scratch space.  This setting isn't used for anything beside
      displaced stepping at present.  */
index 0ae8a08f6bfa898aec352bdc5671c3c3f90a8e97..ec9db1cb3a78be1e7e5fea75871596d1f8d38bec 100644 (file)
@@ -1472,6 +1472,9 @@ avr_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
   set_gdbarch_addr_bit (gdbarch, 32);
 
+  set_gdbarch_wchar_bit (gdbarch, 2 * TARGET_CHAR_BIT);
+  set_gdbarch_wchar_signed (gdbarch, 1);
+
   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
index 616aa267e84c563098cd9af81b5fd057497a49c9..19a8608a01ae251ba2d74ac2732bf8a1721fcd24 100644 (file)
@@ -897,6 +897,7 @@ enum cplus_primitive_types {
   cplus_primitive_type_declong,
   cplus_primitive_type_char16_t,
   cplus_primitive_type_char32_t,
+  cplus_primitive_type_wchar_t,
   nr_cplus_primitive_types
 };
 
@@ -956,6 +957,8 @@ cplus_language_arch_info (struct gdbarch *gdbarch,
     = builtin->builtin_char16;
   lai->primitive_type_vector [cplus_primitive_type_char32_t]
     = builtin->builtin_char32;
+  lai->primitive_type_vector [cplus_primitive_type_wchar_t]
+    = builtin->builtin_wchar;
 
   lai->bool_type_symbol = "bool";
   lai->bool_type_default = builtin->builtin_bool;
index 319a8902a831ad19e785385fb52269659443a861..56643256dab00027f043e6ef58ff59477083e7f5 100644 (file)
@@ -184,6 +184,8 @@ struct gdbarch
   const struct floatformat ** double_format;
   int long_double_bit;
   const struct floatformat ** long_double_format;
+  int wchar_bit;
+  int wchar_signed;
   gdbarch_floatformat_for_type_ftype *floatformat_for_type;
   int ptr_bit;
   int addr_bit;
@@ -389,6 +391,8 @@ gdbarch_alloc (const struct gdbarch_info *info,
   gdbarch->float_bit = 4*TARGET_CHAR_BIT;
   gdbarch->double_bit = 8*TARGET_CHAR_BIT;
   gdbarch->long_double_bit = 8*TARGET_CHAR_BIT;
+  gdbarch->wchar_bit = 4*TARGET_CHAR_BIT;
+  gdbarch->wchar_signed = -1;
   gdbarch->floatformat_for_type = default_floatformat_for_type;
   gdbarch->ptr_bit = gdbarch->int_bit;
   gdbarch->char_signed = -1;
@@ -533,6 +537,9 @@ verify_gdbarch (struct gdbarch *gdbarch)
   /* Skip verify of long_double_bit, invalid_p == 0 */
   if (gdbarch->long_double_format == 0)
     gdbarch->long_double_format = floatformats_ieee_double;
+  /* Skip verify of wchar_bit, invalid_p == 0 */
+  if (gdbarch->wchar_signed == -1)
+    gdbarch->wchar_signed = 1;
   /* Skip verify of floatformat_for_type, invalid_p == 0 */
   /* Skip verify of ptr_bit, invalid_p == 0 */
   if (gdbarch->addr_bit == 0)
@@ -1456,6 +1463,12 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)
   fprintf_unfiltered (file,
                       "gdbarch_dump: vtable_function_descriptors = %s\n",
                       plongest (gdbarch->vtable_function_descriptors));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: wchar_bit = %s\n",
+                      plongest (gdbarch->wchar_bit));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: wchar_signed = %s\n",
+                      plongest (gdbarch->wchar_signed));
   fprintf_unfiltered (file,
                       "gdbarch_dump: gdbarch_write_pc_p() = %d\n",
                       gdbarch_write_pc_p (gdbarch));
@@ -1757,6 +1770,41 @@ set_gdbarch_long_double_format (struct gdbarch *gdbarch,
   gdbarch->long_double_format = long_double_format;
 }
 
+int
+gdbarch_wchar_bit (struct gdbarch *gdbarch)
+{
+  gdb_assert (gdbarch != NULL);
+  /* Skip verify of wchar_bit, invalid_p == 0 */
+  if (gdbarch_debug >= 2)
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_wchar_bit called\n");
+  return gdbarch->wchar_bit;
+}
+
+void
+set_gdbarch_wchar_bit (struct gdbarch *gdbarch,
+                       int wchar_bit)
+{
+  gdbarch->wchar_bit = wchar_bit;
+}
+
+int
+gdbarch_wchar_signed (struct gdbarch *gdbarch)
+{
+  gdb_assert (gdbarch != NULL);
+  /* Check variable changed from pre-default.  */
+  gdb_assert (gdbarch->wchar_signed != -1);
+  if (gdbarch_debug >= 2)
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_wchar_signed called\n");
+  return gdbarch->wchar_signed;
+}
+
+void
+set_gdbarch_wchar_signed (struct gdbarch *gdbarch,
+                          int wchar_signed)
+{
+  gdbarch->wchar_signed = wchar_signed;
+}
+
 const struct floatformat **
 gdbarch_floatformat_for_type (struct gdbarch *gdbarch, const char *name, int length)
 {
index 3264c44711f2a5178a8ea98220e5d196305e087c..4845f239d18d3e09eb8c5afd99c0ffb147e1989e 100644 (file)
@@ -188,6 +188,17 @@ extern void set_gdbarch_long_double_bit (struct gdbarch *gdbarch, int long_doubl
 extern const struct floatformat ** gdbarch_long_double_format (struct gdbarch *gdbarch);
 extern void set_gdbarch_long_double_format (struct gdbarch *gdbarch, const struct floatformat ** long_double_format);
 
+/* The ABI default bit-size for "wchar_t".  wchar_t is a built-in type
+   starting with C++11. */
+
+extern int gdbarch_wchar_bit (struct gdbarch *gdbarch);
+extern void set_gdbarch_wchar_bit (struct gdbarch *gdbarch, int wchar_bit);
+
+/* One if `wchar_t' is signed, zero if unsigned. */
+
+extern int gdbarch_wchar_signed (struct gdbarch *gdbarch);
+extern void set_gdbarch_wchar_signed (struct gdbarch *gdbarch, int wchar_signed);
+
 /* Returns the floating-point format to be used for values of length LENGTH.
    NAME, if non-NULL, is the type name, which may be used to distinguish
    different target formats of the same length. */
index bdfb6b20d0f3e131bcaaf4ef682360612ac69145..a42dc437742ee5cdfce8326103608f421832285c 100755 (executable)
@@ -383,6 +383,12 @@ v:const struct floatformat **:double_format:::::floatformats_ieee_double::pforma
 v:int:long_double_bit:::8 * sizeof (long double):8*TARGET_CHAR_BIT::0
 v:const struct floatformat **:long_double_format:::::floatformats_ieee_double::pformat (gdbarch->long_double_format)
 
+# The ABI default bit-size for "wchar_t".  wchar_t is a built-in type
+# starting with C++11.
+v:int:wchar_bit:::8 * sizeof (wchar_t):4*TARGET_CHAR_BIT::0
+# One if \`wchar_t' is signed, zero if unsigned.
+v:int:wchar_signed:::1:-1:1
+
 # Returns the floating-point format to be used for values of length LENGTH.
 # NAME, if non-NULL, is the type name, which may be used to distinguish
 # different target formats of the same length.
index c1f76fb539e061dd717f980d60a5c0d64c15fa4b..dd3992cc7b6e4f73607b08f692d86a06b51660dd 100644 (file)
@@ -5207,6 +5207,9 @@ gdbtypes_post_init (struct gdbarch *gdbarch)
     = arch_integer_type (gdbarch, 16, 1, "char16_t");
   builtin_type->builtin_char32
     = arch_integer_type (gdbarch, 32, 1, "char32_t");
+  builtin_type->builtin_wchar
+    = arch_integer_type (gdbarch, gdbarch_wchar_bit (gdbarch),
+                        !gdbarch_wchar_signed (gdbarch), "wchar_t");
 
   /* Default data/code pointer types.  */
   builtin_type->builtin_data_ptr
index 3c57970728a0b0b44d6fa5af8c1203114af40f59..f6b4de90bc2917699a5a7733ad3578dd94bce3a8 100644 (file)
@@ -1503,6 +1503,7 @@ struct builtin_type
   /* Wide character types.  */
   struct type *builtin_char16;
   struct type *builtin_char32;
+  struct type *builtin_wchar;
 
   /* Pointer types.  */
 
index 060e5d371480093b6abe7571481360a008c5c735..7b260b119f68bb8e218bf0c2ce64efa17afd8381 100644 (file)
@@ -1381,6 +1381,10 @@ h8300_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
+
+  set_gdbarch_wchar_bit (gdbarch, 2 * TARGET_CHAR_BIT);
+  set_gdbarch_wchar_signed (gdbarch, 0);
+
   set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
   set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
index a12a56da3b0619bd732c7d3ccfd1acc04417fd77..3fe715f4ca00cc7dcd36075cfaf3d5e3db945ca4 100644 (file)
@@ -362,6 +362,9 @@ i386nto_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
         = nto_in_dynsym_resolve_code;
     }
   set_solib_ops (gdbarch, &nto_svr4_so_ops);
+
+  set_gdbarch_wchar_bit (gdbarch, 32);
+  set_gdbarch_wchar_signed (gdbarch, 0);
 }
 
 /* Provide a prototype to silence -Wmissing-prototypes.  */
index 70bd47ce3000a7fbdb79dc097bbd76dba0a60d41..49f9824842d3ee4af7d7d7ed54972e2bff86ccc2 100644 (file)
@@ -4515,6 +4515,9 @@ i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_has_dos_based_file_system (gdbarch, 1);
 
   set_gdbarch_gnu_triplet_regexp (gdbarch, i386_gnu_triplet_regexp);
+
+  set_gdbarch_wchar_bit (gdbarch, 16);
+  set_gdbarch_wchar_signed (gdbarch, 0);
 }
 \f
 
index 4701f7a89e0cad37a183c260086bc471a10d952f..0820af9d61888da368cefd33e99fb30199b278ca 100644 (file)
@@ -911,6 +911,9 @@ m32r_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   tdep = XNEW (struct gdbarch_tdep);
   gdbarch = gdbarch_alloc (&info, tdep);
 
+  set_gdbarch_wchar_bit (gdbarch, 16);
+  set_gdbarch_wchar_signed (gdbarch, 0);
+
   set_gdbarch_read_pc (gdbarch, m32r_read_pc);
   set_gdbarch_unwind_sp (gdbarch, m32r_unwind_sp);
 
index d0f4223d7f074aa6e63166eacd9c6489c573fc22..dd7a6f4bfb1a64af216aee1f5ba1ea0b8ababf2c 100644 (file)
@@ -1112,6 +1112,9 @@ moxie_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   tdep = XNEW (struct gdbarch_tdep);
   gdbarch = gdbarch_alloc (&info, tdep);
 
+  set_gdbarch_wchar_bit (gdbarch, 32);
+  set_gdbarch_wchar_signed (gdbarch, 0);
+
   set_gdbarch_read_pc (gdbarch, moxie_read_pc);
   set_gdbarch_write_pc (gdbarch, moxie_write_pc);
   set_gdbarch_unwind_sp (gdbarch, moxie_unwind_sp);
index 05c48aa27d84bc0286712f0143a9447a79ae066b..e0eea3743a9ae06be92d627c1ca0e35fad0b0a1f 100644 (file)
@@ -2069,6 +2069,9 @@ nds32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 
   gdbarch = gdbarch_alloc (&info, tdep);
 
+  set_gdbarch_wchar_bit (gdbarch, 16);
+  set_gdbarch_wchar_signed (gdbarch, 0);
+
   if (fpu_freg == -1)
     num_regs = NDS32_NUM_REGS;
   else if (use_pseudo_fsrs == 1)
index 9841a60eafb0381cd2343dd3a8d5745571f5460e..a14e93c66aa20669c84d361238e034f9d21034ec 100644 (file)
@@ -1080,6 +1080,11 @@ rs6000_aix_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
   else
     set_gdbarch_frame_red_zone_size (gdbarch, 0);
 
+  if (tdep->wordsize == 8)
+    set_gdbarch_wchar_bit (gdbarch, 32);
+  else
+    set_gdbarch_wchar_bit (gdbarch, 16);
+  set_gdbarch_wchar_signed (gdbarch, 0);
   set_gdbarch_auto_wide_charset (gdbarch, rs6000_aix_auto_wide_charset);
 
   set_solib_ops (gdbarch, &solib_aix_so_ops);
index f4b4c486fd59715b9eacfecf034653ce570716a9..0bc678a286bb3e084aa37bcf4dd7d65acacb5c64 100644 (file)
@@ -2268,6 +2268,10 @@ sh_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
+
+  set_gdbarch_wchar_bit (gdbarch, 2 * TARGET_CHAR_BIT);
+  set_gdbarch_wchar_signed (gdbarch, 0);
+
   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
index d346aec5f9cd5628d3c47023654f678398c897f1..078907a5d4aeaa472f0ce745efd033bab98d8dcc 100644 (file)
@@ -1749,6 +1749,9 @@ sparc32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_long_double_bit (gdbarch, 128);
   set_gdbarch_long_double_format (gdbarch, floatformats_sparc_quad);
 
+  set_gdbarch_wchar_bit (gdbarch, 16);
+  set_gdbarch_wchar_signed (gdbarch, 1);
+
   set_gdbarch_num_regs (gdbarch, SPARC32_NUM_REGS);
   set_gdbarch_register_name (gdbarch, sparc32_register_name);
   set_gdbarch_register_type (gdbarch, sparc32_register_type);
index bf0da18d86ef7f98fb99f8b836c816491219941b..58f5bf08e4e0f2ed6c7ebb6d9bc617692dcd689a 100644 (file)
@@ -1268,6 +1268,9 @@ sparc64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_long_long_bit (gdbarch, 64);
   set_gdbarch_ptr_bit (gdbarch, 64);
 
+  set_gdbarch_wchar_bit (gdbarch, 16);
+  set_gdbarch_wchar_signed (gdbarch, 0);
+
   set_gdbarch_num_regs (gdbarch, SPARC64_NUM_REGS);
   set_gdbarch_register_name (gdbarch, sparc64_register_name);
   set_gdbarch_register_type (gdbarch, sparc64_register_type);
index e2555e05c37fe53b56c9abd135b76301deb7d190..7f858e6ed290356854816d2fb9281f370eaf67c3 100644 (file)
@@ -1,3 +1,12 @@
+2017-04-12  Pedro Alves  <palves@redhat.com>
+
+       PR gdb/21323
+       * gdb.cp/wide_char_types.c: Include <wchar.h>.
+       (wchar): New global.
+       * gdb.cp/wide_char_types.exp (wide_char_types_program)
+       (do_test_wide_char, wide_char_types_no_program, top level): Add
+       wchar_t testing.
+
 2017-04-12  Pedro Alves  <palves@redhat.com>
 
        PR c++/21323
index 8337cd4cbce534a3fd594dc33253184f40e42408..c899b7191cf5cf3fbab54fcfc256f47ba106f829 100644 (file)
 */
 
 #include <uchar.h>
+#include <wchar.h>
 
 char16_t u16 = -1;
 char32_t u32 = -1;
+wchar_t wchar = -1;
 
 int
 main ()
index df5c8a8f1dfa53021638a55c9e59148e649f522e..6a913503dffc208a9af78e97e6aa07b62bb1d15a 100644 (file)
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-# Test GDB's awareness of the char16_t, char32_t (C++11+) built-in
-# types.  We also run most tests here in C mode, and check whether the
-# built-ins are disabled (gdb uses the typedefs in the debug info
-# instead.)
+# Test GDB's awareness of the wchar_t (C++98+) and char16_t, char32_t
+# (C++11+) built-in types.  We also run most tests here in C mode, and
+# check whether the built-ins are disabled (gdb uses the typedefs in
+# the debug info instead.)
 
 standard_testfile
 
-# Test char16_t/char32_t in language LANG, against symbols in
+# Test char16_t/char32_t/wchar_t in language LANG, against symbols in
 # a program.  Lang can be "c", "c++03" or "c++11".  In C++11,
 # char16_t/char32_t are built-in types, and the debug information
 # reflects that (see
@@ -51,16 +51,16 @@ proc wide_char_types_program {lang} {
        fail "can't run to main"
        return 0
     }
-    do_test_wide_char $lang "u16" "u32"
+    do_test_wide_char $lang "u16" "u32" "wchar"
 }
 
-# Test char16_t/char32_t in language LANG.  Use CHAR16_EXP and
-# CHAR32_EXP as expression for each of the corresponding types.
-# (E.g., CHAR16_EXP will be u16 when testing against the program, and
-# "(char16_t)-1" when testing the built-in types without a program
-# loaded.)
+# Test char16_t/char32_t/wchar_t in language LANG.  Use CHAR16_EXP,
+# CHAR32_EXP, and WCHAR_EXP as expression for each of the
+# corresponding types.  (E.g., CHAR16_EXP will be u16 when testing
+# against the program, and "(char16_t)-1" when testing the built-in
+# types without a program loaded.)
 
-proc do_test_wide_char {lang char16_exp char32_exp} {
+proc do_test_wide_char {lang char16_exp char32_exp wchar_exp} {
     global gdb_prompt
 
     # Check that the fixed-width wide types are distinct built-in
@@ -78,26 +78,60 @@ proc do_test_wide_char {lang char16_exp char32_exp} {
            "char32_t is typedef"
     }
 
+    # wchar_t is a disctinct built-in type in C++03+.
+    if {$lang != "c"} {
+       gdb_test "ptype $wchar_exp" "type = wchar_t" \
+           "wchar_t is distinct"
+    } else {
+       gdb_test "ptype $wchar_exp" "type = (unsigned )?(long|int|short)" \
+           "wchar_t is typedef"
+    }
+
     # Check that the fixed-width wide char types are unsigned.
     gdb_test "p $char16_exp" " = 65535 u'\\\\xffff'" \
        "char16_t is unsigned"
     gdb_test "p $char32_exp" " = 4294967295 U'\\\\xffffffff'" \
        "char32_t is unsigned"
 
+    # Whether wchar_t is signed is implementation-dependent.  While we
+    # ignore whether GDB got the ABI size/sign details right here,
+    # this at least verifies that the value isn't garbage, and that
+    # GDB correctly outputs the character using the "L" prefix.
+    set test "wchar_t sign"
+    gdb_test_multiple "p $wchar_exp" $test {
+       -re " = 4294967295 L'\\\\xffffffff'\r\n$gdb_prompt $" {
+           pass "$test (unsigned)"
+       }
+       -re " = 65535 L'\\\\xffff'\r\n$gdb_prompt $" {
+           pass "$test (unsigned)"
+       }
+       -re " = -1 L'\\\\xffffffff'\r\n$gdb_prompt $" {
+           pass "$test (signed)"
+       }
+       -re " = -1 L'\\\\xffff'\r\n$gdb_prompt $" {
+           pass "$test (signed)"
+       }
+    }
+
     # Check sizeof.  These are fixed-width.
     gdb_test "p sizeof($char16_exp)" "= 2" \
        "sizeof($char16_exp) == 2"
     gdb_test "p sizeof($char32_exp)" "= 4" \
        "sizeof(char16_t) == 4"
 
+    # Size of wchar_t depends on ABI.
+    gdb_test "p sizeof($wchar_exp)" "= (2|4)" \
+       "sizeof(wchar_t)"
+
     # Test printing wide literal strings.  Note that when testing with
     # no program started, this relies on GDB's awareness of the
     # built-in wide char types.
     gdb_test {p U"hello"} {= U"hello"}
     gdb_test {p u"hello"} {= u"hello"}
+    gdb_test {p L"hello"} {= L"hello"}
 }
 
-# Make sure that the char16_t/char32_t types are recognized as
+# Make sure that the char16_t/char32_t/wchar_t types are recognized as
 # distinct built-in types in C++ mode, even with no program loaded.
 # Check that in C mode, the types are not recognized.
 
@@ -116,8 +150,12 @@ proc wide_char_types_no_program {} {
        gdb_test "p (char32_t) -1" "No symbol table is loaded.*" \
            "char32_t is not built-in"
 
+       gdb_test "p (wchar_t) -1" "No symbol table is loaded.*" \
+           "wchar_t is not built-in"
+
        gdb_test {p U"hello"} "No type named char32_t\\\."
        gdb_test {p u"hello"} "No type named char16_t\\\."
+       gdb_test {p L"hello"} "No type named wchar_t\\\."
     }
 
     # Note GDB does not distinguish C++ dialects, so the fixed-width
@@ -126,7 +164,7 @@ proc wide_char_types_no_program {} {
     with_test_prefix "c++" {
        gdb_test "set language c++"
 
-       do_test_wide_char "c++11" "(char16_t) -1" "(char32_t) -1"
+       do_test_wide_char "c++11" "(char16_t) -1" "(char32_t) -1" "(wchar_t) -1"
     }
 }
 
index 62a303d31705f3a4de71498330573d925c9a4f91..3e7e8f8b2797f8bf51b44585d9b54ace1b486cff 100644 (file)
@@ -466,6 +466,9 @@ init_w32_command_list (void)
 void
 windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 {
+  set_gdbarch_wchar_bit (gdbarch, 16);
+  set_gdbarch_wchar_signed (gdbarch, 0);
+
   /* Canonical paths on this target look like
      `c:\Program Files\Foo App\mydll.dll', for example.  */
   set_gdbarch_has_dos_based_file_system (gdbarch, 1);
index bfa7f0a6eef43594627c44bf404e6b4cf6928948..7fdfc6517a6c1c7c0443100594f282c9c227ccca 100644 (file)
@@ -809,6 +809,9 @@ xstormy16_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
 
+  set_gdbarch_wchar_bit (gdbarch, 2 * TARGET_CHAR_BIT);
+  set_gdbarch_wchar_signed (gdbarch, 1);
+
   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
index 7fc881f8022f596ef229f31b20929852eaef442a..0fcd483165450a780c0fcded5c266a36bf339757 100644 (file)
@@ -3200,6 +3200,9 @@ xtensa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   xtensa_verify_config (gdbarch);
   xtensa_session_once_reported = 0;
 
+  set_gdbarch_wchar_bit (gdbarch, 2 * TARGET_CHAR_BIT);
+  set_gdbarch_wchar_signed (gdbarch, 0);
+
   /* Pseudo-Register read/write.  */
   set_gdbarch_pseudo_register_read (gdbarch, xtensa_pseudo_register_read);
   set_gdbarch_pseudo_register_write (gdbarch, xtensa_pseudo_register_write);
This page took 0.08145 seconds and 4 git commands to generate.