Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,31 @@ jobs:
- run: bundle exec rake test:valgrind
env:
RUBY_FREE_AT_EXIT: 1
C99_compile:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This job compiles the extension without C23 extensions to ensure it compiles with C99 compilers.

To compile it with clang, not gcc, the macos platform is used.

runs-on: macos-latest
strategy:
fail-fast: false
matrix:
ruby: ['3.4', head]
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: |
brew install ruby-build
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler: none
- name: Set working directory as safe
run: git config --global --add safe.directory $(pwd)
- name: Update rubygems & bundler
run: |
ruby -v
gem update --system
- name: clang version
run: clang --version
- name: bin/setup
run: |
bin/setup
- run: bundle exec rake clean compile_c99

8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,11 @@ task :changelog do
puts " (🤑 There is no *unreleased* pull request associated to the milestone.)"
end
end

desc "Compile extension without C23 extensions"
task :compile_c99 do
ENV["TEST_NO_C23"] = "true"
Rake::Task[:"compile"].invoke
ensure
ENV.delete("TEST_NO_C23")
end
2 changes: 2 additions & 0 deletions ext/rbs_extension/ast_translation.c
Original file line number Diff line number Diff line change
Expand Up @@ -1146,4 +1146,6 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan
return ID2SYM(rb_intern3((const char *) constant->start, constant->length, ctx.encoding));
}
}

rb_raise(rb_eRuntimeError, "Unknown node type: %d", instance->type);
}
4 changes: 4 additions & 0 deletions ext/rbs_extension/ast_translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
#ifndef RBS_EXTENSION_AST_TRANSLATION_H
#define RBS_EXTENSION_AST_TRANSLATION_H

#include "compat.h"

SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
#include "ruby.h"
#include "ruby/encoding.h"
SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END

#include "rbs/ast.h"
#include "rbs/location.h"
Expand Down
4 changes: 4 additions & 0 deletions ext/rbs_extension/class_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
#ifndef RBS__CONSTANTS_H
#define RBS__CONSTANTS_H

#include "compat.h"

SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
#include "ruby.h"
SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END

extern VALUE RBS;

Expand Down
10 changes: 10 additions & 0 deletions ext/rbs_extension/compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifdef __clang__
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including ruby.h caused compilation errors without C23 extensions. This macro is to suppress diagnostics in the header files.

#define SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wc2x-extensions\"")
#define SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END \
_Pragma("clang diagnostic pop")
#else
#define SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
#define SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END
#endif
4 changes: 4 additions & 0 deletions ext/rbs_extension/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@

append_cflags ['-std=gnu99', '-Wimplicit-fallthrough', '-Wunused-result']
append_cflags ['-O0', '-g'] if ENV['DEBUG']
if ENV["TEST_NO_C23"]
Copy link
Member Author

@soutaro soutaro May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using append_cflags doesn't work because the compilation with -Wc2x-extensions fails. (I guess this is because ruby.h issues diagnostics without C23 extensions.)

puts "Adding -Wc2x-extensions to CFLAGS"
$CFLAGS << " -Werror -Wc2x-extensions"
end

create_makefile 'rbs_extension'
5 changes: 5 additions & 0 deletions ext/rbs_extension/legacy_location.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#ifndef RBS_LOCATION_H
#define RBS_LOCATION_H

#include "compat.h"

SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
#include "ruby.h"
SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END

#include "rbs.h"

/**
Expand Down
3 changes: 3 additions & 0 deletions ext/rbs_extension/rbs_extension.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <stdbool.h>
#include "compat.h"

SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
#include "ruby.h"
#include "ruby/re.h"
#include "ruby/encoding.h"
SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END

#include "class_constants.h"
#include "rbs.h"
Expand Down
4 changes: 4 additions & 0 deletions ext/rbs_extension/rbs_string_bridging.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#ifndef RBS__RBS_STRING_BRIDGING_H
#define RBS__RBS_STRING_BRIDGING_H

#include "compat.h"

SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
#include "ruby.h"
#include "ruby/encoding.h"
SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END

#include "rbs/string.h"

Expand Down
6 changes: 3 additions & 3 deletions include/rbs/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ typedef struct {
const rbs_encoding_t *encoding;
} rbs_lexer_t;

extern rbs_token_t NullToken;
extern rbs_position_t NullPosition;
extern rbs_range_t NULL_RANGE;
extern const rbs_token_t NullToken;
extern const rbs_position_t NullPosition;
extern const rbs_range_t NULL_RANGE;

char *rbs_peek_token(rbs_lexer_t *lexer, rbs_token_t tok);
int rbs_token_chars(rbs_token_t tok);
Expand Down
6 changes: 3 additions & 3 deletions src/lexstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ static const char *RBS_TOKENTYPE_NAMES[] = {
"tANNOTATION", /* Annotation */
};

rbs_token_t NullToken = { .type = NullType, .range = {} };
rbs_position_t NullPosition = { -1, -1, -1, -1 };
rbs_range_t NULL_RANGE = { { -1, -1, -1, -1 }, { -1, -1, -1, -1 } };
const rbs_position_t NullPosition = { -1, -1, -1, -1 };
const rbs_range_t NULL_RANGE = { { -1, -1, -1, -1 }, { -1, -1, -1, -1 } };
const rbs_token_t NullToken = { .type = NullType, .range = { {0}, {0} } };

const char *rbs_token_type_str(enum RBSTokenType type) {
return RBS_TOKENTYPE_NAMES[type];
Expand Down
2 changes: 1 addition & 1 deletion src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -3415,7 +3415,7 @@ rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding
.vars = NULL,
.last_comment = NULL,

.constant_pool = {},
.constant_pool = {0},
.allocator = allocator,
.error = NULL,
};
Expand Down
2 changes: 1 addition & 1 deletion src/util/rbs_constant_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ rbs_constant_pool_resize(rbs_constant_pool_t *pool) {
}

// This storage is initialized by `Init_rbs_extension()` in `main.c`.
static rbs_constant_pool_t RBS_GLOBAL_CONSTANT_POOL_STORAGE = {};
static rbs_constant_pool_t RBS_GLOBAL_CONSTANT_POOL_STORAGE = {0};
rbs_constant_pool_t *RBS_GLOBAL_CONSTANT_POOL = &RBS_GLOBAL_CONSTANT_POOL_STORAGE;

/**
Expand Down
2 changes: 2 additions & 0 deletions templates/ext/rbs_extension/ast_translation.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,6 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan
return ID2SYM(rb_intern3((const char *) constant->start, constant->length, ctx.encoding));
}
}

rb_raise(rb_eRuntimeError, "Unknown node type: %d", instance->type);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GCC reports warning: control reaches end of non-void function [-Wreturn-type] diagnostic here.

}
4 changes: 4 additions & 0 deletions templates/ext/rbs_extension/ast_translation.h.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#ifndef RBS_EXTENSION_AST_TRANSLATION_H
#define RBS_EXTENSION_AST_TRANSLATION_H

#include "compat.h"

SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
#include "ruby.h"
#include "ruby/encoding.h"
SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END

#include "rbs/ast.h"
#include "rbs/location.h"
Expand Down
4 changes: 4 additions & 0 deletions templates/ext/rbs_extension/class_constants.h.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#ifndef RBS__CONSTANTS_H
#define RBS__CONSTANTS_H

#include "compat.h"

SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
#include "ruby.h"
SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END

extern VALUE RBS;

Expand Down