Skip to content

Commit 6b26044

Browse files
committed
Make RBS_ASSERT macro
1 parent eb0fe59 commit 6b26044

File tree

11 files changed

+37
-20
lines changed

11 files changed

+37
-20
lines changed

ext/rbs_extension/extconf.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
'-Wc++-compat',
1919
]
2020

21-
append_cflags ['-O0', '-pg'] if ENV['DEBUG']
21+
if ENV['DEBUG']
22+
append_cflags ['-O0', '-pg']
23+
else
24+
append_cflags ['-DNDEBUG']
25+
end
2226
if ENV["TEST_NO_C23"]
2327
puts "Adding -Wc2x-extensions to CFLAGS"
2428
$CFLAGS << " -Werror -Wc2x-extensions"

ext/rbs_extension/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* ```
1717
* */
1818
static NORETURN(void) raise_error(rbs_error_t *error, VALUE buffer) {
19-
rbs_assert(error != NULL, "raise_error() called with NULL error");
19+
RBS_ASSERT(error != NULL, "raise_error() called with NULL error");
2020

2121
if (!error->syntax_error) {
2222
rb_raise(rb_eRuntimeError, "Unexpected error");

include/rbs/util/rbs_assert.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
#include "rbs/defines.h"
55
#include <stdbool.h>
66

7-
void rbs_assert(bool condition, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(2, 3);
7+
/**
8+
* RBS_ASSERT macro that calls rbs_assert in debug builds and is removed in release builds.
9+
* In debug mode, it forwards all arguments to the rbs_assert function.
10+
* In release mode, it expands to nothing.
11+
*/
12+
#ifdef NDEBUG
13+
#define RBS_ASSERT(condition, ...) ((void) 0)
14+
#else
15+
#define RBS_ASSERT(condition, ...) rbs_assert_impl(condition, __VA_ARGS__)
16+
#endif
17+
18+
void rbs_assert_impl(bool condition, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(2, 3);
819

920
#endif

src/lexstate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ bool rbs_next_char(rbs_lexer_t *lexer, unsigned int *codepoint, size_t *byte_len
143143
}
144144

145145
void rbs_skip(rbs_lexer_t *lexer) {
146-
rbs_assert(lexer->current_character_bytes > 0, "rbs_skip called with current_character_bytes == 0");
146+
RBS_ASSERT(lexer->current_character_bytes > 0, "rbs_skip called with current_character_bytes == 0");
147147

148148
if (RBS_UNLIKELY(lexer->current_code_point == '\0')) {
149149
return;

src/location.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))
77

88
void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, size_t capacity) {
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);
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);
1010

1111
loc->children = rbs_allocator_malloc_impl(allocator, RBS_LOC_CHILDREN_SIZE(capacity), rbs_alignof(rbs_loc_children));
1212

@@ -16,8 +16,8 @@ void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, siz
1616
}
1717

1818
void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r) {
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);
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);
2121

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

src/parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ error_handling: {
233233
ids = "class/module/constant name";
234234
}
235235

236-
rbs_assert(ids != NULL, "Unknown kind of type: %i", kind);
236+
RBS_ASSERT(ids != NULL, "Unknown kind of type: %i", kind);
237237

238238
rbs_parser_set_error(parser, parser->current_token, true, "expected one of %s", ids);
239239
return false;

src/util/rbs_allocator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void *rbs_allocator_realloc_impl(rbs_allocator_t *allocator, void *ptr, size_t o
9898

9999
// Allocates `size` bytes from `allocator`, aligned to an `alignment`-byte boundary.
100100
void *rbs_allocator_malloc_impl(rbs_allocator_t *allocator, size_t size, size_t alignment) {
101-
rbs_assert(size % alignment == 0, "size must be a multiple of the alignment. size: %zu, alignment: %zu", size, alignment);
101+
RBS_ASSERT(size % alignment == 0, "size must be a multiple of the alignment. size: %zu, alignment: %zu", size, alignment);
102102

103103
if (allocator->default_page_payload_size < size) { // Big allocation, give it its own page.
104104
rbs_allocator_page_t *new_page = rbs_allocator_page_new(size);

src/util/rbs_assert.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#include <stdlib.h>
66
#include <stdbool.h>
77

8-
void rbs_assert(bool condition, const char *fmt, ...) {
8+
void rbs_assert_impl(bool condition, const char *fmt, ...) {
9+
printf("RBS_ASSERT called\n");
10+
911
if (condition) {
1012
return;
1113
}

src/util/rbs_buffer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void rbs_buffer_append_string(rbs_allocator_t *allocator, rbs_buffer_t *buffer,
2525
if (next_length > buffer->capacity) {
2626
size_t old_capacity = buffer->capacity;
2727

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

3030
size_t new_capacity = buffer->capacity * 2;
3131

@@ -34,7 +34,7 @@ void rbs_buffer_append_string(rbs_allocator_t *allocator, rbs_buffer_t *buffer,
3434
}
3535

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

3939
buffer->value = new_value;
4040
buffer->capacity = new_capacity;

src/util/rbs_constant_pool.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ next_power_of_two(uint32_t v) {
3737
return v;
3838
}
3939

40-
static bool is_power_of_two(uint32_t size) {
40+
__attribute__((unused)) static bool is_power_of_two(uint32_t size) {
4141
return (size & (size - 1)) == 0;
4242
}
4343

@@ -46,7 +46,7 @@ static bool is_power_of_two(uint32_t size) {
4646
*/
4747
static inline bool
4848
rbs_constant_pool_resize(rbs_constant_pool_t *pool) {
49-
rbs_assert(is_power_of_two(pool->capacity), "pool->capacity is not a power of two. Got %i", pool->capacity);
49+
RBS_ASSERT(is_power_of_two(pool->capacity), "pool->capacity is not a power of two. Got %i", pool->capacity);
5050

5151
uint32_t next_capacity = pool->capacity * 2;
5252
if (next_capacity < pool->capacity) return false;
@@ -123,7 +123,7 @@ bool rbs_constant_pool_init(rbs_constant_pool_t *pool, uint32_t capacity) {
123123
*/
124124
rbs_constant_t *
125125
rbs_constant_pool_id_to_constant(const rbs_constant_pool_t *pool, rbs_constant_id_t constant_id) {
126-
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);
126+
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);
127127
return &pool->constants[constant_id - 1];
128128
}
129129

@@ -133,7 +133,7 @@ rbs_constant_pool_id_to_constant(const rbs_constant_pool_t *pool, rbs_constant_i
133133
*/
134134
rbs_constant_id_t
135135
rbs_constant_pool_find(const rbs_constant_pool_t *pool, const uint8_t *start, size_t length) {
136-
rbs_assert(is_power_of_two(pool->capacity), "pool->capacity is not a power of two. Got %i", pool->capacity);
136+
RBS_ASSERT(is_power_of_two(pool->capacity), "pool->capacity is not a power of two. Got %i", pool->capacity);
137137
const uint32_t mask = pool->capacity - 1;
138138

139139
uint32_t hash = rbs_constant_pool_hash(start, length);
@@ -161,7 +161,7 @@ rbs_constant_pool_insert(rbs_constant_pool_t *pool, const uint8_t *start, size_t
161161
if (!rbs_constant_pool_resize(pool)) return RBS_CONSTANT_ID_UNSET;
162162
}
163163

164-
rbs_assert(is_power_of_two(pool->capacity), "pool->capacity is not a power of two. Got %i", pool->capacity);
164+
RBS_ASSERT(is_power_of_two(pool->capacity), "pool->capacity is not a power of two. Got %i", pool->capacity);
165165
const uint32_t mask = pool->capacity - 1;
166166

167167
uint32_t hash = rbs_constant_pool_hash(start, length);
@@ -202,7 +202,7 @@ rbs_constant_pool_insert(rbs_constant_pool_t *pool, const uint8_t *start, size_t
202202
// IDs are allocated starting at 1, since the value 0 denotes a non-existent
203203
// constant.
204204
uint32_t id = ++pool->size;
205-
rbs_assert(pool->size < ((uint32_t) (1 << 30)), "pool->size is too large. Got %i", pool->size);
205+
RBS_ASSERT(pool->size < ((uint32_t) (1 << 30)), "pool->size is too large. Got %i", pool->size);
206206

207207
*bucket = (rbs_constant_pool_bucket_t) {
208208
.id = (unsigned int) (id & 0x3fffffff),

0 commit comments

Comments
 (0)