From owner-freebsd-current@FreeBSD.ORG Thu Oct 2 18:30:28 2008 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id AE5BB106568A for ; Thu, 2 Oct 2008 18:30:28 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from server.baldwin.cx (bigknife-pt.tunnel.tserv9.chi1.ipv6.he.net [IPv6:2001:470:1f10:75::2]) by mx1.freebsd.org (Postfix) with ESMTP id 4336A8FC28 for ; Thu, 2 Oct 2008 18:30:28 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from localhost.corp.yahoo.com (john@localhost [IPv6:::1]) (authenticated bits=0) by server.baldwin.cx (8.14.2/8.14.2) with ESMTP id m92IULIp083774; Thu, 2 Oct 2008 14:30:22 -0400 (EDT) (envelope-from jhb@freebsd.org) From: John Baldwin To: freebsd-current@freebsd.org Date: Thu, 2 Oct 2008 12:02:59 -0400 User-Agent: KMail/1.9.7 References: <200810011118.28474.jhb@freebsd.org> In-Reply-To: <200810011118.28474.jhb@freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200810021202.59271.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH authentication, not delayed by milter-greylist-2.0.2 (server.baldwin.cx [IPv6:::1]); Thu, 02 Oct 2008 14:30:22 -0400 (EDT) X-Virus-Scanned: ClamAV 0.93.1/8372/Thu Oct 2 11:21:47 2008 on server.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-2.6 required=4.2 tests=AWL,BAYES_00,NO_RELAYS autolearn=ham version=3.1.3 X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on server.baldwin.cx Cc: Ryan Stone Subject: Re: Possible alternate definition of CTASSERT to allow its use in header files X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Oct 2008 18:30:28 -0000 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