From 54c7f19254f3d50072b5b3c93b8ab009d9f6cc6b Mon Sep 17 00:00:00 2001
From: Thomas Hindoe Paaboel Andersen <phomes@gmail.com>
Date: Sun, 30 Dec 2012 21:58:53 +0100
Subject: [PATCH] use static_assert instead of assert_cc

Moved a few checks to avoid warnings about mixed declarations and code.
---
 TODO                      |  2 --
 src/core/dbus.c           |  2 +-
 src/core/execute.c        |  2 +-
 src/core/job.c            |  2 +-
 src/core/manager.c        |  2 +-
 src/core/mount-setup.c    |  2 +-
 src/journal/catalog.c     |  2 +-
 src/shared/capability.c   |  6 +++---
 src/shared/conf-parser.c  |  3 +--
 src/shared/dbus-common.c  | 30 +++++++++++++++---------------
 src/shared/hashmap.c      |  2 +-
 src/shared/macro.h        |  9 ---------
 src/shared/missing.h      |  4 ++++
 src/shared/util.h         | 16 ++++++++--------
 src/systemctl/systemctl.c |  3 ++-
 15 files changed, 40 insertions(+), 47 deletions(-)

diff --git a/TODO b/TODO
index 71bb07b..59e2864 100644
--- a/TODO
+++ b/TODO
@@ -39,8 +39,6 @@ Features:
   "dropped %u messages" not only when we are about to print the next
   message that works, but alraedy after a short tiemout
 
-* use C11 static_assert() where we currently use assert_cc()
-
 * journald: also get thread ID from client, plus thread name
 
 * check if we can make journalctl by default use --follow mode inside of less if called without args?
