Skip to content

Commit

Permalink
LibC: Hint the compiler that assertions rarely fail
Browse files Browse the repository at this point in the history
Also, rewrite the macro to expand to an if statement instead of
a weird ternary operator with a (void)0 banch.
  • Loading branch information
bugaevc authored and awesomekling committed Apr 30, 2020
1 parent b319aca commit 1b36ddc
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Libraries/LibC/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ __BEGIN_DECLS
__attribute__((noreturn)) void __assertion_failed(const char* msg);
# define __stringify_helper(x) # x
# define __stringify(x) __stringify_helper(x)
# define assert(expr) ((expr) ? (void)0 : __assertion_failed(# expr "\n" __FILE__ ":" __stringify(__LINE__)));
# define assert(expr) \
do { \
if (__builtin_expect(!(expr), 0)) \
__assertion_failed(#expr "\n" __FILE__ ":" __stringify(__LINE__)); \
} while (0)
# define ASSERT_NOT_REACHED() assert(false)
#else
# define assert(expr)
Expand Down

0 comments on commit 1b36ddc

Please sign in to comment.