On Fri, 2013-11-29 at 02:39 +0100, Lennart Poettering wrote:
> On Thu, 28.11.13 00:54, Zbigniew Jędrzejewski-Szmek 
> (zbys...@kemper.freedesktop.org) wrote:

> Instead we should add some code to macro.h which turns off the the
> warning with the #pragma stuff only if it detects it is being run on an
> old gcc.

There's also this approach:


>From 7affb075dd1889fbb6b8d8865dec4b5e1d36448f Mon Sep 17 00:00:00 2001
From: Colin Walters <walt...@verbum.org>
Date: Fri, 29 Nov 2013 14:43:45 -0500
Subject: [PATCH] macro: Split assert_cc, add assert_cc_toplevel

To suppress warnings about -Wdeclaration-after-statement, we need to
wrap static asserts inside functions with a standard do {} while(0)
block.  But the toplevel asserts can't have that, so add
assert_cc_toplevel for those.
---
 src/libsystemd-bus/bus-util.h |   12 ++++++------
 src/shared/macro.h            |   10 ++++++++--
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/libsystemd-bus/bus-util.h b/src/libsystemd-bus/bus-util.h
index 9d49237..ac37793 100644
--- a/src/libsystemd-bus/bus-util.h
+++ b/src/libsystemd-bus/bus-util.h
@@ -80,10 +80,10 @@ int bus_property_get_bool(sd_bus *bus, const char *path, const char *interface,
 #define bus_property_get_usec ((sd_bus_property_get_t) NULL)
 #define bus_property_set_usec ((sd_bus_property_set_t) NULL)
 
-assert_cc(sizeof(int) == sizeof(int32_t));
+assert_cc_toplevel(sizeof(int) == sizeof(int32_t));
 #define bus_property_get_int ((sd_bus_property_get_t) NULL)
 
-assert_cc(sizeof(unsigned) == sizeof(unsigned));
+assert_cc_toplevel(sizeof(unsigned) == sizeof(unsigned));
 #define bus_property_get_unsigned ((sd_bus_property_get_t) NULL)
 
 /* On 64bit machines we can use the default serializer for size_t and
@@ -105,16 +105,16 @@ int bus_property_get_ulong(sd_bus *bus, const char *path, const char *interface,
 /* uid_t and friends on Linux 32 bit. This means we can just use the
  * default serializer for 32bit unsigned, for serializing it, and map
  * it to NULL here */
-assert_cc(sizeof(uid_t) == sizeof(uint32_t));
+assert_cc_toplevel(sizeof(uid_t) == sizeof(uint32_t));
 #define bus_property_get_uid ((sd_bus_property_get_t) NULL)
 
-assert_cc(sizeof(gid_t) == sizeof(uint32_t));
+assert_cc_toplevel(sizeof(gid_t) == sizeof(uint32_t));
 #define bus_property_get_gid ((sd_bus_property_get_t) NULL)
 
-assert_cc(sizeof(pid_t) == sizeof(uint32_t));
+assert_cc_toplevel(sizeof(pid_t) == sizeof(uint32_t));
 #define bus_property_get_pid ((sd_bus_property_get_t) NULL)
 
-assert_cc(sizeof(mode_t) == sizeof(uint32_t));
+assert_cc_toplevel(sizeof(mode_t) == sizeof(uint32_t));
 #define bus_property_get_mode ((sd_bus_property_get_t) NULL)
 
 int bus_log_parse_error(int r);
diff --git a/src/shared/macro.h b/src/shared/macro.h
index 6caecab..eef34f6 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -148,9 +148,15 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
         } while (false)
 
 #if defined(static_assert)
-#define assert_cc(expr) static_assert(expr, #expr)
+#define assert_cc_toplevel(expr) static_assert(expr, #expr)
 #else
-#define assert_cc(expr) struct UNIQUE(_assert_struct_) { char x[(expr) ? 0 : -1]; };
+#define assert_cc_toplevel(expr) struct UNIQUE(_assert_struct_) { char x[(expr) ? 0 : -1]; }
+#endif
+
+#if defined(static_assert)
+#define assert_cc(expr) do { static_assert(expr, #expr); } while (false)
+#else
+#define assert_cc(expr) do { struct UNIQUE(_assert_struct_) { char x[(expr) ? 0 : -1]; }; } while (false)
 #endif
 
 #define assert_return(expr, r)                    \
-- 
1.7.1

_______________________________________________
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel

Reply via email to