[glib/nirbheek/fix-g-static-assert-expr] gmacros: Don't use static_assert for G_STATIC_ASSERT_MSG



commit 1bdc76d46218208d8aad3a9169862f3f75dd5068
Author: Nirbheek Chauhan <nirbheek centricular com>
Date:   Sat Jun 29 15:30:51 2019 +0530

    gmacros: Don't use static_assert for G_STATIC_ASSERT_MSG
    
    You can't actually do that since it doesn't return an expression.
    
    Also add a test this time.

 glib/gmacros.h      | 9 +++++----
 glib/tests/macros.c | 6 ++++++
 2 files changed, 11 insertions(+), 4 deletions(-)
---
diff --git a/glib/gmacros.h b/glib/gmacros.h
index c101da6cd..c3a59c580 100644
--- a/glib/gmacros.h
+++ b/glib/gmacros.h
@@ -734,20 +734,21 @@
 #ifndef __GI_SCANNER__ /* The static assert macro really confuses the introspection parser */
 #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
 #define G_PASTE(identifier1,identifier2)      G_PASTE_ARGS (identifier1, identifier2)
+/* Use a more readable G_STATIC_ASSERT implementation when C11 or C++11 are available.
+ * Can't use it for G_STATIC_ASSERT_EXPR because it does not return an expression.
+ */
 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
 #define G_STATIC_ASSERT(expr) _Static_assert (expr, "Expression evaluates to false")
-#define G_STATIC_ASSERT_EXPR(expr) _Static_assert (expr, "Expression evaluates to false")
 #elif defined(__cplusplus) && __cplusplus >= 201103L
 #define G_STATIC_ASSERT(expr) static_assert (expr, "Expression evaluates to false")
-#define G_STATIC_ASSERT_EXPR(expr) static_assert (expr, "Expression evaluates to false")
 #else
 #ifdef __COUNTER__
 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __COUNTER__)[(expr) 
? 1 : -1] G_GNUC_UNUSED
 #else
 #define G_STATIC_ASSERT(expr) typedef char G_PASTE (_GStaticAssertCompileTimeAssertion_, __LINE__)[(expr) ? 
1 : -1] G_GNUC_UNUSED
-#endif
-#define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1]))
+#endif /* __COUNTER__ */
 #endif /* __STDC_VERSION__ */
+#define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1]))
 #endif /* !__GI_SCANNER__ */
 
 /* Provide a string identifying the current code position */
diff --git a/glib/tests/macros.c b/glib/tests/macros.c
index 7fc1d6086..ad30fd30d 100644
--- a/glib/tests/macros.c
+++ b/glib/tests/macros.c
@@ -20,6 +20,12 @@
 
 #include <glib.h>
 
+/* Test static assert macros */
+#if G_STATIC_ASSERT_EXPR (sizeof (char) == 1)
+G_STATIC_ASSERT (sizeof (gint) == 4);
+#endif
+
+
 /* Test G_ALIGNOF() gives the same results as the G_STRUCT_OFFSET fallback. This
  * should be the minimal alignment for the given type.
  *


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]