From 5adfa09fce2c61239f9a7fa5fb154282f802af0a Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sun, 17 Jun 2007 18:22:39 +0000 Subject: r13477@catbus: nickm | 2007-06-17 14:22:03 -0400 Sun CC likes to give warnings for the do { } while(0) construction for making statement-like macros. Define STMT_BEGIN/STMT_END macros that do the right thing, and use them everywhere. svn:r10645 --- src/common/compat.h | 21 ++++++++++++++++++--- src/common/container.c | 4 ++-- src/common/container.h | 8 ++++---- src/common/ht.h | 4 +--- src/common/log.h | 4 ++-- src/common/test.h | 3 --- src/common/util.h | 12 ++++++------ 7 files changed, 33 insertions(+), 23 deletions(-) (limited to 'src/common') diff --git a/src/common/compat.h b/src/common/compat.h index 62612b72c6..fe07d3254b 100644 --- a/src/common/compat.h +++ b/src/common/compat.h @@ -120,6 +120,21 @@ extern INLINE double U64_TO_DBL(uint64_t x) { #define PREDICT_UNLIKELY(exp) (exp) #endif +/* Ways to declare macros. */ +#define STMT_NIL (void)0 +#ifdef __GNUC__ +#define STMT_BEGIN (void) ({ +#define STMT_END }) +#else +#if defined(sun) || defined(__sun__) +#define STMT_BEGIN if (1) { +#define STMT_END } else STMT_NIL +#else +#define STMT_BEGIN do { +#define STMT_END } while(0) +#endif +#endif + /* ===== String compatibility */ #ifdef MS_WINDOWS /* Windows names string functions differently from most other platforms. */ @@ -339,9 +354,9 @@ void tor_mutex_free(tor_mutex_t *m); unsigned long tor_get_thread_id(void); #else #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int))) -#define tor_mutex_acquire(m) do { } while (0) -#define tor_mutex_release(m) do { } while (0) -#define tor_mutex_free(m) do { tor_free(m); } while (0) +#define tor_mutex_acquire(m) STMT_NIL +#define tor_mutex_release(m) STMT_NIL +#define tor_mutex_free(m) STMT_BEGIN tor_free(m); STMT_END #define tor_get_thread_id() (1UL) #endif diff --git a/src/common/container.c b/src/common/container.c index f903a28300..4cac686147 100644 --- a/src/common/container.c +++ b/src/common/container.c @@ -665,8 +665,8 @@ smartlist_uniq_digests(smartlist_t *sl) HT_HEAD(prefix ## impl, prefix ## entry_t) head; \ } -DEFINE_MAP_STRUCTS(strmap_t, char *key, strmap_) -DEFINE_MAP_STRUCTS(digestmap_t, char key[DIGEST_LEN], digestmap_) +DEFINE_MAP_STRUCTS(strmap_t, char *key, strmap_); +DEFINE_MAP_STRUCTS(digestmap_t, char key[DIGEST_LEN], digestmap_); /** Helper: compare strmap_entry_t objects by key value. */ static INLINE int diff --git a/src/common/container.h b/src/common/container.h index 44ea81fa65..973bc38640 100644 --- a/src/common/container.h +++ b/src/common/container.h @@ -159,24 +159,24 @@ char *smartlist_join_strings2(smartlist_t *sl, const char *join, * */ #define SMARTLIST_FOREACH(sl, type, var, cmd) \ - do { \ + STMT_BEGIN \ int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \ type var; \ for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \ ++var ## _sl_idx) { \ var = (sl)->list[var ## _sl_idx]; \ cmd; \ - } } while (0) + } STMT_END /** Helper: While in a SMARTLIST_FOREACH loop over the list sl indexed * with the variable var, remove the current element in a way that * won't confuse the loop. */ #define SMARTLIST_DEL_CURRENT(sl, var) \ - do { \ + STMT_BEGIN \ smartlist_del(sl, var ## _sl_idx); \ --var ## _sl_idx; \ --var ## _sl_len; \ - } while (0); + STMT_END #define DECLARE_MAP_FNS(maptype, keytype, prefix) \ typedef struct maptype maptype; \ diff --git a/src/common/ht.h b/src/common/ht.h index 63629c3ac6..72fe5e2cb0 100644 --- a/src/common/ht.h +++ b/src/common/ht.h @@ -79,9 +79,7 @@ ht_string_hash(const char *s) } #define _HT_SET_HASH(elm, field, hashfn) \ - do { \ - (elm)->field.hte_hash = hashfn(elm); \ - } while (0) + (elm)->field.hte_hash = hashfn(elm) #define HT_FOREACH(x, name, head) \ for ((x) = HT_START(name, head); \ diff --git a/src/common/log.h b/src/common/log.h index f451599d00..01ab82de7e 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -131,10 +131,10 @@ void _log_fn(int severity, uint32_t domain, #define log_fn(severity, domain, args...) \ _log_fn(severity, domain, __PRETTY_FUNCTION__, args) #define log_debug(domain, args...) \ - do { \ + STMT_BEGIN \ if (PREDICT_UNLIKELY(_log_global_min_severity == LOG_DEBUG)) \ _log_fn(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args); \ - } while (0) + STMT_END #define log_info(domain, args...) \ _log_fn(LOG_INFO, domain, __PRETTY_FUNCTION__, args) #define log_notice(domain, args...) \ diff --git a/src/common/test.h b/src/common/test.h index 134c9a3f2a..6919376d26 100644 --- a/src/common/test.h +++ b/src/common/test.h @@ -16,9 +16,6 @@ #include #include "compat.h" -#define STMT_BEGIN do { -#define STMT_END } while (0) - #ifdef __GNUC__ #define PRETTY_FUNCTION __PRETTY_FUNCTION__ #else diff --git a/src/common/util.h b/src/common/util.h index 0f0902452f..0da98625c0 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -47,14 +47,14 @@ /** Like assert(3), but send assertion failures to the log as well as to * stderr. */ -#define tor_assert(expr) do { \ +#define tor_assert(expr) STMT_BEGIN \ if (PREDICT_UNLIKELY(IS_FALSE_AS_INT(expr))) { \ log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \ _SHORT_FILE_, __LINE__, __func__, #expr); \ fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \ _SHORT_FILE_, __LINE__, __func__, #expr); \ abort(); \ - } } while (0) + } STMT_END #endif #ifdef USE_DMALLOC @@ -83,19 +83,19 @@ void _tor_free(void *mem); #ifdef USE_DMALLOC extern int dmalloc_free(const char *file, const int line, void *pnt, const int func_id); -#define tor_free(p) do { \ +#define tor_free(p) STMT_BEGIN \ if (PREDICT_LIKELY((p)!=NULL)) { \ dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \ (p)=NULL; \ } \ - } while (0) + STMT_END #else -#define tor_free(p) do { \ +#define tor_free(p) STMT_BEGIN \ if (PREDICT_LIKELY((p)!=NULL)) { \ free(p); \ (p)=NULL; \ } \ - } while (0) + STMT_END #endif #define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS) -- cgit v1.2.3-54-g00ecf