Skip to content

Commit 6fd0c99

Browse files
authored
Update 2024-03-21-setjmp-plus-longjmp-equals-goto-but-awesome.md
1 parent 2c0caa6 commit 6fd0c99

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

_posts/2024-03-21-setjmp-plus-longjmp-equals-goto-but-awesome.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,21 @@ int main() {
137137

138138
Compiling and running this program with `gcc foo.c && ./a.out` [on godbolt.org](https://godbolt.org/z/3P9j59d15) prints `foo` to stdout, then prints `The value of 42 was bigger than expected!` to stderr, and then exits with `EXIT_FAILURE`.
139139

140-
And this is how grug finally uses `snprintf()` and `longjmp()` to throw a formatted error message in a few dozen spots:
140+
Although I dislike C's macros, `grug.c` is over 9k lines long and has over 400 spots where it needs to do throw if an error fails, so this is roughly what it uses:
141141

142142
```bettercpp
143+
#define grug_error(...) {\
144+
snprintf(error_msg, sizeof(error_msg), __VA_ARGS__);\
145+
error_line_number = __LINE__;\
146+
longjmp(error_jmp_buffer, 1);\
147+
}
148+
149+
#define grug_assert(condition, ...) {\
150+
if (!(condition)) {\
151+
grug_error(__VA_ARGS__);\
152+
}\
153+
}
154+
143155
struct token peek_token(size_t token_index) {
144156
grug_assert(token_index < tokens_size, "token_index %zu was out of bounds in peek_token()", token_index);
145157
return tokens[token_index];

0 commit comments

Comments
 (0)