Skip to content

Commit 04997ac

Browse files
committed
Fix rb_int2num_inline
1 parent aceea3c commit 04997ac

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed

_tools/ruby_h_to_go/lib/ruby_header_parser/parser.rb

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ def __extract_function_definitions(c_kinds:, kind:, is_parse_multiline_definitio
7676
parts = line.split("\t")
7777

7878
function_name = parts[0]
79+
filepath = parts[1]
7980

8081
next unless data.should_generate_function?(function_name)
8182

8283
next unless parts[3] == kind
8384

8485
line_num = Util.find_field(parts, "line").to_i
85-
definition =
86-
parse_function_definition(filepath: parts[1], pattern: parts[2], line_num:, is_parse_multiline_definition:)
86+
definition = parse_function_definition(filepath:, pattern: parts[2], line_num:, is_parse_multiline_definition:)
8787

8888
args = parse_definition_args(function_name, Util.find_field(parts, "signature"))
8989

@@ -94,9 +94,9 @@ def __extract_function_definitions(c_kinds:, kind:, is_parse_multiline_definitio
9494

9595
definitions << FunctionDefinition.new(
9696
definition:,
97-
name: parts[0],
98-
filepath: parts[1],
99-
typeref: create_typeref(definition:, function_name:, typeref_field:),
97+
name: function_name,
98+
filepath:,
99+
typeref: create_typeref(definition:, function_name:, typeref_field:, filepath:, line_num:),
100100
args:,
101101
)
102102
end
@@ -212,17 +212,11 @@ def parse_definition_args(function_name, signature)
212212
# @param definition [String]
213213
# @param function_name [String]
214214
# @param typeref_field [String,nil]
215+
# @param filepath [String]
216+
# @param line_num [Integer]
215217
# @return [RubyHeaderParser::TyperefDefinition]
216-
def create_typeref(definition:, function_name:, typeref_field:)
217-
typeref_type =
218-
if typeref_field
219-
type = typeref_field.gsub(/[A-Z_]+\s*\(\(.*\)\)/, "").gsub("RUBY_SYMBOL_EXPORT_BEGIN", "")
220-
Util.sanitize_type(type) # rubocop:disable Style/IdenticalConditionalBranches
221-
else
222-
# parse typeref in definition
223-
type = definition[0...definition.index(function_name)].gsub("char *", "char*").strip
224-
Util.sanitize_type(type) # rubocop:disable Style/IdenticalConditionalBranches
225-
end
218+
def create_typeref(definition:, function_name:, typeref_field:, filepath:, line_num:)
219+
typeref_type = parse_typeref_type(definition:, function_name:, typeref_field:, filepath:, line_num:)
226220

227221
typeref_pointer = nil
228222
if typeref_type.match?(/\*+$/)
@@ -232,5 +226,39 @@ def create_typeref(definition:, function_name:, typeref_field:)
232226

233227
TyperefDefinition.new(type: typeref_type, pointer: typeref_pointer)
234228
end
229+
230+
# @param definition [String]
231+
# @param function_name [String]
232+
# @param typeref_field [String,nil]
233+
# @param filepath [String]
234+
# @param line_num [Integer]
235+
# @return [String]
236+
def parse_typeref_type(definition:, function_name:, typeref_field:, filepath:, line_num:)
237+
typeref_type =
238+
if typeref_field
239+
typeref_field.gsub(/[A-Z_]+\s*\(\(.*\)\)/, "").gsub("RUBY_SYMBOL_EXPORT_BEGIN", "")
240+
else
241+
# parse typeref in definition
242+
definition[0...definition.index(function_name)].gsub("char *", "char*").strip
243+
end
244+
245+
typeref_type = Util.sanitize_type(typeref_type)
246+
return typeref_type unless typeref_type.empty?
247+
248+
# Check prev line
249+
line = read_file_line(filepath:, line_num: line_num - 1)
250+
return Util.sanitize_type(line) if line
251+
252+
""
253+
end
254+
255+
# @param filepath [String]
256+
# @param line_num [Integer]
257+
def read_file_line(filepath:, line_num:)
258+
return nil if line_num < 1
259+
260+
lines = File.open(filepath, "rb") { |f| f.readlines(chomp: true) }
261+
lines[line_num - 1]
262+
end
235263
end
236264
end

_tools/ruby_h_to_go/lib/ruby_header_parser/util.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def self.split_signature(signature)
2323
# @param type [String]
2424
# @return [String]
2525
def self.sanitize_type(type)
26-
type.gsub(/(RUBY_EXTERN|enum|volatile|const|struct)\s+/i, "").gsub("const*", "").strip
26+
type.gsub(/(RUBY_EXTERN|enum|volatile|const|struct|static\s+inline)\s+/i, "").gsub("const*", "").strip
2727
end
2828
end
2929
end

_tools/ruby_h_to_go/spec/ruby_header_parser/parser_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,22 @@
177177
its(:typeref) { should eq typedef(type: "int") }
178178
its(:args) { should eq args }
179179
end
180+
181+
context "rb_int2num_inline" do
182+
subject { definitions.find { |d| d.name == "rb_int2num_inline" } }
183+
184+
let(:args) do
185+
[
186+
argument(type: "int", name: "v"),
187+
]
188+
end
189+
190+
its(:name) { should eq "rb_int2num_inline" }
191+
its(:definition) { should eq "rb_int2num_inline(int v)" }
192+
its(:filepath) { should be_end_with "/ruby/internal/arithmetic/int.h" }
193+
its(:typeref) { should eq typedef(type: "VALUE") }
194+
its(:args) { should eq args }
195+
end
180196
end
181197

182198
describe "#extract_struct_definitions" do

0 commit comments

Comments
 (0)