diff --git a/src/core/dbus.c b/src/core/dbus.c
index 2a1c660..6a050d1 100644
--- a/src/core/dbus.c
+++ b/src/core/dbus.c
@@ -1446,6 +1446,7 @@ void bus_broadcast_finished(
 
         DBusMessage *message;
 
+        static_assert(sizeof(usec_t) == sizeof(uint64_t), "'usec_t' must have same size as 'uint64_t'");
         assert(m);
 
         message = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartupFinished");
@@ -1454,7 +1455,6 @@ void bus_broadcast_finished(
                 return;
         }
 
-        assert_cc(sizeof(usec_t) == sizeof(uint64_t));
         if (!dbus_message_append_args(message,
                                       DBUS_TYPE_UINT64, &firmware_usec,
                                       DBUS_TYPE_UINT64, &loader_usec,
diff --git a/src/core/execute.c b/src/core/execute.c
index 7628470..bf71922 100644
--- a/src/core/execute.c
+++ b/src/core/execute.c
@@ -960,7 +960,7 @@ static int apply_seccomp(uint32_t *syscall_filter) {
                                 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
                         };
 
-                        assert_cc(ELEMENTSOF(item) == 2);
+                        static_assert(ELEMENTSOF(item) == 2, "item must have exactly 2 elements");
 
                         f[ELEMENTSOF(header) + 2*n]  = item[0];
                         f[ELEMENTSOF(header) + 2*n+1] = item[1];
diff --git a/src/core/job.c b/src/core/job.c
index f08b8cb..7071d4a 100644
--- a/src/core/job.c
+++ b/src/core/job.c
@@ -318,7 +318,7 @@ static const JobType job_merging_table[] = {
 };
 
 JobType job_type_lookup_merge(JobType a, JobType b) {
-        assert_cc(ELEMENTSOF(job_merging_table) == _JOB_TYPE_MAX_MERGING * (_JOB_TYPE_MAX_MERGING - 1) / 2);
+        static_assert(ELEMENTSOF(job_merging_table) == _JOB_TYPE_MAX_MERGING * (_JOB_TYPE_MAX_MERGING - 1) / 2, "JobType does not match job_merging_table");
         assert(a >= 0 && a < _JOB_TYPE_MAX_MERGING);
         assert(b >= 0 && b < _JOB_TYPE_MAX_MERGING);
 
diff --git a/src/core/manager.c b/src/core/manager.c
index a3839e1..e352f2f 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -140,6 +140,7 @@ static int manager_setup_time_change(Manager *m) {
         struct epoll_event ev;
         struct itimerspec its;
 
+        static_assert(sizeof(time_t) == sizeof(long), "'time_t' must have same size as 'long'");
         assert(m);
         assert(m->time_change_watch.type == WATCH_INVALID);
 
@@ -157,7 +158,6 @@ static int manager_setup_time_change(Manager *m) {
 
         /* We only care for the cancellation event, hence we set the
          * timeout to the latest possible value. */
-        assert_cc(sizeof(time_t) == sizeof(long));
         its.it_value.tv_sec = LONG_MAX;
 
         if (timerfd_settime(m->time_change_watch.fd, TFD_TIMER_ABSTIME|TFD_TIMER_CANCEL_ON_SET, &its, NULL) < 0) {
diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c
index 98614d0..5b414ae 100644
--- a/src/core/mount-setup.c
+++ b/src/core/mount-setup.c
@@ -183,7 +183,7 @@ int mount_setup_early(void) {
         unsigned i;
         int r = 0;
 
-        assert_cc(N_EARLY_MOUNT <= ELEMENTSOF(mount_table));
+        static_assert(N_EARLY_MOUNT <= ELEMENTSOF(mount_table), "N_EARLY_MOUNT must be within the bounds of the mount_table");
 
         /* Do a minimal mount of /proc and friends to enable the most
          * basic stuff, such as SELinux */
diff --git a/src/journal/catalog.c b/src/journal/catalog.c
index 3735ad9..0ad51b9 100644
--- a/src/journal/catalog.c
+++ b/src/journal/catalog.c
@@ -64,7 +64,7 @@ typedef struct CatalogItem {
 static unsigned catalog_hash_func(const void *p) {
         const CatalogItem *i = p;
 
-        assert_cc(sizeof(unsigned) == sizeof(uint8_t)*4);
+        static_assert(sizeof(unsigned) == sizeof(uint8_t)*4, "unsigned' must have same size as 4*'uint8_t'");
 
         return (((unsigned) i->id.bytes[0] << 24) |
                 ((unsigned) i->id.bytes[1] << 16) |
diff --git a/src/shared/capability.c b/src/shared/capability.c
index 9b743e8..10779b3 100644
--- a/src/shared/capability.c
+++ b/src/shared/capability.c
@@ -178,13 +178,13 @@ static int drop_from_file(const char *fn, uint64_t drop) {
         uint64_t current, after;
         char *p;
 
+        static_assert(sizeof(hi) == sizeof(unsigned), "'hi' (uint32_t) must have same size as 'unsigned'");
+        static_assert(sizeof(lo) == sizeof(unsigned), "'lo' (uint32_t) must have same size as 'unsigned'");
+
         r = read_one_line_file(fn, &p);
         if (r < 0)
                 return r;
 
-        assert_cc(sizeof(hi) == sizeof(unsigned));
-        assert_cc(sizeof(lo) == sizeof(unsigned));
-
         k = sscanf(p, "%u %u", &lo, &hi);
         free(p);
 
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
index 9f5c07c..1112387 100644
--- a/src/shared/conf-parser.c
+++ b/src/shared/conf-parser.c
@@ -503,13 +503,12 @@ int config_parse_bytes_off(
 
         off_t *bytes = data;
 
+        static_assert(sizeof(off_t) == sizeof(uint64_t), "'off_t' must have same size as 'uint64_t'");
         assert(filename);
         assert(lvalue);
         assert(rvalue);
         assert(data);
 
-        assert_cc(sizeof(off_t) == sizeof(uint64_t));
-
         if (parse_bytes(rvalue, bytes) < 0) {
                 log_error("[%s:%u] Failed to parse bytes value, ignoring: %s", filename, line, rvalue);
                 return 0;
diff --git a/src/shared/dbus-common.c b/src/shared/dbus-common.c
index e9a78c2..e0d6a4a 100644
--- a/src/shared/dbus-common.c
+++ b/src/shared/dbus-common.c
@@ -572,14 +572,14 @@ int bus_property_append_tristate_false(DBusMessageIter *i, const char *property,
 }
 
 int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *data) {
+        /* Let's ensure that usec_t is actually 64bit, and hence this
+         * function can be used for usec_t */
+        static_assert(sizeof(uint64_t) == sizeof(usec_t), "'uint64_t' must have same size as 'usec_t'");
+
         assert(i);
         assert(property);
         assert(data);
 
-        /* Let's ensure that usec_t is actually 64bit, and hence this
-         * function can be used for usec_t */
-        assert_cc(sizeof(uint64_t) == sizeof(usec_t));
-
         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
                 return -ENOMEM;
 
@@ -587,18 +587,18 @@ int bus_property_append_uint64(DBusMessageIter *i, const char *property, void *d
 }
 
 int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *data) {
-        assert(i);
-        assert(property);
-        assert(data);
-
         /* Let's ensure that pid_t, mode_t, uid_t, gid_t are actually
          * 32bit, and hence this function can be used for
          * pid_t/mode_t/uid_t/gid_t */
-        assert_cc(sizeof(uint32_t) == sizeof(pid_t));
-        assert_cc(sizeof(uint32_t) == sizeof(mode_t));
-        assert_cc(sizeof(uint32_t) == sizeof(unsigned));
-        assert_cc(sizeof(uint32_t) == sizeof(uid_t));
-        assert_cc(sizeof(uint32_t) == sizeof(gid_t));
+        static_assert(sizeof(uint32_t) == sizeof(pid_t), "'uint32_t' must have same size as 'pid_t'");
+        static_assert(sizeof(uint32_t) == sizeof(mode_t), "'uint32_t' must have same size as 'mode_t'");
+        static_assert(sizeof(uint32_t) == sizeof(unsigned), "'uint32_t' must have same size as 'unsigned'");
+        static_assert(sizeof(uint32_t) == sizeof(uid_t), "'uint32_t' must have same size as 'uid_t'");
+        static_assert(sizeof(uint32_t) == sizeof(gid_t), "'uint32_t' must have same size as 'gid_t'");
+
+        assert(i);
+        assert(property);
+        assert(data);
 
         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
                 return -ENOMEM;
@@ -607,12 +607,12 @@ int bus_property_append_uint32(DBusMessageIter *i, const char *property, void *d
 }
 
 int bus_property_append_int32(DBusMessageIter *i, const char *property, void *data) {
+        static_assert(sizeof(int32_t) == sizeof(int), "'int32_t' must have same size as 'int'");
+
         assert(i);
         assert(property);
         assert(data);
 
-        assert_cc(sizeof(int32_t) == sizeof(int));
-
         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
                 return -ENOMEM;
 
diff --git a/src/shared/hashmap.c b/src/shared/hashmap.c
index dcfbb67..8bd679e 100644
--- a/src/shared/hashmap.c
+++ b/src/shared/hashmap.c
@@ -150,7 +150,7 @@ int trivial_compare_func(const void *a, const void *b) {
 unsigned uint64_hash_func(const void *p) {
         uint64_t u;
 
-        assert_cc(sizeof(uint64_t) == 2*sizeof(unsigned));
+        static_assert(sizeof(uint64_t) == 2*sizeof(unsigned), "'uint64_t' must have same size as 2*'unsigned'");
 
         u = *(const uint64_t*) p;
 
diff --git a/src/shared/macro.h b/src/shared/macro.h
index e930fda..976471d 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -119,15 +119,6 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
                 log_assert_failed_unreachable(t, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
         } while (false)
 
-#define assert_cc(expr)                            \
-        do {                                       \
-                switch (0) {                       \
-                        case 0:                    \
-                        case !!(expr):             \
-                                ;                  \
-                }                                  \
-        } while (false)
-
 #define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
 #define UINT_TO_PTR(u) ((void*) ((uintptr_t) (u)))
 
diff --git a/src/shared/missing.h b/src/shared/missing.h
index c53579f..9067443 100644
--- a/src/shared/missing.h
+++ b/src/shared/missing.h
@@ -257,3 +257,7 @@ static inline int name_to_handle_at(int fd, const char *name, struct file_handle
 #ifndef TFD_TIMER_CANCEL_ON_SET
 #define TFD_TIMER_CANCEL_ON_SET (1 << 1)
 #endif
+
+#ifndef static_assert
+#define static_assert _Static_assert
+#endif
diff --git a/src/shared/util.h b/src/shared/util.h
index 25b349a..8926cda 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -124,41 +124,41 @@ int safe_atolli(const char *s, long long int *ret_i);
 
 #if __WORDSIZE == 32
 static inline int safe_atolu(const char *s, unsigned long *ret_u) {
-        assert_cc(sizeof(unsigned long) == sizeof(unsigned));
+        static_assert(sizeof(unsigned long) == sizeof(unsigned), "'unsigned long' must have same size as 'unsigned'");
         return safe_atou(s, (unsigned*) ret_u);
 }
 static inline int safe_atoli(const char *s, long int *ret_u) {
-        assert_cc(sizeof(long int) == sizeof(int));
+        static_assert(sizeof(long int) == sizeof(int), "'long int' must have same size as 'int'");
         return safe_atoi(s, (int*) ret_u);
 }
 #else
 static inline int safe_atolu(const char *s, unsigned long *ret_u) {
-        assert_cc(sizeof(unsigned long) == sizeof(unsigned long long));
+        static_assert(sizeof(unsigned long) == sizeof(unsigned long long), "'unsigned long' must have same size as 'unsigned long long'");
         return safe_atollu(s, (unsigned long long*) ret_u);
 }
 static inline int safe_atoli(const char *s, long int *ret_u) {
-        assert_cc(sizeof(long int) == sizeof(long long int));
+        static_assert(sizeof(long int) == sizeof(long long int), "'long int' must have same size as 'long long int'");
         return safe_atolli(s, (long long int*) ret_u);
 }
 #endif
 
 static inline int safe_atou32(const char *s, uint32_t *ret_u) {
-        assert_cc(sizeof(uint32_t) == sizeof(unsigned));
+        static_assert(sizeof(uint32_t) == sizeof(unsigned), "'uint32_t' must have same size as 'unsigned'");
         return safe_atou(s, (unsigned*) ret_u);
 }
 
 static inline int safe_atoi32(const char *s, int32_t *ret_i) {
-        assert_cc(sizeof(int32_t) == sizeof(int));
+        static_assert(sizeof(int32_t) == sizeof(int), "'int32_t' must have same size as 'int'");
         return safe_atoi(s, (int*) ret_i);
 }
 
 static inline int safe_atou64(const char *s, uint64_t *ret_u) {
-        assert_cc(sizeof(uint64_t) == sizeof(unsigned long long));
+        static_assert(sizeof(uint64_t) == sizeof(unsigned long long), "'uint64_t' must have same size as 'unsigned long long'");
         return safe_atollu(s, (unsigned long long*) ret_u);
 }
 
 static inline int safe_atoi64(const char *s, int64_t *ret_i) {
-        assert_cc(sizeof(int64_t) == sizeof(long long int));
+        static_assert(sizeof(int64_t) == sizeof(long long int), "'int64_t' must have same size as 'long long int'");
         return safe_atolli(s, (long long int*) ret_i);
 }
 
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 086872c..885cb09 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -1072,12 +1072,13 @@ static int cancel_job(DBusConnection *bus, char **args) {
                 unsigned id;
                 const char *path;
 
+                static_assert(sizeof(uint32_t) == sizeof(id), "'uint32_t' must have same size as 'id' (unsigned)");
+
                 r = safe_atou(*name, &id);
                 if (r < 0) {
                         log_error("Failed to parse job id: %s", strerror(-r));
                         goto finish;
                 }
-                assert_cc(sizeof(uint32_t) == sizeof(id));
 
                 r = bus_method_call_with_reply (
                                 bus,
-- 
1.8.0.2

