From owner-freebsd-current@FreeBSD.ORG Tue Sep 30 20:23:33 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 EA06210656A6 for ; Tue, 30 Sep 2008 20:23:33 +0000 (UTC) (envelope-from rysto32@gmail.com) Received: from mail-gx0-f21.google.com (mail-gx0-f21.google.com [209.85.217.21]) by mx1.freebsd.org (Postfix) with ESMTP id A421C8FC0A for ; Tue, 30 Sep 2008 20:23:33 +0000 (UTC) (envelope-from rysto32@gmail.com) Received: by gxk14 with SMTP id 14so395159gxk.19 for ; Tue, 30 Sep 2008 13:23:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type:content-transfer-encoding :content-disposition; bh=9tpIx65SmgP09eLp7NWx1qoLVA04kgvsyxLj+p8WQ8Q=; b=ret5D+L+YjGDlfiOnmNEiF/3amhkgnbvsFvClV9ButTXwfcWgZ7iOR2dcn5EEYX5HA i3jdbwXQq7hb2exr5Tj9q8LZ2jWb5A8YJXSrCrxBrrYUzL/iTQD9rfwmE2TwUAJ9vsns cpb3l/aSU5OFkgETSBeNWyek/8kpEqjzspU30= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type :content-transfer-encoding:content-disposition; b=sFnsApsMr5QIjGcWQaUlDKiuzFwh02CKcQnURtIk3LTvwCrvv7bzWpzIPdmDg9SYZS XowgJ/6IMjRedXZvv7OpO9l/fRJ7JwIPIahfKdN9HNUEvfYx1iZcsApG+lgCsGbQbkfD 3/X5UkOseptSP5lnjkUZ0T9se9+VrDgtu2IcU= Received: by 10.150.124.2 with SMTP id w2mr10569158ybc.118.1222804526656; Tue, 30 Sep 2008 12:55:26 -0700 (PDT) Received: by 10.150.97.16 with HTTP; Tue, 30 Sep 2008 12:55:26 -0700 (PDT) Message-ID: Date: Tue, 30 Sep 2008 15:55:26 -0400 From: "Ryan Stone" To: freebsd-current@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Mailman-Approved-At: Tue, 30 Sep 2008 21:11:52 +0000 Subject: 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: Tue, 30 Sep 2008 20:23:34 -0000 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?