[1/3] RFC: add static_assert macro
Tom Tromey
tromey@redhat.com
Wed Nov 16 19:58:00 GMT 2011
In the absence of comments I plan to check this in.
This adds a static_assert macro to gdb_assert.h and changes some code in
MI to use this rather than a runtime check.
I just noticed this in passing, and it seemed like a good cleanup.
Static checks, when possible, are preferable to dynamic ones.
Tom
2011-11-16 Tom Tromey <tromey@redhat.com>
* mi/mi-common.c (_initialize_gdb_mi_common): Remove.
Use static_assert to check the size of
async_reason_string_lookup.
* common/gdb_assert.h (static_assert): New macro.
>From a0751874af42798aa7ec8c21707c1503af017a14 Mon Sep 17 00:00:00 2001
From: Tom Tromey <tromey@redhat.com>
Date: Tue, 15 Nov 2011 14:37:46 -0700
Subject: [PATCH 1/3] add static_assert and change MI to use it
---
gdb/ChangeLog | 7 +++++++
gdb/common/gdb_assert.h | 6 ++++++
gdb/mi/mi-common.c | 14 +++-----------
3 files changed, 16 insertions(+), 11 deletions(-)
diff --git a/gdb/common/gdb_assert.h b/gdb/common/gdb_assert.h
index 634049e..de1a1c9 100644
--- a/gdb/common/gdb_assert.h
+++ b/gdb/common/gdb_assert.h
@@ -20,6 +20,12 @@
#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 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/mi/mi-common.c b/gdb/mi/mi-common.c
index ab126f6..e7dd3c4 100644
--- a/gdb/mi/mi-common.c
+++ b/gdb/mi/mi-common.c
@@ -18,6 +18,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
+#include "gdb_assert.h"
#include "mi-common.h"
static const char * const async_reason_string_lookup[] =
@@ -37,19 +38,10 @@ static const char * const async_reason_string_lookup[] =
NULL
};
+static_assert (ARRAY_SIZE (async_reason_string_lookup) == EXEC_ASYNC_LAST + 1);
+
const char *
async_reason_lookup (enum async_reply_reason reason)
{
return async_reason_string_lookup[reason];
}
-
-/* Provide a prototype to silence -Wmissing-prototypes. */
-extern initialize_file_ftype _initialize_gdb_mi_common;
-
-void
-_initialize_gdb_mi_common (void)
-{
- if (ARRAY_SIZE (async_reason_string_lookup) != EXEC_ASYNC_LAST + 1)
- internal_error (__FILE__, __LINE__,
- _("async_reason_string_lookup is inconsistent"));
-}
--
1.7.6.4
More information about the Gdb-patches
mailing list