[telepathy-glib/master] Add tp_verify and tp_verify_true, compile-time static assertion macros

Simon McVittie simon.mcvittie at collabora.co.uk
Fri Jul 31 05:42:33 PDT 2009


Imported from the following gnulib module, and adapted to be in the
telepathy-glib namespace:

Description:
Compile-time assert-like macros.

Files:
lib/verify.h

Depends-on:

configure.ac:

Makefile.am:
lib_SOURCES += verify.h

Include:
"verify.h"

License:
LGPLv2+

Maintainer:
Paul Eggert, Jim Meyering
---
 docs/reference/telepathy-glib-sections.txt |    2 +
 telepathy-glib/Makefile.am                 |    3 +-
 telepathy-glib/util.c                      |   32 ++++++
 telepathy-glib/util.h                      |    8 +-
 telepathy-glib/verify.h                    |  146 ++++++++++++++++++++++++++++
 5 files changed, 188 insertions(+), 3 deletions(-)
 create mode 100644 telepathy-glib/verify.h

diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt
index 71aaa73..cb8c352 100644
--- a/docs/reference/telepathy-glib-sections.txt
+++ b/docs/reference/telepathy-glib-sections.txt
@@ -1023,6 +1023,8 @@ TP_SVC_CONNECTION_MANAGER_GET_CLASS
 <FILE>util</FILE>
 <TITLE>util</TITLE>
 <INCLUDE>telepathy-glib/util.h</INCLUDE>
+tp_verify
+tp_verify_true
 tp_g_hash_table_update
 tp_g_ptr_array_contains
 tp_g_value_slice_new
diff --git a/telepathy-glib/Makefile.am b/telepathy-glib/Makefile.am
index f701a69..26c49bd 100644
--- a/telepathy-glib/Makefile.am
+++ b/telepathy-glib/Makefile.am
@@ -174,7 +174,8 @@ tpginclude_HEADERS = \
     svc-media-interfaces.h \
     svc-properties-interface.h \
     text-mixin.h \
-    util.h
+    util.h \
+    verify.h
 
 nodist_libtelepathy_glib_internal_la_SOURCES = \
     _gen/error-str.c \
diff --git a/telepathy-glib/util.c b/telepathy-glib/util.c
index 62ac663..cc5699f 100644
--- a/telepathy-glib/util.c
+++ b/telepathy-glib/util.c
@@ -32,6 +32,38 @@
 
 #include <string.h>
 
+/**
+ * tp_verify:
+ * @R: a requirement (constant expression) to be checked at compile-time
+ *
+ * Make an assertion at compile time, like C++0x's proposed static_assert
+ * keyword. If @R is determined to be true, there is no overhead at runtime;
+ * if @R is determined to be false, compilation will fail.
+ *
+ * This macro can be used at file scope (it expands to a dummy extern
+ * declaration).
+ *
+ * (This is gnulib's verify macro, written by Paul Eggert, Bruno Haible and
+ * Jim Meyering.)
+ */
+
+/**
+ * tp_verify_true:
+ * @R: a requirement (constant expression) to be checked at compile-time
+ *
+ * Make an assertion at compile time, like C++0x's proposed static_assert
+ * keyword. If @R is determined to be true, there is no overhead at runtime,
+ * and the macro evaluates to 1 as an integer constant expression;
+ * if @R is determined to be false, compilation will fail.
+ *
+ * This macro can be used anywhere that an integer constant expression would
+ * be allowed.
+ *
+ * (This is gnulib's verify_true macro, written by Paul Eggert, Bruno Haible
+ * and Jim Meyering.)
+ *
+ * Returns: 1
+ */
 
 /**
  * tp_g_ptr_array_contains:
diff --git a/telepathy-glib/util.h b/telepathy-glib/util.h
index f06d099..ce64857 100644
--- a/telepathy-glib/util.h
+++ b/telepathy-glib/util.h
@@ -20,10 +20,13 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#include <glib-object.h>
-
 #ifndef __TP_UTIL_H__
 #define __TP_UTIL_H__
+#define __TP_IN_UTIL_H__
+
+#include <glib-object.h>
+
+#include <telepathy-glib/verify.h>
 
 G_BEGIN_DECLS
 
@@ -70,4 +73,5 @@ guint64 tp_g_key_file_get_uint64 (GKeyFile *key_file, const gchar *group_name,
 
 G_END_DECLS
 
+#undef  __TP_IN_UTIL_H__
 #endif /* __TP_UTIL_H__ */
