Skip to content

Instantly share code, notes, and snippets.

@ykst
Created November 21, 2014 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ykst/63ec7981431e42ba1489 to your computer and use it in GitHub Desktop.
Save ykst/63ec7981431e42ba1489 to your computer and use it in GitHub Desktop.
いつものASSERTマクロ ref: http://qiita.com/ykst/items/f368e6ec8d0160b19ba8
#ifndef unlikely
# ifdef __builtin_expect
# define unlikely(x) __builtin_expect((x), 0)
# else
# define unlikely(x) (x)
# endif
#endif
#define ERROR(fmt, ...) do { \
fprintf(stderr, "\x1b[1;31mERROR:%s:%s:%d: " fmt "\x1b[0m", __BASE_FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
} while (0)
#define ASSERT(b,action) ({ int __b = (int)(b); if(unlikely(!(__b))){ ERROR("failed (%s)\n", #b); action; } __b;})
// ASSERT(<条件式>, <失敗時アクション>)
ASSERT(maybe_failed() == SUCCESS, goto error);
ERROR :main.c:main:17: failed (maybe_failed() == SUCCESS)
ASSERT(maybe_failed() == SUCCESS, { do_something(); goto error; });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment