Re: Compile time assert macro

2013-05-17 Thread Stefan Fritsch
On Fri, 3 May 2013, Stefan Fritsch wrote:
 I think a CTASSERT macro like in FreeBSD would be nice. It could be used 
 to verify assumptions about type and struct sizes.

Ariane suggested making the syntax compatible to the C11 variant.

OKs for this version?

--- a/sys/lib/libkern/libkern.h
+++ b/sys/lib/libkern/libkern.h
@@ -138,6 +138,12 @@ abs(int j)
 #endif
 #endif
 
+#if __STDC_VERSION__  201112L
+#define_Static_assert(predicate, msg)  \
+   extern char __static_assert[(predicate) ? 1 : -1 ]  \
+   __attribute__((__unused__))
+#endif
+
 /* Prototypes for non-quad routines. */
 void__assert(const char *, const char *, int, const char *)
__attribute__ ((__noreturn__));
--- a/share/man/man9/kern.9
+++ b/share/man/man9/kern.9
@@ -108,6 +108,20 @@ enabled.
 tests are only included if the kernel has
 .Dv DEBUG
 enabled.
+.Pp
+.nr nS 1
+.Fn _Static_assert CONDITION MESSAGE
+.nr nS 0
+.Pp
+This is a standard feature in C11 and is implemented as a macro if compiling
+with a pre-C11 compiler.
+It causes a compile time error if the given condition evaluates to
+false.
+Its main purpose is to verify assertions about type and struct sizes that
+would otherwise make the code fail at run time.
+.Fn _Static_assert
+can be used in global scope or at the start of blocks, where variable
+declarations are allowed.
 .Sh BYTE STRINGS
 .nr nS 1
 .Ft int



Re: Compile time assert macro

2013-05-17 Thread Mark Kettenis
 Date: Fri, 17 May 2013 13:24:45 +0200 (CEST)
 From: Stefan Fritsch s...@sfritsch.de
 
 On Fri, 3 May 2013, Stefan Fritsch wrote:
  I think a CTASSERT macro like in FreeBSD would be nice. It could be used 
  to verify assumptions about type and struct sizes.
 
 Ariane suggested making the syntax compatible to the C11 variant.

This makes switching between KASSERT and CTASSERT painful.  And you
have to be creative and add a message string, which then isn't used...
NetBSD has CTASSERT as well.  I like CTASSERT better.  Our kernel is
not written in C11.

 --- a/sys/lib/libkern/libkern.h
 +++ b/sys/lib/libkern/libkern.h
 @@ -138,6 +138,12 @@ abs(int j)
  #endif
  #endif
  
 +#if __STDC_VERSION__  201112L
 +#define  _Static_assert(predicate, msg)  \
 + extern char __static_assert[(predicate) ? 1 : -1 ]  \
 + __attribute__((__unused__))
 +#endif
 +
  /* Prototypes for non-quad routines. */
  void  __assert(const char *, const char *, int, const char *)
   __attribute__ ((__noreturn__));
 --- a/share/man/man9/kern.9
 +++ b/share/man/man9/kern.9
 @@ -108,6 +108,20 @@ enabled.
  tests are only included if the kernel has
  .Dv DEBUG
  enabled.
 +.Pp
 +.nr nS 1
 +.Fn _Static_assert CONDITION MESSAGE
 +.nr nS 0
 +.Pp
 +This is a standard feature in C11 and is implemented as a macro if compiling
 +with a pre-C11 compiler.
 +It causes a compile time error if the given condition evaluates to
 +false.
 +Its main purpose is to verify assertions about type and struct sizes that
 +would otherwise make the code fail at run time.
 +.Fn _Static_assert
 +can be used in global scope or at the start of blocks, where variable
 +declarations are allowed.
  .Sh BYTE STRINGS
  .nr nS 1
  .Ft int
 
 



Compile time assert macro

2013-05-03 Thread Stefan Fritsch
Hi,

I think a CTASSERT macro like in FreeBSD would be nice. It could be used 
to verify assumptions about type and struct sizes.

This should work on gcc 2.9.5, too, but I haven't tested it.

--- sys/lib/libkern/libkern.h
+++ sys/lib/libkern/libkern.h
@@ -138,6 +138,9 @@ abs(int j)
 #endif
 #endif
 
+#defineCTASSERT(x) extern char  _ctassert[(x) ? 1 : -1 ]   \
+   __attribute__((__unused__))
+
 /* Prototypes for non-quad routines. */
 void__assert(const char *, const char *, int, const char *)
__attribute__ ((__noreturn__));