From owner-freebsd-current@FreeBSD.ORG Wed Oct 1 19:19:11 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 8536D10656A5 for ; Wed, 1 Oct 2008 19:19:11 +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 0454C8FC23 for ; Wed, 1 Oct 2008 19:19:10 +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 m91JJ422071550; Wed, 1 Oct 2008 15:19:04 -0400 (EDT) (envelope-from jhb@freebsd.org) From: John Baldwin To: freebsd-current@freebsd.org Date: Wed, 1 Oct 2008 11:18:28 -0400 User-Agent: KMail/1.9.7 References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200810011118.28474.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH authentication, not delayed by milter-greylist-2.0.2 (server.baldwin.cx [IPv6:::1]); Wed, 01 Oct 2008 15:19:04 -0400 (EDT) X-Virus-Scanned: ClamAV 0.93.1/8367/Wed Oct 1 12:39:43 2008 on server.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-2.4 required=4.2 tests=AWL,BAYES_00, DATE_IN_PAST_03_06,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: Wed, 01 Oct 2008 19:19:11 -0000 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. -- John Baldwin