Skip to content

Add NOB_ASSERT that doesnt depend on assert.h#105

Open
screaminglineage wants to merge 3 commits intotsoding:mainfrom
screaminglineage:nob-assert
Open

Add NOB_ASSERT that doesnt depend on assert.h#105
screaminglineage wants to merge 3 commits intotsoding:mainfrom
screaminglineage:nob-assert

Conversation

@screaminglineage
Copy link

The main issue this solves is that when the assert() from libc is called in a debugger, you end up several stack levels deep and have to hunt down where the crash actually occurred.

It ends up looking like this.
image

This approach instead makes the compiler generate a trap instruction and crash immediately.

PS: You could also implement cool custom formatting for asserts, but I didn't put that in for the sake of keeping it simple.

#define assertf(expr, ...)    \
    (!(expr))?     \
        (printf("%s:%d: assertion `%s` failed: \"", __FILE__, __LINE__, #expr),    \
        printf(__VA_ARGS__),  \
        printf('\"\n'),  \ 
        CRASH())    \ 
    : (void)0

assertf(a == b, "expected %d but got %d", a, b);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant