Skip to content

Commit aceea3c

Browse files
authored
Merge pull request #143 from sue445/feature/fix_rb_errno_ptr
Fix cast when return value is pointer
2 parents f2ddb93 + 9834a96 commit aceea3c

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

.rubocop_todo.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2024-09-22 07:12:45 UTC using RuboCop version 1.66.1.
3+
# on 2024-09-25 08:16:07 UTC using RuboCop version 1.66.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 5
9+
# Offense count: 6
1010
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
1111
Metrics/AbcSize:
12-
Max: 67
12+
Max: 68
1313

1414
# Offense count: 1
1515
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
@@ -22,22 +22,22 @@ Metrics/BlockLength:
2222
Metrics/BlockNesting:
2323
Max: 4
2424

25-
# Offense count: 3
25+
# Offense count: 2
2626
# Configuration parameters: CountComments, CountAsOne.
2727
Metrics/ClassLength:
2828
Max: 131
2929

30-
# Offense count: 4
30+
# Offense count: 5
3131
# Configuration parameters: AllowedMethods, AllowedPatterns.
3232
Metrics/CyclomaticComplexity:
3333
Max: 17
3434

35-
# Offense count: 10
35+
# Offense count: 12
3636
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
3737
Metrics/MethodLength:
38-
Max: 61
38+
Max: 67
3939

4040
# Offense count: 3
4141
# Configuration parameters: AllowedMethods, AllowedPatterns.
4242
Metrics/PerceivedComplexity:
43-
Max: 15
43+
Max: 17

_tools/ruby_h_to_go/lib/ruby_h_to_go/function_definition.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,14 @@ def generate_go_content
110110
go_function_lines << call_c_method
111111
go_function_lines.push(*after_call_function_lines)
112112
else
113-
go_function_lines << "ret := #{go_function_typeref}(#{call_c_method})"
113+
cast_func =
114+
if go_function_typeref.start_with?("*")
115+
"(#{go_function_typeref})"
116+
else
117+
go_function_typeref
118+
end
119+
120+
go_function_lines << "ret := #{cast_func}(#{call_c_method})"
114121
go_function_lines.push(*after_call_function_lines)
115122
go_function_lines << "return ret"
116123
end

_tools/ruby_h_to_go/spec/ruby_h_to_go/function_definition_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,36 @@
187187

188188
it { should eq go_content }
189189
end
190+
191+
context "rb_errno_ptr" do
192+
let(:definition) do
193+
RubyHeaderParser::FunctionDefinition.new(
194+
name: "rb_errno_ptr",
195+
definition: "int *rb_errno_ptr(void)",
196+
filepath: "/path/to/include/ruby/ruby.h",
197+
typeref: typedef(type: "int", pointer: :ref),
198+
args: [],
199+
)
200+
end
201+
202+
let(:go_content) do
203+
<<~GO
204+
// RbErrnoPtr calls `rb_errno_ptr` in C
205+
//
206+
// Original definition is following
207+
//
208+
// int *rb_errno_ptr(void)
209+
//
210+
// ref. https://github.com/ruby/ruby/blob/master/include/ruby/ruby.h
211+
func RbErrnoPtr() *int {
212+
ret := (*int)(C.rb_errno_ptr())
213+
return ret
214+
}
215+
216+
GO
217+
end
218+
219+
it { should eq go_content }
220+
end
190221
end
191222
end

_tools/ruby_h_to_go/spec/ruby_header_parser/parser_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@
136136
its(:typeref) { should eq typedef(type: "int") }
137137
its(:args) { should eq args }
138138
end
139+
140+
context "rb_errno_ptr" do
141+
subject { definitions.find { |d| d.name == "rb_errno_ptr" } }
142+
143+
let(:args) do
144+
[]
145+
end
146+
147+
its(:name) { should eq "rb_errno_ptr" }
148+
its(:definition) { should eq "int *rb_errno_ptr(void)" }
149+
its(:filepath) { should be_end_with "/ruby/ruby.h" }
150+
its(:typeref) { should eq typedef(type: "int", pointer: :ref) }
151+
its(:args) { should eq args }
152+
end
139153
end
140154

141155
describe "#extract_static_inline_function_definitions" do

0 commit comments

Comments
 (0)