Skip to content

Commit d80f8b4

Browse files
authored
Merge pull request #2729 from ruby/rbs-assert
Make assertions macro
2 parents 5779612 + f7a28df commit d80f8b4

File tree

14 files changed

+74
-28
lines changed

14 files changed

+74
-28
lines changed

bin/benchmark-parse.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,37 @@
22
require "benchmark/ips"
33
require "csv"
44

5+
require "optparse"
6+
7+
label = nil #: String?
8+
9+
OptionParser.new do |opts|
10+
opts.banner = "Usage: benchmark-parse.rb [options] [file|directory]..."
11+
12+
opts.on("--label=LABEL", "Set the benchmark label") do |v|
13+
label = v
14+
end
15+
end.parse!(ARGV)
16+
17+
file_names = []
518
files = {}
619
ARGV.each do |file|
20+
path = Pathname(file)
21+
if path.directory?
22+
Pathname.glob(path.join("**", "*.rbs")).each do |p|
23+
file_names << p.to_s
24+
end
25+
else
26+
file_names << path.to_s
27+
end
28+
end
29+
30+
file_names.uniq.each do |file|
731
content = File.read(file)
832
files[file] = RBS::Buffer.new(content: content, name: Pathname(file))
933
end
1034

11-
puts "Benchmarking parsing #{files.size} files..."
35+
puts "Benchmarking RBS(#{RBS::VERSION} in #{Bundler.default_gemfile.basename}#{label ? " (#{label})" : ""}) parsing with #{files.size} files..."
1236

1337
result = Benchmark.ips do |x|
1438
x.report("parsing") do

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/defines.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,13 @@
7474
#define NODISCARD __attribute__((warn_unused_result))
7575
#endif
7676

77+
/**
78+
* Mark a function or variable as potentially unused to suppress compiler warnings.
79+
*/
80+
#if defined(__GNUC__) || defined(__clang__)
81+
#define RBS_ATTRIBUTE_UNUSED __attribute__((unused))
82+
#else
83+
#define RBS_ATTRIBUTE_UNUSED
84+
#endif
85+
7786
#endif

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

include/rbs/util/rbs_encoding.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#ifndef RBS_RBS_ENCODING_H
77
#define RBS_RBS_ENCODING_H
88

9+
#include "rbs/defines.h"
10+
911
#include <assert.h>
1012
#include <stdbool.h>
1113
#include <stddef.h>

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static rbs_allocator_page_t *rbs_allocator_page_new(size_t payload_size) {
6464
return page;
6565
}
6666

67-
rbs_allocator_t *rbs_allocator_init() {
67+
rbs_allocator_t *rbs_allocator_init(void) {
6868
rbs_allocator_t *allocator = malloc(sizeof(rbs_allocator_t));
6969

7070
const size_t system_page_size = get_system_page_size();
@@ -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);

0 commit comments

Comments
 (0)