Merge branch 'nirbheek/static-assert-try-2' into 'master'

gmacros: Use _Static_assert only for non-expr static assert

See merge request GNOME/glib!962
This commit is contained in:
Emmanuele Bassi 2019-07-01 13:41:58 +00:00
commit a656555f76
2 changed files with 17 additions and 1 deletions

View File

@ -734,13 +734,19 @@
#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)
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define G_STATIC_ASSERT(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")
#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
#endif /* __STDC_VERSION__ */
#define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1]))
#endif
#endif /* !__GI_SCANNER__ */
/* Provide a string identifying the current code position */
#if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)

View File

@ -20,6 +20,15 @@
#include <glib.h>
/* Test that G_STATIC_ASSERT_EXPR can be used as an expression */
static void
test_assert_static (void)
{
G_STATIC_ASSERT (4 == 4);
if (G_STATIC_ASSERT_EXPR (1 == 1), sizeof (gchar) == 2)
g_assert_not_reached ();
}
/* Test G_ALIGNOF() gives the same results as the G_STRUCT_OFFSET fallback. This
* should be the minimal alignment for the given type.
*
@ -48,6 +57,7 @@ main (int argc,
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/alignof/fallback", test_alignof_fallback);
g_test_add_func ("/assert/static", test_assert_static);
return g_test_run ();
}