Commit 5adfa09f authored by Nick Mathewson's avatar Nick Mathewson 🐻
Browse files

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
parent 93f32db4
...@@ -120,6 +120,21 @@ extern INLINE double U64_TO_DBL(uint64_t x) { ...@@ -120,6 +120,21 @@ extern INLINE double U64_TO_DBL(uint64_t x) {
#define PREDICT_UNLIKELY(exp) (exp) #define PREDICT_UNLIKELY(exp) (exp)
#endif #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 */ /* ===== String compatibility */
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
/* Windows names string functions differently from most other platforms. */ /* Windows names string functions differently from most other platforms. */
...@@ -339,9 +354,9 @@ void tor_mutex_free(tor_mutex_t *m); ...@@ -339,9 +354,9 @@ void tor_mutex_free(tor_mutex_t *m);
unsigned long tor_get_thread_id(void); unsigned long tor_get_thread_id(void);
#else #else
#define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int))) #define tor_mutex_new() ((tor_mutex_t*)tor_malloc(sizeof(int)))
#define tor_mutex_acquire(m) do { } while (0) #define tor_mutex_acquire(m) STMT_NIL
#define tor_mutex_release(m) do { } while (0) #define tor_mutex_release(m) STMT_NIL
#define tor_mutex_free(m) do { tor_free(m); } while (0) #define tor_mutex_free(m) STMT_BEGIN tor_free(m); STMT_END
#define tor_get_thread_id() (1UL) #define tor_get_thread_id() (1UL)
#endif #endif
......
...@@ -665,8 +665,8 @@ smartlist_uniq_digests(smartlist_t *sl) ...@@ -665,8 +665,8 @@ smartlist_uniq_digests(smartlist_t *sl)
HT_HEAD(prefix ## impl, prefix ## entry_t) head; \ HT_HEAD(prefix ## impl, prefix ## entry_t) head; \
} }
DEFINE_MAP_STRUCTS(strmap_t, char *key, strmap_) DEFINE_MAP_STRUCTS(strmap_t, char *key, strmap_);
DEFINE_MAP_STRUCTS(digestmap_t, char key[DIGEST_LEN], digestmap_) DEFINE_MAP_STRUCTS(digestmap_t, char key[DIGEST_LEN], digestmap_);
/** Helper: compare strmap_entry_t objects by key value. */ /** Helper: compare strmap_entry_t objects by key value. */
static INLINE int static INLINE int
......
...@@ -159,24 +159,24 @@ char *smartlist_join_strings2(smartlist_t *sl, const char *join, ...@@ -159,24 +159,24 @@ char *smartlist_join_strings2(smartlist_t *sl, const char *join,
* </pre> * </pre>
*/ */
#define SMARTLIST_FOREACH(sl, type, var, cmd) \ #define SMARTLIST_FOREACH(sl, type, var, cmd) \
do { \ STMT_BEGIN \
int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \ int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
type var; \ type var; \
for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \ for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
++var ## _sl_idx) { \ ++var ## _sl_idx) { \
var = (sl)->list[var ## _sl_idx]; \ var = (sl)->list[var ## _sl_idx]; \
cmd; \ cmd; \
} } while (0) } STMT_END
/** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
* with the variable <b>var</b>, remove the current element in a way that * with the variable <b>var</b>, remove the current element in a way that
* won't confuse the loop. */ * won't confuse the loop. */
#define SMARTLIST_DEL_CURRENT(sl, var) \ #define SMARTLIST_DEL_CURRENT(sl, var) \
do { \ STMT_BEGIN \
smartlist_del(sl, var ## _sl_idx); \ smartlist_del(sl, var ## _sl_idx); \
--var ## _sl_idx; \ --var ## _sl_idx; \
--var ## _sl_len; \ --var ## _sl_len; \
} while (0); STMT_END
#define DECLARE_MAP_FNS(maptype, keytype, prefix) \ #define DECLARE_MAP_FNS(maptype, keytype, prefix) \
typedef struct maptype maptype; \ typedef struct maptype maptype; \
......
...@@ -79,9 +79,7 @@ ht_string_hash(const char *s) ...@@ -79,9 +79,7 @@ ht_string_hash(const char *s)
} }
#define _HT_SET_HASH(elm, field, hashfn) \ #define _HT_SET_HASH(elm, field, hashfn) \
do { \ (elm)->field.hte_hash = hashfn(elm)
(elm)->field.hte_hash = hashfn(elm); \
} while (0)
#define HT_FOREACH(x, name, head) \ #define HT_FOREACH(x, name, head) \
for ((x) = HT_START(name, head); \ for ((x) = HT_START(name, head); \
......
...@@ -131,10 +131,10 @@ void _log_fn(int severity, uint32_t domain, ...@@ -131,10 +131,10 @@ void _log_fn(int severity, uint32_t domain,
#define log_fn(severity, domain, args...) \ #define log_fn(severity, domain, args...) \
_log_fn(severity, domain, __PRETTY_FUNCTION__, args) _log_fn(severity, domain, __PRETTY_FUNCTION__, args)
#define log_debug(domain, args...) \ #define log_debug(domain, args...) \
do { \ STMT_BEGIN \
if (PREDICT_UNLIKELY(_log_global_min_severity == LOG_DEBUG)) \ if (PREDICT_UNLIKELY(_log_global_min_severity == LOG_DEBUG)) \
_log_fn(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args); \ _log_fn(LOG_DEBUG, domain, __PRETTY_FUNCTION__, args); \
} while (0) STMT_END
#define log_info(domain, args...) \ #define log_info(domain, args...) \
_log_fn(LOG_INFO, domain, __PRETTY_FUNCTION__, args) _log_fn(LOG_INFO, domain, __PRETTY_FUNCTION__, args)
#define log_notice(domain, args...) \ #define log_notice(domain, args...) \
......
...@@ -16,9 +16,6 @@ ...@@ -16,9 +16,6 @@
#include <stdio.h> #include <stdio.h>
#include "compat.h" #include "compat.h"
#define STMT_BEGIN do {
#define STMT_END } while (0)
#ifdef __GNUC__ #ifdef __GNUC__
#define PRETTY_FUNCTION __PRETTY_FUNCTION__ #define PRETTY_FUNCTION __PRETTY_FUNCTION__
#else #else
......
...@@ -47,14 +47,14 @@ ...@@ -47,14 +47,14 @@
/** Like assert(3), but send assertion failures to the log as well as to /** Like assert(3), but send assertion failures to the log as well as to
* stderr. */ * stderr. */
#define tor_assert(expr) do { \ #define tor_assert(expr) STMT_BEGIN \
if (PREDICT_UNLIKELY(IS_FALSE_AS_INT(expr))) { \ if (PREDICT_UNLIKELY(IS_FALSE_AS_INT(expr))) { \
log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \ log(LOG_ERR, LD_BUG, "%s:%d: %s: Assertion %s failed; aborting.", \
_SHORT_FILE_, __LINE__, __func__, #expr); \ _SHORT_FILE_, __LINE__, __func__, #expr); \
fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \ fprintf(stderr,"%s:%d %s: Assertion %s failed; aborting.\n", \
_SHORT_FILE_, __LINE__, __func__, #expr); \ _SHORT_FILE_, __LINE__, __func__, #expr); \
abort(); \ abort(); \
} } while (0) } STMT_END
#endif #endif
#ifdef USE_DMALLOC #ifdef USE_DMALLOC
...@@ -83,19 +83,19 @@ void _tor_free(void *mem); ...@@ -83,19 +83,19 @@ void _tor_free(void *mem);
#ifdef USE_DMALLOC #ifdef USE_DMALLOC
extern int dmalloc_free(const char *file, const int line, void *pnt, extern int dmalloc_free(const char *file, const int line, void *pnt,
const int func_id); const int func_id);
#define tor_free(p) do { \ #define tor_free(p) STMT_BEGIN \
if (PREDICT_LIKELY((p)!=NULL)) { \ if (PREDICT_LIKELY((p)!=NULL)) { \
dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \ dmalloc_free(_SHORT_FILE_, __LINE__, (p), 0); \
(p)=NULL; \ (p)=NULL; \
} \ } \
} while (0) STMT_END
#else #else
#define tor_free(p) do { \ #define tor_free(p) STMT_BEGIN \
if (PREDICT_LIKELY((p)!=NULL)) { \ if (PREDICT_LIKELY((p)!=NULL)) { \
free(p); \ free(p); \
(p)=NULL; \ (p)=NULL; \
} \ } \
} while (0) STMT_END
#endif #endif
#define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS) #define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS)
......
...@@ -40,18 +40,21 @@ const char buffers_c_id[] = ...@@ -40,18 +40,21 @@ const char buffers_c_id[] =
/** Initialize the sentinel values on <b>m</b> (a value of buf-&gt;mem), which /** Initialize the sentinel values on <b>m</b> (a value of buf-&gt;mem), which
* has <b>ln</b> useful bytes. */ * has <b>ln</b> useful bytes. */
#define SET_GUARDS(m, ln) \ #define SET_GUARDS(m, ln) \
do { set_uint32((m)-4,START_MAGIC); set_uint32((m)+ln,END_MAGIC); } while (0) STMT_BEGIN \
set_uint32((m)-4,START_MAGIC); \
set_uint32((m)+ln,END_MAGIC); \
STMT_END
#else #else
#define RAW_MEM(m) (m) #define RAW_MEM(m) (m)
#define GUARDED_MEM(m) (m) #define GUARDED_MEM(m) (m)
#define ALLOC_LEN(ln) (ln) #define ALLOC_LEN(ln) (ln)
#define SET_GUARDS(m,ln) do {} while (0) #define SET_GUARDS(m,ln) STMT_NIL
#endif #endif
#ifdef PARANOIA #ifdef PARANOIA
#define check() do { assert_buf_ok(buf); } while (0) #define check() STMT_BEGIN assert_buf_ok(buf); STMT_END
#else #else
#define check() do { } while (0) #define check() STMT_NIL
#endif #endif
#ifdef NOINLINE #ifdef NOINLINE
......
...@@ -552,11 +552,11 @@ typedef struct { ...@@ -552,11 +552,11 @@ typedef struct {
/** Macro: assert that <b>cfg</b> has the right magic field for format /** Macro: assert that <b>cfg</b> has the right magic field for format
* <b>fmt</b>. */ * <b>fmt</b>. */
#define CHECK(fmt, cfg) do { \ #define CHECK(fmt, cfg) STMT_BEGIN \
tor_assert(fmt && cfg); \ tor_assert(fmt && cfg); \
tor_assert((fmt)->magic == \ tor_assert((fmt)->magic == \
*(uint32_t*)STRUCT_VAR_P(cfg,fmt->magic_offset)); \ *(uint32_t*)STRUCT_VAR_P(cfg,fmt->magic_offset)); \
} while (0) STMT_END
static void config_line_append(config_line_t **lst, static void config_line_append(config_line_t **lst,
const char *key, const char *val); const char *key, const char *val);
...@@ -2419,8 +2419,8 @@ options_validate(or_options_t *old_options, or_options_t *options, ...@@ -2419,8 +2419,8 @@ options_validate(or_options_t *old_options, or_options_t *options,
const char *uname = get_uname(); const char *uname = get_uname();
char buf[1024]; char buf[1024];
#define REJECT(arg) \ #define REJECT(arg) \
do { *msg = tor_strdup(arg); return -1; } while (0) STMT_BEGIN *msg = tor_strdup(arg); return -1; STMT_END
#define COMPLAIN(arg) do { log(LOG_WARN, LD_CONFIG, arg); } while (0) #define COMPLAIN(arg) STMT_BEGIN log(LOG_WARN, LD_CONFIG, arg); STMT_END
tor_assert(msg); tor_assert(msg);
*msg = NULL; *msg = NULL;
......
...@@ -103,7 +103,7 @@ static int dns_resolve_impl(edge_connection_t *exitconn, int is_resolve, ...@@ -103,7 +103,7 @@ static int dns_resolve_impl(edge_connection_t *exitconn, int is_resolve,
static void _assert_cache_ok(void); static void _assert_cache_ok(void);
#define assert_cache_ok() _assert_cache_ok() #define assert_cache_ok() _assert_cache_ok()
#else #else
#define assert_cache_ok() do {} while (0) #define assert_cache_ok() STMT_NIL
#endif #endif
static void assert_resolve_ok(cached_resolve_t *resolve); static void assert_resolve_ok(cached_resolve_t *resolve);
......
...@@ -1965,7 +1965,7 @@ nt_service_loadlibrary(void) ...@@ -1965,7 +1965,7 @@ nt_service_loadlibrary(void)
goto err; goto err;
} }
#define LOAD(f) do { \ #define LOAD(f) STMT_BEGIN \
if (!(fn = GetProcAddress(library, #f))) { \ if (!(fn = GetProcAddress(library, #f))) { \
log_err(LD_BUG, \ log_err(LD_BUG, \
"Couldn't find %s in advapi32.dll! We probably got the " \ "Couldn't find %s in advapi32.dll! We probably got the " \
...@@ -1974,7 +1974,7 @@ nt_service_loadlibrary(void) ...@@ -1974,7 +1974,7 @@ nt_service_loadlibrary(void)
} else { \ } else { \
service_fns.f ## _fn = fn; \ service_fns.f ## _fn = fn; \
} \ } \
} while (0) STMT_END
LOAD(ChangeServiceConfig2A); LOAD(ChangeServiceConfig2A);
LOAD(CloseServiceHandle); LOAD(CloseServiceHandle);
......
...@@ -2576,14 +2576,14 @@ void control_adjust_event_log_severity(void); ...@@ -2576,14 +2576,14 @@ void control_adjust_event_log_severity(void);
* Stmt must not contain any return or goto statements. * Stmt must not contain any return or goto statements.
*/ */
#define CONN_LOG_PROTECT(conn, stmt) \ #define CONN_LOG_PROTECT(conn, stmt) \
do { \ STMT_BEGIN \
int _log_conn_is_control = (conn && conn->type == CONN_TYPE_CONTROL); \ int _log_conn_is_control = (conn && conn->type == CONN_TYPE_CONTROL); \
if (_log_conn_is_control) \ if (_log_conn_is_control) \
disable_control_logging(); \ disable_control_logging(); \
do {stmt;} while (0); \ STMT_BEGIN stmt; STMT_END; \
if (_log_conn_is_control) \ if (_log_conn_is_control) \
enable_control_logging(); \ enable_control_logging(); \
} while (0) STMT_END
/** Log information about the connection <b>conn</b>, protecting it as with /** Log information about the connection <b>conn</b>, protecting it as with
* CONN_LOG_PROTECT. Example: * CONN_LOG_PROTECT. Example:
......
...@@ -220,7 +220,7 @@ authdir_policy_badexit_address(uint32_t addr, uint16_t port) ...@@ -220,7 +220,7 @@ authdir_policy_badexit_address(uint32_t addr, uint16_t port)
} }
#define REJECT(arg) \ #define REJECT(arg) \
do { *msg = tor_strdup(arg); goto err; } while (0) STMT_BEGIN *msg = tor_strdup(arg); goto err; STMT_END
/** Config helper: If there's any problem with the policy configuration /** Config helper: If there's any problem with the policy configuration
* options in <b>options</b>, return -1 and set <b>msg</b> to a newly * options in <b>options</b>, return -1 and set <b>msg</b> to a newly
......
...@@ -2282,12 +2282,13 @@ token_free(directory_token_t *tok) ...@@ -2282,12 +2282,13 @@ token_free(directory_token_t *tok)
} }
#define RET_ERR(msg) \ #define RET_ERR(msg) \
do { \ STMT_BEGIN \
if (tok) token_free(tok); \ if (tok) token_free(tok); \
tok = tor_malloc_zero(sizeof(directory_token_t)); \ tok = tor_malloc_zero(sizeof(directory_token_t)); \
tok->tp = _ERR; \ tok->tp = _ERR; \
tok->error = tor_strdup(msg); \ tok->error = tor_strdup(msg); \
goto done_tokenizing; } while (0) goto done_tokenizing; \
STMT_END
static INLINE directory_token_t * static INLINE directory_token_t *
token_check_object(const char *kwd, token_check_object(const char *kwd,
...@@ -2346,12 +2347,13 @@ get_next_token(const char **s, token_rule_t *table) ...@@ -2346,12 +2347,13 @@ get_next_token(const char **s, token_rule_t *table)
const char *kwd = ""; const char *kwd = "";
#define RET_ERR(msg) \ #define RET_ERR(msg) \
do { \ STMT_BEGIN \
if (tok) token_free(tok); \ if (tok) token_free(tok); \
tok = tor_malloc_zero(sizeof(directory_token_t)); \ tok = tor_malloc_zero(sizeof(directory_token_t)); \
tok->tp = _ERR; \ tok->tp = _ERR; \
tok->error = tor_strdup(msg); \ tok->error = tor_strdup(msg); \
goto done_tokenizing; } while (0) goto done_tokenizing; \
STMT_END
tok = tor_malloc_zero(sizeof(directory_token_t)); tok = tor_malloc_zero(sizeof(directory_token_t));
tok->tp = _ERR; tok->tp = _ERR;
......
...@@ -1007,25 +1007,25 @@ _test_eq_ip6(struct in6_addr *a, struct in6_addr *b, const char *e1, ...@@ -1007,25 +1007,25 @@ _test_eq_ip6(struct in6_addr *a, struct in6_addr *b, const char *e1,
} }
#define test_eq_ip6(a,b) _test_eq_ip6((a),(b),#a,#b,__LINE__) #define test_eq_ip6(a,b) _test_eq_ip6((a),(b),#a,#b,__LINE__)
#define test_pton6_same(a,b) do { \ #define test_pton6_same(a,b) STMT_BEGIN \
r = tor_inet_pton(AF_INET6, a, &a1); \ r = tor_inet_pton(AF_INET6, a, &a1); \
test_assert(r==1); \ test_assert(r==1); \
r = tor_inet_pton(AF_INET6, b, &a2); \ r = tor_inet_pton(AF_INET6, b, &a2); \
test_assert(r==1); \ test_assert(r==1); \
test_eq_ip6(&a1,&a2); \ test_eq_ip6(&a1,&a2); \
} while (0) STMT_END
#define test_pton6_bad(a) \ #define test_pton6_bad(a) \
test_eq(0, tor_inet_pton(AF_INET6, a, &a1)) test_eq(0, tor_inet_pton(AF_INET6, a, &a1))
#define test_ntop6_reduces(a,b) do { \ #define test_ntop6_reduces(a,b) STMT_BEGIN \
r = tor_inet_pton(AF_INET6, a, &a1); \ r = tor_inet_pton(AF_INET6, a, &a1); \
test_assert(r==1); \ test_assert(r==1); \
test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b); \ test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b); \
r = tor_inet_pton(AF_INET6, b, &a2); \ r = tor_inet_pton(AF_INET6, b, &a2); \
test_assert(r==1); \ test_assert(r==1); \
test_eq_ip6(&a1, &a2); \ test_eq_ip6(&a1, &a2); \
} while (0) STMT_END
static void static void
test_ip6_helpers(void) test_ip6_helpers(void)
......
...@@ -41,8 +41,8 @@ ...@@ -41,8 +41,8 @@
#define RESPONSE_LEN_4 8 #define RESPONSE_LEN_4 8
#define log_sock_error(act, _s) \ #define log_sock_error(act, _s) \
do { log_fn(LOG_ERR, LD_NET, "Error while %s: %s", act, \ STMT_BEGIN log_fn(LOG_ERR, LD_NET, "Error while %s: %s", act, \
tor_socket_strerror(tor_socket_errno(_s))); } while (0) tor_socket_strerror(tor_socket_errno(_s))); STMT_END
static void usage(void) ATTR_NORETURN; static void usage(void) ATTR_NORETURN;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment