bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gnulib] new gnulib module "verify" for compile-time assertions


From: Bruno Haible
Subject: Re: [bug-gnulib] new gnulib module "verify" for compile-time assertions
Date: Fri, 23 Sep 2005 11:47:07 +0200
User-agent: KMail/1.5

Paul Eggert wrote:
> The first step is to add a new module "verify", which defines macros
> verify(EXPR) and verify_expr(EXPR) that act like assert(EXPR) except
> they check EXPR at compile-time, not at run-time.  verify(EXPR) is for
> declaration contexts, and verify_expr is for expression contexts.

Thanks a lot for making this generally available!

At first sight, using a bitfield width instead of an array size, to
avoid multiple-definition errors, seems awfully cool. But it does not
work:

$ cat foo.c
#include "verify.h"
verify (1 + 1 == 2); verify (1 + 1 == 2);
$ gcc -O -c foo.c
foo.c:2: conflicting types for `verify_function_2'
foo.c:2: previous declaration of `verify_function_2'

Here's a fix that makes this work. OK to commit?

Bruno


diff -c -3 -r1.1 verify.h
*** verify.h    22 Sep 2005 22:56:29 -0000      1.1
--- verify.h    23 Sep 2005 09:48:54 -0000
***************
*** 16,22 ****
     along with this program; if not, write to the Free Software Foundation,
     Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
  
! /* Written by Paul Eggert and Jim Meyering.  */
  
  #ifndef VERIFY_H
  # define VERIFY_H 1
--- 16,22 ----
     along with this program; if not, write to the Free Software Foundation,
     Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
  
! /* Written by Paul Eggert, Jim Meyering, Bruno Haible.  */
  
  #ifndef VERIFY_H
  # define VERIFY_H 1
***************
*** 26,36 ****
  
  /* A type that is valid if and only if R is nonzero.
     R should be an integer constant expression.
!    verify_type__ and verify_error_if_negative_size__ are symbols that
!    are private to this header file.  */
  
! # define verify_type__(R) \
!     struct { int verify_error_if_negative_size__ : (R) ? 1 : -1; }
  
  /* Verify requirement R at compile-time, as a declaration.
     R should be an integer constant expression.
--- 26,36 ----
  
  /* A type that is valid if and only if R is nonzero.
     R should be an integer constant expression.
!    verify_array_type__ and verify_pointer_type__ are symbols that are
!    private to this header file.  */
  
! # define verify_array_type__(R) int [(R) ? 1 : -1]
! # define verify_pointer_type__(R) int (*) [(R) ? 1 : -1]
  
  /* Verify requirement R at compile-time, as a declaration.
     R should be an integer constant expression.
***************
*** 44,50 ****
     no conforming compiler will reject them outright.  */
  
  # define verify(R) \
!     extern verify_type__ (R) GL_CONCAT (verify_function_, __LINE__) (void)
  
  /* Verify requirement R at compile-time, as an expression.
     R should be an integer constant expression.
--- 44,50 ----
     no conforming compiler will reject them outright.  */
  
  # define verify(R) \
!     extern void GL_CONCAT (verify_function_, __LINE__) (verify_array_type__ 
(R))
  
  /* Verify requirement R at compile-time, as an expression.
     R should be an integer constant expression.
***************
*** 52,57 ****
     This macro can be used in some contexts where verify cannot, and vice 
versa.
     Return void.  */
  
! # define verify_expr(R) ((void) ((verify_type__ (R) *) 0))
  
  #endif
--- 52,57 ----
     This macro can be used in some contexts where verify cannot, and vice 
versa.
     Return void.  */
  
! # define verify_expr(R) ((void) ((verify_pointer_type__ (R)) 0))
  
  #endif





reply via email to

[Prev in Thread] Current Thread [Next in Thread]