Skip to content

Commit 877f2ee

Browse files
committed
Replace assert with rbs_assert
1 parent 5afb879 commit 877f2ee

File tree

10 files changed

+52
-21
lines changed

10 files changed

+52
-21
lines changed

ext/rbs_extension/main.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "rbs_extension.h"
2+
#include "rbs/util/rbs_assert.h"
23
#include "rbs/util/rbs_allocator.h"
34
#include "rbs/util/rbs_constant_pool.h"
45
#include "ast_translation.h"
@@ -15,7 +16,7 @@
1516
* ```
1617
* */
1718
static NORETURN(void) raise_error(error *error, VALUE buffer) {
18-
assert(error != NULL && "raise_error() called with NULL error");
19+
rbs_assert(error != NULL, "raise_error() called with NULL error");
1920

2021
if (!error->syntax_error) {
2122
rb_raise(rb_eRuntimeError, "Unexpected error");

include/rbs/util/rbs_assert.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef RBS_ASSERT_H
2+
#define RBS_ASSERT_H
3+
4+
#include "rbs/defines.h"
5+
#include <stdbool.h>
6+
7+
void rbs_assert(bool condition, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(2, 3);
8+
9+
#endif

src/parser.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "rbs/encoding.h"
1111
#include "rbs/rbs_string.h"
1212
#include "rbs/rbs_unescape.h"
13+
#include "rbs/util/rbs_assert.h"
1314

1415
#define INTERN(str) \
1516
rbs_constant_pool_insert_constant( \
@@ -237,7 +238,7 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb
237238
ids = "class/module/constant name";
238239
}
239240

240-
assert(ids != NULL && "Unknown kind of type");
241+
rbs_assert(ids != NULL, "Unknown kind of type: %i", kind);
241242

242243
set_error(state, state->current_token, true, "expected one of %s", ids);
243244
return false;

src/parserstate.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,7 @@ void rbs_parser_declare_type_variables(parserstate *parser, size_t count, const
367367
);
368368

369369
if (!parser_insert_typevar(parser, name)) {
370-
fprintf(stderr, "RuntimeError: %s\n", parser->error->message);
371-
exit(1);
370+
rbs_assert(false, "Failed to insert type variable. Message: %s", parser->error->message);
372371
}
373372
}
374373
}

src/rbs_buffer.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "rbs/rbs_buffer.h"
2-
3-
#include <assert.h>
2+
#include "rbs/util/rbs_assert.h"
43

54
bool rbs_buffer_init(rbs_allocator_t *allocator, rbs_buffer_t *buffer) {
65
size_t capacity = RBS_BUFFER_DEFAULT_CAPACITY;
@@ -26,7 +25,7 @@ void rbs_buffer_append_string(rbs_allocator_t *allocator, rbs_buffer_t *buffer,
2625
if (next_length > buffer->capacity) {
2726
size_t old_capacity = buffer->capacity;
2827

29-
assert(old_capacity != 0 && "Precondition: capacity must be at least 1.");
28+
rbs_assert(old_capacity != 0, "Precondition: capacity must be at least 1. Got %zu", old_capacity);
3029

3130
size_t new_capacity = buffer->capacity * 2;
3231

@@ -35,7 +34,7 @@ void rbs_buffer_append_string(rbs_allocator_t *allocator, rbs_buffer_t *buffer,
3534
}
3635

3736
char *new_value = rbs_allocator_realloc(allocator, buffer->value, old_capacity, new_capacity, char);
38-
assert(new_value != NULL && "Failed to append to buffer");
37+
rbs_assert(new_value != NULL, "Failed to append to buffer. Old capacity: %zu, new capacity: %zu", old_capacity, new_capacity);
3938

4039
buffer->value = new_value;
4140
buffer->capacity = new_capacity;

src/rbs_encoding.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "rbs/rbs_encoding.h"
2+
#include "rbs/util/rbs_assert.h"
23

34
#if defined(__GNUC__)
45
# define RBS_ATTRIBUTE_UNUSED __attribute__((unused))
@@ -2261,7 +2262,7 @@ static const uint8_t rbs_utf_8_dfa[] = {
22612262
*/
22622263
static rbs_unicode_codepoint_t
22632264
rbs_utf_8_codepoint(const uint8_t *b, ptrdiff_t n, size_t *width) {
2264-
assert(n >= 0);
2265+
rbs_assert(n >= 0, "n must be greater than or equal to 0. Got %ti", n);
22652266

22662267
size_t maximum = (n > 4) ? 4 : ((size_t) n);
22672268
uint32_t codepoint;
@@ -2291,7 +2292,7 @@ rbs_utf_8_codepoint(const uint8_t *b, ptrdiff_t n, size_t *width) {
22912292
*/
22922293
size_t
22932294
rbs_encoding_utf_8_char_width(const uint8_t *b, ptrdiff_t n) {
2294-
assert(n >= 0);
2295+
rbs_assert(n >= 0, "n must be greater than or equal to 0. Got %ti", n);
22952296

22962297
size_t maximum = (n > 4) ? 4 : ((size_t) n);
22972298
uint32_t state = 0;

src/rbs_location.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "rbs/rbs_location.h"
2+
#include "rbs/util/rbs_assert.h"
23

34
#include <stdio.h>
45

56
#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))
67

78
void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, size_t capacity) {
8-
assert(capacity <= sizeof(rbs_loc_entry_bitmap) * 8 && "Capacity is too large");
9+
rbs_assert(capacity <= sizeof(rbs_loc_entry_bitmap) * 8, "Capacity %zu is too large. Max is %zu", capacity, sizeof(rbs_loc_entry_bitmap) * 8);
910

1011
loc->children = rbs_allocator_malloc_impl(allocator, RBS_LOC_CHILDREN_SIZE(capacity), alignof(rbs_loc_children));
1112

@@ -15,8 +16,8 @@ void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, siz
1516
}
1617

1718
void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r) {
18-
assert(loc->children != NULL && "All children should have been pre-allocated with rbs_loc_alloc_children()");
19-
assert((loc->children->len + 1 <= loc->children->cap) && "Not enough space was pre-allocated for the children.");
19+
rbs_assert(loc->children != NULL, "All children should have been pre-allocated with rbs_loc_alloc_children()");
20+
rbs_assert((loc->children->len + 1 <= loc->children->cap), "Not enough space was pre-allocated for the children. Children: %hu, Capacity: %hu", loc->children->len, loc->children->cap);
2021

2122
unsigned short i = loc->children->len++;
2223
loc->children->entries[i].name = name;

src/util/rbs_allocator.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616

1717
#include "rbs/util/rbs_allocator.h"
18+
#include "rbs/util/rbs_assert.h"
1819

1920
#include <stdlib.h>
20-
#include <assert.h>
2121
#include <string.h> // for memset()
2222
#include <stdint.h>
2323

@@ -138,7 +138,7 @@ void *rbs_allocator_realloc_impl(rbs_allocator_t *allocator, void *ptr, size_t o
138138

139139
// Allocates `size` bytes from `allocator`, aligned to an `alignment`-byte boundary.
140140
void *rbs_allocator_malloc_impl(rbs_allocator_t *allocator, size_t size, size_t alignment) {
141-
assert(size % alignment == 0 && "size must be a multiple of the alignment");
141+
rbs_assert(size % alignment == 0, "size must be a multiple of the alignment. size: %zu, alignment: %zu", size, alignment);
142142

143143
if (default_page_payload_size < size) { // Big allocation, give it its own page.
144144
// How much we need to pad the new page's payload in order to get an aligned pointer
@@ -176,7 +176,7 @@ void *rbs_allocator_malloc_impl(rbs_allocator_t *allocator, size_t size, size_t
176176
allocator->page = new_page;
177177

178178
p = rbs_allocator_page_attempt_alloc(new_page, size, alignment);
179-
assert(p != NULL && "Failed to allocate a new allocator page");
179+
rbs_assert(p != NULL, "Failed to allocate a new allocator page");
180180
return p;
181181
}
182182

src/util/rbs_assert.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "rbs/util/rbs_assert.h"
2+
3+
#include <stdarg.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <stdbool.h>
7+
8+
void rbs_assert(bool condition, const char *fmt, ...) {
9+
if (condition) {
10+
return;
11+
}
12+
13+
va_list args;
14+
va_start(args, fmt);
15+
vfprintf(stderr, fmt, args);
16+
va_end(args);
17+
fprintf(stderr, "\n");
18+
exit(EXIT_FAILURE);
19+
}

src/util/rbs_constant_pool.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "rbs/util/rbs_constant_pool.h"
2+
#include "rbs/util/rbs_assert.h"
23

34
/**
45
* A relatively simple hash function (djb2) that is used to hash strings. We are
@@ -48,7 +49,7 @@ is_power_of_two(uint32_t size) {
4849
*/
4950
static inline bool
5051
rbs_constant_pool_resize(rbs_constant_pool_t *pool) {
51-
assert(is_power_of_two(pool->capacity));
52+
rbs_assert(is_power_of_two(pool->capacity), "pool->capacity is not a power of two. Got %i", pool->capacity);
5253

5354
uint32_t next_capacity = pool->capacity * 2;
5455
if (next_capacity < pool->capacity) return false;
@@ -126,7 +127,7 @@ rbs_constant_pool_init(rbs_constant_pool_t *pool, uint32_t capacity) {
126127
*/
127128
rbs_constant_t *
128129
rbs_constant_pool_id_to_constant(const rbs_constant_pool_t *pool, rbs_constant_id_t constant_id) {
129-
assert(constant_id != RBS_CONSTANT_ID_UNSET && constant_id <= pool->size);
130+
rbs_assert(constant_id != RBS_CONSTANT_ID_UNSET && constant_id <= pool->size, "constant_id is not valid. Got %i, pool->size: %i", constant_id, pool->size);
130131
return &pool->constants[constant_id - 1];
131132
}
132133

@@ -136,7 +137,7 @@ rbs_constant_pool_id_to_constant(const rbs_constant_pool_t *pool, rbs_constant_i
136137
*/
137138
rbs_constant_id_t
138139
rbs_constant_pool_find(const rbs_constant_pool_t *pool, const uint8_t *start, size_t length) {
139-
assert(is_power_of_two(pool->capacity));
140+
rbs_assert(is_power_of_two(pool->capacity), "pool->capacity is not a power of two. Got %i", pool->capacity);
140141
const uint32_t mask = pool->capacity - 1;
141142

142143
uint32_t hash = rbs_constant_pool_hash(start, length);
@@ -164,7 +165,7 @@ rbs_constant_pool_insert(rbs_constant_pool_t *pool, const uint8_t *start, size_t
164165
if (!rbs_constant_pool_resize(pool)) return RBS_CONSTANT_ID_UNSET;
165166
}
166167

167-
assert(is_power_of_two(pool->capacity));
168+
rbs_assert(is_power_of_two(pool->capacity), "pool->capacity is not a power of two. Got %i", pool->capacity);
168169
const uint32_t mask = pool->capacity - 1;
169170

170171
uint32_t hash = rbs_constant_pool_hash(start, length);
@@ -205,7 +206,7 @@ rbs_constant_pool_insert(rbs_constant_pool_t *pool, const uint8_t *start, size_t
205206
// IDs are allocated starting at 1, since the value 0 denotes a non-existent
206207
// constant.
207208
uint32_t id = ++pool->size;
208-
assert(pool->size < ((uint32_t) (1 << 30)));
209+
rbs_assert(pool->size < ((uint32_t) (1 << 30)), "pool->size is too large. Got %i", pool->size);
209210

210211
*bucket = (rbs_constant_pool_bucket_t) {
211212
.id = (unsigned int) (id & 0x3fffffff),

0 commit comments

Comments
 (0)