Re: Possible alternate definition of CTASSERT to allow its use in header files

From: John Baldwin <jhb_at_freebsd.org>
Date: Thu, 2 Oct 2008 12:02:59 -0400
On Wednesday 01 October 2008 11:18:28 am John Baldwin wrote:
> On Tuesday 30 September 2008 03:55:26 pm Ryan Stone wrote:
> > This was prompted by some recent check-ins removing CTASSERTs from
> > header files to prevent spurious errors from popping up.  For example,
> > this check-in:
> > http://lists.freebsd.org/pipermail/cvs-src/2008-September/095328.html
> > 
> > I've come up with an alternate definition of CTASSERT that can be used
> > in header files.  It works on gcc 3.4.6, 4.0.2 and 4.3.0(the only
> > compilers I have quick access to).
> > 
> > $ cat /tmp/tmp.c
> > // New definition
> > #define NEWASSERT(x) _NEWASSERT(x, __LINE__)
> > #define _NEWASSERT(x, line) __NEWASSERT(x, line)
> > #define __NEWASSERT(x, line) extern int __assert_ ## line [ x ? 1 : -1 ];
> > 
> > //existing BSD implementation
> > #define CTASSERT(x)             _CTASSERT(x, __LINE__)
> > #define _CTASSERT(x, y)         __CTASSERT(x, y)
> > #define __CTASSERT(x, y)        typedef char __assert ## y[(x) ? 1 : -1]
> > 
> > CTASSERT(1);                                      // line 11
> > CTASSERT(0);                                      // line 12
> > CTASSERT(1); CTASSERT(0);                // line 13
> > 
> > 
> > NEWASSERT(1);                                   // line 16
> > NEWASSERT(0) ;                                  // line 17
> > NEWASSERT(1); NEWASSERT(0);        // line 18
> > NEWASSERT(1); NEWASSERT(1);        // line 19
> > 
> > 
> > $ gcc -v -c /tmp/tmp.c -Wall -Werror
> > /tmp/tmp.c:12: error: size of array `__assert12' is negative
> > /tmp/tmp.c:13: error: size of array `__assert13' is negative
> > /tmp/tmp.c:13: error: redefinition of typedef '__assert13'
> > /tmp/tmp.c:13: error: previous declaration of '__assert13' was here
> > /tmp/tmp.c:17: error: size of array `__assert_17' is negative
> > /tmp/tmp.c:18: error: size of array `__assert_18' is negative
> > $
> > 
> > Note that the compiler doesn't complain about multiple definitions of
> > __assert18 and __assert19 like it does about the multiple definitions
> > of __assert13, which is the reason that CTASSERTs can't be used in
> > header files.
> > 
> > Thoughts?  Will this work on compilers other than gcc?
> 
> I think this is quite slick actually.  I'm not sure this is standard C 
though.  
> For the kernel it is probably fine so long as icc handles it.

My bad, I thought you replaced 'extern int' with 'typedef'.  Multiple extern's 
should be quite acceptable I believe.

-- 
John Baldwin
Received on Thu Oct 02 2008 - 16:30:28 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:36 UTC