Mesa (master): mesa: add STATIC_ASSERT() macro for compile-time assertions

Brian Paul brianp at kemper.freedesktop.org
Wed Nov 30 14:21:17 UTC 2011


Module: Mesa
Branch: master
Commit: 15be8b4bf01a4f01b2afcea82bd042f3a98b9ac1
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=15be8b4bf01a4f01b2afcea82bd042f3a98b9ac1

Author: Brian Paul <brianp at vmware.com>
Date:   Mon Nov 28 18:13:43 2011 -0700

mesa: add STATIC_ASSERT() macro for compile-time assertions

This can be used to check that tables have the right number of entries,
etc. at compile-time.  This will hopefully catch things that are missed
if particular drivers aren't tested, for example.

v2: Simplify the macro to omit the extra line number info (the compiler
already indicates the line number).  And wrap the macro for readability.

---

 src/mesa/main/compiler.h |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/src/mesa/main/compiler.h b/src/mesa/main/compiler.h
index 921e302..25d9810 100644
--- a/src/mesa/main/compiler.h
+++ b/src/mesa/main/compiler.h
@@ -310,6 +310,18 @@ static INLINE GLuint CPU_TO_LE32(GLuint x)
 #endif
 #endif
 
+
+/**
+ * Static (compile-time) assertion.
+ * Basically, use COND to dimension an array.  If COND is false/zero the
+ * array size will be -1 and we'll get a compilation error.
+ */
+#define STATIC_ASSERT(COND) \
+   do { \
+      typedef int static_assertion_failed[(!!(COND))*2-1]; \
+   } while (0)
+
+
 #if (__GNUC__ >= 3)
 #define PRINTFLIKE(f, a) __attribute__ ((format(__printf__, f, a)))
 #else




More information about the mesa-commit mailing list