diff --git a/telepathy-glib/verify.h b/telepathy-glib/verify.h
new file mode 100644
index 0000000..d46a460
--- /dev/null
+++ b/telepathy-glib/verify.h
@@ -0,0 +1,146 @@
+/* Compile-time assert-like macros.
+
+   Imported from gnulib 20090701 and adapted to Telepathy namespace.
+
+   Copyright (C) 2005, 2006 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as published by
+   the Free Software Foundation; either version 2.1 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Paul Eggert, Bruno Haible, and Jim Meyering.  */
+
+#ifndef __TP_IN_UTIL_H__
+#error Not to be used directly, #include <telepathy-glib/util.h> instead
+#endif
+
+#ifndef TP_VERIFY_H
+# define TP_VERIFY_H 1
+
+/* Each of these macros verifies that its argument R is nonzero.  To
+   be portable, R should be an integer constant expression.  Unlike
+   assert (R), there is no run-time overhead.
+
+   There are two macros, since no single macro can be used in all
+   contexts in C.  verify_true (R) is for scalar contexts, including
+   integer constant expression contexts.  verify (R) is for declaration
+   contexts, e.g., the top level.
+
+   Symbols ending in "__" are private to this header.
+
+   The code below uses several ideas.
+
+   * The first step is ((R) ? 1 : -1).  Given an expression R, of
+     integral or boolean or floating-point type, this yields an
+     expression of integral type, whose value is later verified to be
+     constant and nonnegative.
+
+   * Next this expression W is wrapped in a type
+     struct verify_type__ { unsigned int verify_error_if_negative_size__: W; }.
+     If W is negative, this yields a compile-time error.  No compiler can
+     deal with a bit-field of negative size.
+
+     One might think that an array size check would have the same
+     effect, that is, that the type struct { unsigned int dummy[W]; }
+     would work as well.  However, inside a function, some compilers
+     (such as C++ compilers and GNU C) allow local parameters and
+     variables inside array size expressions.  With these compilers,
+     an array size check would not properly diagnose this misuse of
+     the verify macro:
+
+       void function (int n) { verify (n < 0); }
+
+   * For the verify macro, the struct verify_type__ will need to
+     somehow be embedded into a declaration.  To be portable, this
+     declaration must declare an object, a constant, a function, or a
+     typedef name.  If the declared entity uses the type directly,
+     such as in
+
+       struct dummy {...};
+       typedef struct {...} dummy;
+       extern struct {...} *dummy;
+       extern void dummy (struct {...} *);
+       extern struct {...} *dummy (void);
+
+     two uses of the verify macro would yield colliding declarations
+     if the entity names are not disambiguated.  A workaround is to
+     attach the current line number to the entity name:
+
+       #define GL_CONCAT0(x, y) x##y
+       #define GL_CONCAT(x, y) GL_CONCAT0 (x, y)
+       extern struct {...} * GL_CONCAT(dummy,__LINE__);
+
+     But this has the problem that two invocations of verify from
+     within the same macro would collide, since the __LINE__ value
+     would be the same for both invocations.
+
+     A solution is to use the sizeof operator.  It yields a number,
+     getting rid of the identity of the type.  Declarations like
+
+       extern int dummy [sizeof (struct {...})];
+       extern void dummy (int [sizeof (struct {...})]);
+       extern int (*dummy (void)) [sizeof (struct {...})];
+
+     can be repeated.
+
+   * Should the implementation use a named struct or an unnamed struct?
+     Which of the following alternatives can be used?
+
+       extern int dummy [sizeof (struct {...})];
+       extern int dummy [sizeof (struct verify_type__ {...})];
+       extern void dummy (int [sizeof (struct {...})]);
+       extern void dummy (int [sizeof (struct verify_type__ {...})]);
+       extern int (*dummy (void)) [sizeof (struct {...})];
+       extern int (*dummy (void)) [sizeof (struct verify_type__ {...})];
+
+     In the second and sixth case, the struct type is exported to the
+     outer scope; two such declarations therefore collide.  GCC warns
+     about the first, third, and fourth cases.  So the only remaining
+     possibility is the fifth case:
+
+       extern int (*dummy (void)) [sizeof (struct {...})];
+
+   * This implementation exploits the fact that GCC does not warn about
+     the last declaration mentioned above.  If a future version of GCC
+     introduces a warning for this, the problem could be worked around
+     by using code specialized to GCC, e.g.,:
+
+       #if 4 <= __GNUC__
+       # define verify(R) \
+	   extern int (* verify_function__ (void)) \
+		      [__builtin_constant_p (R) && (R) ? 1 : -1]
+       #endif
+
+   * In C++, any struct definition inside sizeof is invalid.
+     Use a template type to work around the problem.  */
+
+
+/* Verify requirement R at compile-time, as an integer constant expression.
+   Return 1.  */
+
+# ifdef __cplusplus
+template <int w>
+  struct tp_verify_type__ { unsigned int verify_error_if_negative_size__: w; };
+#  define tp_verify_true(R) \
+     (!!sizeof (tp_verify_type__<(R) ? 1 : -1>))
+# else
+#  define tp_verify_true(R) \
+     (!!sizeof \
+      (struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; }))
+# endif
+
+/* Verify requirement R at compile-time, as a declaration without a
+   trailing ';'.  */
+
+# define tp_verify(R) extern int (* tp_verify_function__ (void)) [tp_verify_true (R)]
+
+#endif
-- 
1.5.6.5




More information about the telepathy-commits mailing list