Skip to content

Commit

Permalink
Put assert check into macro for static analyzers
Browse files Browse the repository at this point in the history
This way an analyzer will see it aborts when the
expression is false; especially the ccc-analyzer (clang)
generated many false positives otherwise.
  • Loading branch information
stbuehler committed Jun 1, 2012
1 parent 24dd542 commit b82d542
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions util/Assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@
#define Assert_GLUE(x, y) x ## y
#define Assert_STRING(x) #x

static inline void Assert_internal(const char* expr, int isTrue, const char* file, int line)
{
if (!isTrue) {
fprintf(stderr, "%s:%d Assertion failed: %s", file, line, expr);
abort();
}
}

/**
* Assert_compileTime()
*
Expand All @@ -45,8 +37,14 @@ static inline void Assert_internal(const char* expr, int isTrue, const char* fil


/** Runtime assertion which is always applied. */
#define Assert_always(expr) \
Assert_internal(Assert_STRING(expr), (expr) ? 1 : 0, __FILE__, __LINE__)
#define Assert_always(expr) do { \
if (!(expr)) { \
fprintf(stderr, "%s:%d Assertion failed: %s\n", \
__FILE__, __LINE__, Assert_STRING(expr)); \
abort(); \
} \
} while (0)
/* CHECKFILES_IGNORE a ; is expected after the while(0) but it will be supplied by the caller */

// Turn off assertions when the code is more stable.
#define Assert_true(expr) Assert_always(expr)
Expand Down

0 comments on commit b82d542

Please sign in to comment.