unix: add UNREACHABLE() macro

Asserts and aborts when program flow reaches a place it shouldn't.
This commit is contained in:
Ben Noordhuis 2012-01-21 04:01:39 +01:00
parent 3de0411591
commit cdb44df86d
5 changed files with 21 additions and 15 deletions

View File

@ -86,7 +86,5 @@ uv_err_code uv_translate_sys_error(int sys_errno) {
case ETIMEDOUT: return UV_ETIMEDOUT;
default: return UV_UNKNOWN;
}
assert(0 && "unreachable");
return -1;
UNREACHABLE();
}

View File

@ -25,6 +25,8 @@
#include "uv-common.h"
#include "uv-eio.h"
#include <assert.h>
#include <stdlib.h> /* abort */
#include <stddef.h> /* offsetof */
#if __STRICT_ANSI__
@ -133,13 +135,20 @@ inline static int sys_accept4(int fd,
#define container_of(ptr, type, member) \
((type *) ((char *) (ptr) - offsetof(type, member)))
#define SAVE_ERRNO(block) \
do { \
int _saved_errno = errno; \
do { block; } while (0); \
errno = _saved_errno; \
} \
while (0);
#define UNREACHABLE() \
do { \
assert(0 && "unreachable code"); \
abort(); \
} \
while (0)
#define SAVE_ERRNO(block) \
do { \
int _saved_errno = errno; \
do { block; } while (0); \
errno = _saved_errno; \
} \
while (0)
/* flags */
enum {

View File

@ -141,13 +141,13 @@ int uv_fs_event_init(uv_loop_t* loop,
void uv__fs_event_destroy(uv_fs_event_t* handle) {
assert(0 && "unreachable");
UNREACHABLE();
}
/* Called by libev, don't touch. */
void uv__kqueue_hack(EV_P_ int fflags, ev_io *w) {
assert(0 && "unreachable");
UNREACHABLE();
}
#endif /* HAVE_KQUEUE */

View File

@ -679,8 +679,7 @@ int uv_fs_event_init(uv_loop_t* loop,
void uv__fs_event_destroy(uv_fs_event_t* handle) {
assert(0 && "unreachable");
abort();
UNREACHABLE();
}
#endif /* HAVE_INOTIFY_INIT || HAVE_INOTIFY_INIT1 */

View File

@ -214,7 +214,7 @@ int uv_fs_event_init(uv_loop_t* loop,
void uv__fs_event_destroy(uv_fs_event_t* handle) {
assert(0 && "unreachable"); /* should never be called */
UNREACHABLE();
}
#endif /* HAVE_PORTS_FS */