Patchwork Replace gdb_static_assert with static_assert

login
register
mail settings
Submitter Simon Marchi
Date Dec. 4, 2017, 9:25 p.m.
Message ID <1512422710-26693-1-git-send-email-simon.marchi@ericsson.com>
Download mbox | patch
Permalink /patch/24718/
State New
Headers show

Comments

Simon Marchi - Dec. 4, 2017, 9:25 p.m.
Since we use C++11, we can replace gdb_static_assert with static_assert,
which is built in the language and gives clearer error messages.

The static_assert in C++11 takes a mandatory "message" argument (as
opposed to C++17).  I tried to put something intelligent there, but
usually it ended up useless/obvious/redundant, just restating the
assertion using words.  So I decided to leave them empty.

gdb/ChangeLog:

	* common/gdb_assert.h: Remove.
	* aarch64-linux-nat.c: Replace gdb_static_assert with
	static_assert.
	* arc-tdep.c: Likewise.
	* defs.h: Likewise.
	* dwarf2read.c: Likewise.
	* gdb_bfd.c: Likewise.
	* i386-fbsd-tdep.c: Likewise.
	* linux-tdep.c: Likewise.
	* mi/mi-common.c: Likewise.
	* nat/amd64-linux-siginfo.c: Likewise.
	* nios2-tdep.c: Likewise.
	* remote-notif.c: Likewise.
	* symfile-mem.c: Likewise.
	* symtab.c: Likewise.
	* symtab.h: Likewise.
	* value.c: Likewise.

gdb/gdbserver/ChangeLog:

	* server.h: Replace gdb_static_assert with static_assert.
---
 gdb/aarch64-linux-nat.c       |  8 ++++----
 gdb/arc-tdep.c                |  2 +-
 gdb/common/gdb_assert.h       |  6 ------
 gdb/defs.h                    |  2 +-
 gdb/dwarf2read.c              |  4 ++--
 gdb/gdb_bfd.c                 |  2 +-
 gdb/gdbserver/server.h        |  2 +-
 gdb/i386-fbsd-tdep.c          | 32 ++++++++++++++++----------------
 gdb/linux-tdep.c              |  2 +-
 gdb/mi/mi-common.c            |  4 ++--
 gdb/nat/amd64-linux-siginfo.c | 10 +++++-----
 gdb/nios2-tdep.c              |  2 +-
 gdb/remote-notif.c            |  2 +-
 gdb/symfile-mem.c             |  6 +++---
 gdb/symtab.c                  |  2 +-
 gdb/symtab.h                  |  6 +++---
 gdb/value.c                   |  2 +-
 17 files changed, 44 insertions(+), 50 deletions(-)
Yao Qi - Dec. 5, 2017, 4:29 p.m.
Simon Marchi <simon.marchi@ericsson.com> writes:

> gdb/ChangeLog:
>
>	* common/gdb_assert.h: Remove.

        * common/gdb_assert.h (gdb_static_assert): Remove.

because you don't remove this file.

> -/* A static assertion.  This will cause a compile-time error if EXPR,
> -   which must be a compile-time constant, is false.  */
> -
> -#define gdb_static_assert(expr) \
> -  extern int never_defined_just_used_for_checking[(expr) ? 1 : -1]

Did you consider define gdb_static_assert as static_assert ((expr), "")?
You don't have to change anywhere else.  Additionally, one day we move
to c++17, we can replace gdb_static_assert with static_assert globally.
Simon Marchi - Dec. 5, 2017, 4:32 p.m.
On 2017-12-05 11:29, Yao Qi wrote:
> Simon Marchi <simon.marchi@ericsson.com> writes:
> 
>> gdb/ChangeLog:
>> 
>> 	* common/gdb_assert.h: Remove.
> 
>         * common/gdb_assert.h (gdb_static_assert): Remove.
> 
> because you don't remove this file.

Woops, thanks.

>> -/* A static assertion.  This will cause a compile-time error if EXPR,
>> -   which must be a compile-time constant, is false.  */
>> -
>> -#define gdb_static_assert(expr) \
>> -  extern int never_defined_just_used_for_checking[(expr) ? 1 : -1]
> 
> Did you consider define gdb_static_assert as static_assert ((expr), 
> "")?
> You don't have to change anywhere else.  Additionally, one day we move
> to c++17, we can replace gdb_static_assert with static_assert globally.

That's a good idea, I'll do that.

Thanks,

Simon

Patch

diff --git a/gdb/aarch64-linux-nat.c b/gdb/aarch64-linux-nat.c
index 9e317c0..c6ce4c4 100644
--- a/gdb/aarch64-linux-nat.c
+++ b/gdb/aarch64-linux-nat.c
@@ -161,7 +161,7 @@  fetch_gregs_from_thread (struct regcache *regcache)
 
   /* Make sure REGS can hold all registers contents on both aarch64
      and arm.  */
-  gdb_static_assert (sizeof (regs) >= 18 * 4);
+  static_assert (sizeof (regs) >= 18 * 4, "");
 
   tid = ptid_get_lwp (regcache_get_ptid (regcache));
 
@@ -199,7 +199,7 @@  store_gregs_to_thread (const struct regcache *regcache)
 
   /* Make sure REGS can hold all registers contents on both aarch64
      and arm.  */
-  gdb_static_assert (sizeof (regs) >= 18 * 4);
+  static_assert (sizeof (regs) >= 18 * 4, "");
   tid = ptid_get_lwp (regcache_get_ptid (regcache));
 
   iovec.iov_base = &regs;
@@ -242,7 +242,7 @@  fetch_fpregs_from_thread (struct regcache *regcache)
 
   /* Make sure REGS can hold all VFP registers contents on both aarch64
      and arm.  */
-  gdb_static_assert (sizeof regs >= VFP_REGS_SIZE);
+  static_assert (sizeof regs >= VFP_REGS_SIZE, "");
 
   tid = ptid_get_lwp (regcache_get_ptid (regcache));
 
@@ -290,7 +290,7 @@  store_fpregs_to_thread (const struct regcache *regcache)
 
   /* Make sure REGS can hold all VFP registers contents on both aarch64
      and arm.  */
-  gdb_static_assert (sizeof regs >= VFP_REGS_SIZE);
+  static_assert (sizeof regs >= VFP_REGS_SIZE, "");
   tid = ptid_get_lwp (regcache_get_ptid (regcache));
 
   iovec.iov_base = &regs;
diff --git a/gdb/arc-tdep.c b/gdb/arc-tdep.c
index 09572ac..bd01e97 100644
--- a/gdb/arc-tdep.c
+++ b/gdb/arc-tdep.c
@@ -190,7 +190,7 @@  arc_insn_get_operand_value_signed (const struct arc_instruction &insn,
       /* Convert unsigned raw value to signed one.  This assumes 2's
 	 complement arithmetic, but so is the LONG_MIN value from generic
 	 defs.h and that assumption is true for ARC.  */
-      gdb_static_assert (sizeof (insn.limm_value) == sizeof (int));
+      static_assert (sizeof (insn.limm_value) == sizeof (int), "");
       return (((LONGEST) insn.limm_value) ^ INT_MIN) - INT_MIN;
     case ARC_OPERAND_KIND_SHIMM:
       /* Sign conversion has been done by binutils.  */
diff --git a/gdb/common/gdb_assert.h b/gdb/common/gdb_assert.h
index 5de9d3f..e8a9d14 100644
--- a/gdb/common/gdb_assert.h
+++ b/gdb/common/gdb_assert.h
@@ -19,12 +19,6 @@ 
 #ifndef GDB_ASSERT_H
 #define GDB_ASSERT_H
 
-/* A static assertion.  This will cause a compile-time error if EXPR,
-   which must be a compile-time constant, is false.  */
-
-#define gdb_static_assert(expr) \
-  extern int never_defined_just_used_for_checking[(expr) ? 1 : -1]
-
 /* PRAGMATICS: "gdb_assert.h":gdb_assert() is a lower case (rather
    than upper case) macro since that provides the closest fit to the
    existing lower case macro <assert.h>:assert() that it is
diff --git a/gdb/defs.h b/gdb/defs.h
index f76293f..cf0434b 100644
--- a/gdb/defs.h
+++ b/gdb/defs.h
@@ -223,7 +223,7 @@  enum language
 /* The number of bits needed to represent all languages, with enough
    padding to allow for reasonable growth.  */
 #define LANGUAGE_BITS 5
-gdb_static_assert (nr_languages <= (1 << LANGUAGE_BITS));
+static_assert (nr_languages <= (1 << LANGUAGE_BITS), "");
 
 enum precision_type
   {
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 78c663c..081ddcf 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -3062,7 +3062,7 @@  create_cus_from_index_list (struct objfile *objfile,
 
   for (i = 0; i < n_elements; i += 2)
     {
-      gdb_static_assert (sizeof (ULONGEST) >= 8);
+      static_assert (sizeof (ULONGEST) >= 8, "");
 
       sect_offset sect_off
 	= (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
@@ -3135,7 +3135,7 @@  create_signatured_type_table_from_index (struct objfile *objfile,
       void **slot;
       cu_offset type_offset_in_tu;
 
-      gdb_static_assert (sizeof (ULONGEST) >= 8);
+      static_assert (sizeof (ULONGEST) >= 8, "");
       sect_offset sect_off
 	= (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
       type_offset_in_tu
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index cc02740..9cafd52 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -880,7 +880,7 @@  gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
 
 
 
-gdb_static_assert (ARRAY_SIZE (_bfd_std_section) == 4);
+static_assert (ARRAY_SIZE (_bfd_std_section) == 4, "");
 
 /* See gdb_bfd.h.  */
 
diff --git a/gdb/gdbserver/server.h b/gdb/gdbserver/server.h
index 46b614c..54d4cbc 100644
--- a/gdb/gdbserver/server.h
+++ b/gdb/gdbserver/server.h
@@ -21,7 +21,7 @@ 
 
 #include "common-defs.h"
 
-gdb_static_assert (sizeof (CORE_ADDR) >= sizeof (void *));
+static_assert (sizeof (CORE_ADDR) >= sizeof (void *), "");
 
 #ifdef __MINGW32CE__
 #include "wincecompat.h"
diff --git a/gdb/i386-fbsd-tdep.c b/gdb/i386-fbsd-tdep.c
index 5a2c67e..46371f6 100644
--- a/gdb/i386-fbsd-tdep.c
+++ b/gdb/i386-fbsd-tdep.c
@@ -107,24 +107,24 @@  static const gdb_byte i386fbsd_osigtramp_end[] =
 };
 
 /* The three different trampolines are all the same size.  */
-gdb_static_assert (sizeof i386fbsd_sigtramp_start
-		   == sizeof i386fbsd_freebsd4_sigtramp_start);
-gdb_static_assert (sizeof i386fbsd_sigtramp_start
-		   == sizeof i386fbsd_osigtramp_start);
-gdb_static_assert (sizeof i386fbsd_sigtramp_middle
-		   == sizeof i386fbsd_freebsd4_sigtramp_middle);
-gdb_static_assert (sizeof i386fbsd_sigtramp_middle
-		   == sizeof i386fbsd_osigtramp_middle);
-gdb_static_assert (sizeof i386fbsd_sigtramp_end
-		   == sizeof i386fbsd_freebsd4_sigtramp_end);
-gdb_static_assert (sizeof i386fbsd_sigtramp_end
-		   == sizeof i386fbsd_osigtramp_end);
+static_assert (sizeof i386fbsd_sigtramp_start
+	       == sizeof i386fbsd_freebsd4_sigtramp_start, "");
+static_assert (sizeof i386fbsd_sigtramp_start
+	       == sizeof i386fbsd_osigtramp_start, "");
+static_assert (sizeof i386fbsd_sigtramp_middle
+	       == sizeof i386fbsd_freebsd4_sigtramp_middle, "");
+static_assert (sizeof i386fbsd_sigtramp_middle
+	       == sizeof i386fbsd_osigtramp_middle, "");
+static_assert (sizeof i386fbsd_sigtramp_end
+	       == sizeof i386fbsd_freebsd4_sigtramp_end, "");
+static_assert (sizeof i386fbsd_sigtramp_end
+	       == sizeof i386fbsd_osigtramp_end, "");
 
 /* We assume that the middle is the largest chunk below.  */
-gdb_static_assert (sizeof i386fbsd_sigtramp_middle
-		   > sizeof i386fbsd_sigtramp_start);
-gdb_static_assert (sizeof i386fbsd_sigtramp_middle
-		   > sizeof i386fbsd_sigtramp_end);
+static_assert (sizeof i386fbsd_sigtramp_middle
+	       > sizeof i386fbsd_sigtramp_start, "");
+static_assert (sizeof i386fbsd_sigtramp_middle
+	       > sizeof i386fbsd_sigtramp_end, "");
 
 static int
 i386fbsd_sigtramp_p (struct frame_info *this_frame)
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index c8a8216..bd4805f 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1008,7 +1008,7 @@  linux_core_info_proc_mappings (struct gdbarch *gdbarch, const char *args)
   unsigned int addr_size_bits, addr_size;
   struct gdbarch *core_gdbarch = gdbarch_from_bfd (core_bfd);
   /* We assume this for reading 64-bit core files.  */
-  gdb_static_assert (sizeof (ULONGEST) >= 8);
+  static_assert (sizeof (ULONGEST) >= 8, "");
 
   section = bfd_get_section_by_name (core_bfd, ".note.linuxcore.file");
   if (section == NULL)
diff --git a/gdb/mi/mi-common.c b/gdb/mi/mi-common.c
index 977ea87..818ad0e 100644
--- a/gdb/mi/mi-common.c
+++ b/gdb/mi/mi-common.c
@@ -42,8 +42,8 @@  static const char * const async_reason_string_lookup[] =
   NULL
 };
 
-gdb_static_assert (ARRAY_SIZE (async_reason_string_lookup)
-		   == EXEC_ASYNC_LAST + 1);
+static_assert (ARRAY_SIZE (async_reason_string_lookup)
+	       == EXEC_ASYNC_LAST + 1, "");
 
 const char *
 async_reason_lookup (enum async_reply_reason reason)
diff --git a/gdb/nat/amd64-linux-siginfo.c b/gdb/nat/amd64-linux-siginfo.c
index 3bdf0db..d960a1a 100644
--- a/gdb/nat/amd64-linux-siginfo.c
+++ b/gdb/nat/amd64-linux-siginfo.c
@@ -587,10 +587,10 @@  amd64_linux_siginfo_fixup_common (siginfo_t *ptrace, gdb_byte *inf,
 
 /* Sanity check for the siginfo structure sizes.  */
 
-gdb_static_assert (sizeof (siginfo_t) == GDB_SI_SIZE);
+static_assert (sizeof (siginfo_t) == GDB_SI_SIZE, "");
 #ifndef __ILP32__
-gdb_static_assert (sizeof (nat_siginfo_t) == GDB_SI_SIZE);
+static_assert (sizeof (nat_siginfo_t) == GDB_SI_SIZE, "");
 #endif
-gdb_static_assert (sizeof (compat_x32_siginfo_t) == GDB_SI_SIZE);
-gdb_static_assert (sizeof (compat_siginfo_t) == GDB_SI_SIZE);
-gdb_static_assert (sizeof (ptrace_siginfo_t) == GDB_SI_SIZE);
+static_assert (sizeof (compat_x32_siginfo_t) == GDB_SI_SIZE, "");
+static_assert (sizeof (compat_siginfo_t) == GDB_SI_SIZE, "");
+static_assert (sizeof (ptrace_siginfo_t) == GDB_SI_SIZE, "");
diff --git a/gdb/nios2-tdep.c b/gdb/nios2-tdep.c
index 0a9f138..c8f0a40 100644
--- a/gdb/nios2-tdep.c
+++ b/gdb/nios2-tdep.c
@@ -138,7 +138,7 @@  static int nios2_dwarf2gdb_regno_map[] =
   NIOS2_MPUACC_REGNUM     /* 48 */
 };
 
-gdb_static_assert (ARRAY_SIZE (nios2_dwarf2gdb_regno_map) == NIOS2_NUM_REGS);
+static_assert (ARRAY_SIZE (nios2_dwarf2gdb_regno_map) == NIOS2_NUM_REGS, "");
 
 /* Implement the dwarf2_reg_to_regnum gdbarch method.  */
 
diff --git a/gdb/remote-notif.c b/gdb/remote-notif.c
index 3952161..1f79a07 100644
--- a/gdb/remote-notif.c
+++ b/gdb/remote-notif.c
@@ -50,7 +50,7 @@  static struct notif_client *notifs[] =
   &notif_client_stop,
 };
 
-gdb_static_assert (ARRAY_SIZE (notifs) == REMOTE_NOTIF_LAST);
+static_assert (ARRAY_SIZE (notifs) == REMOTE_NOTIF_LAST, "");
 
 static void do_notif_event_xfree (void *arg);
 
diff --git a/gdb/symfile-mem.c b/gdb/symfile-mem.c
index 4ce315d..7bbff49 100644
--- a/gdb/symfile-mem.c
+++ b/gdb/symfile-mem.c
@@ -57,9 +57,9 @@ 
 /* Verify parameters of target_read_memory_bfd and target_read_memory are
    compatible.  */
 
-gdb_static_assert (sizeof (CORE_ADDR) == sizeof (bfd_vma));
-gdb_static_assert (sizeof (gdb_byte) == sizeof (bfd_byte));
-gdb_static_assert (sizeof (ssize_t) <= sizeof (bfd_size_type));
+static_assert (sizeof (CORE_ADDR) == sizeof (bfd_vma), "");
+static_assert (sizeof (gdb_byte) == sizeof (bfd_byte), "");
+static_assert (sizeof (ssize_t) <= sizeof (bfd_size_type), "");
 
 /* Provide bfd/ compatible prototype for target_read_memory.  Casting would not
    be enough as LEN width may differ.  */
diff --git a/gdb/symtab.c b/gdb/symtab.c
index dd7434e..2394623 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -5681,7 +5681,7 @@  const struct symbol_impl *symbol_impls = &symbol_impl[0];
 
 /* Make sure we saved enough room in struct symbol.  */
 
-gdb_static_assert (MAX_SYMBOL_IMPLS <= (1 << SYMBOL_ACLASS_BITS));
+static_assert (MAX_SYMBOL_IMPLS <= (1 << SYMBOL_ACLASS_BITS), "");
 
 /* Register a computed symbol type.  ACLASS must be LOC_COMPUTED.  OPS
    is the ops vector associated with this index.  This returns the new
diff --git a/gdb/symtab.h b/gdb/symtab.h
index 239a479..f2c9490 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -592,7 +592,7 @@  enum minimal_symbol_type
 /* The number of enum minimal_symbol_type values, with some padding for
    reasonable growth.  */
 #define MINSYM_TYPE_BITS 4
-gdb_static_assert (nr_minsym_types <= (1 << MINSYM_TYPE_BITS));
+static_assert (nr_minsym_types <= (1 << MINSYM_TYPE_BITS), "");
 
 /* Define a simple structure used to hold some very basic information about
    all defined global symbols (text, data, bss, abs, etc).  The only required
@@ -749,7 +749,7 @@  typedef enum domain_enum_tag
 /* The number of bits in a symbol used to represent the domain.  */
 
 #define SYMBOL_DOMAIN_BITS 3
-gdb_static_assert (NR_DOMAINS <= (1 << SYMBOL_DOMAIN_BITS));
+static_assert (NR_DOMAINS <= (1 << SYMBOL_DOMAIN_BITS), "");
 
 extern const char *domain_name (domain_enum);
 
@@ -892,7 +892,7 @@  enum address_class
    verify that we have reserved enough space for synthetic address
    classes.  */
 #define SYMBOL_ACLASS_BITS 5
-gdb_static_assert (LOC_FINAL_VALUE <= (1 << SYMBOL_ACLASS_BITS));
+static_assert (LOC_FINAL_VALUE <= (1 << SYMBOL_ACLASS_BITS), "");
 
 /* The methods needed to implement LOC_COMPUTED.  These methods can
    use the symbol's .aux_value for additional per-symbol information.
diff --git a/gdb/value.c b/gdb/value.c
index 3e0ca25..6089ced 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -964,7 +964,7 @@  static int max_value_size = 65536; /* 64k bytes */
    is bigger than LONGEST on all GDB supported hosts we're fine.  */
 
 #define MIN_VALUE_FOR_MAX_VALUE_SIZE 16
-gdb_static_assert (sizeof (LONGEST) <= MIN_VALUE_FOR_MAX_VALUE_SIZE);
+static_assert (sizeof (LONGEST) <= MIN_VALUE_FOR_MAX_VALUE_SIZE, "");
 
 /* Implement the "set max-value-size" command.  */