8000 [ruby/fiddle] Add support for "const" in type · ruby/ruby@dc929b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc929b6

Browse files
kouhsbt
authored andcommitted
[ruby/fiddle] Add support for "const" in type
GitHub: fix #68 Reported by kojix2. Thanks!!! ruby/fiddle@d7322c234a
1 parent 0da9ce2 commit dc929b6

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

ext/fiddle/lib/fiddle/cparser.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,11 @@ def parse_signature(signature, tymap=nil)
148148
#
149149
def parse_ctype(ty, tymap=nil)
150150
tymap ||= {}
151-
case ty
152-
when Array
151+
if ty.is_a?(Array)
153152
return [parse_ctype(ty[0], tymap), ty[1]]
153+
end
154+
ty = ty.gsub(/\Aconst\s+/, "")
155+
case ty
154156
when 'void'
155157
return TYPE_VOID
156158
when /\A(?:(?:signed\s+)?long\s+long(?:\s+int\s+)?|int64_t)(?:\s+\w+)?\z/

test/fiddle/test_cparser.rb

Lines changed: 50 a 10000 dditions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,61 +12,88 @@ class TestCParser < TestCase
1212

1313
def test_char_ctype
1414
assert_equal(TYPE_CHAR, parse_ctype('char'))
15+
assert_equal(TYPE_CHAR, parse_ctype('const char'))
1516
assert_equal(TYPE_CHAR, parse_ctype('signed char'))
17+
assert_equal(TYPE_CHAR, parse_ctype('const signed char'))
1618
assert_equal(-TYPE_CHAR, parse_ctype('unsigned char'))
19+
assert_equal(-TYPE_CHAR, parse_ctype('const unsigned char'))
1720
end
1821

1922
def test_short_ctype
2023
assert_equal(TYPE_SHORT, parse_ctype('short'))
24+
assert_equal(TYPE_SHORT, parse_ctype('const short'))
2125
assert_equal(TYPE_SHORT, parse_ctype('short int'))
26+
assert_equal(TYPE_SHORT, parse_ctype('const short int'))
2227
assert_equal(TYPE_SHORT, parse_ctype('signed short'))
28+
assert_equal(TYPE_SHORT, parse_ctype('const signed short'))
2329
assert_equal(TYPE_SHORT, parse_ctype('signed short int'))
30+
assert_equal(TYPE_SHORT, parse_ctype('const signed short int'))
2431
assert_equal(-TYPE_SHORT, parse_ctype('unsigned short'))
32+
assert_equal(-TYPE_SHORT, parse_ctype('const unsigned short'))
2533
assert_equal(-TYPE_SHORT, parse_ctype('unsigned short int'))
34+
assert_equal(-TYPE_SHORT, parse_ctype('const unsigned short int'))
2635
end
2736

2837
def test_int_ctype
2938
assert_equal(TYPE_INT, parse_ctype('int'))
39+
assert_equal(TYPE_INT, parse_ctype('const int'))
3040
assert_equal(TYPE_INT, parse_ctype('signed int'))
41+
assert_equal(TYPE_INT, parse_ctype('const signed int'))
3142
assert_equal(-TYPE_INT, parse_ctype('uint'))
43+
assert_equal(-TYPE_INT, parse_ctype('const uint'))
3244
assert_equal(-TYPE_INT, parse_ctype('unsigned int'))
45+
assert_equal(-TYPE_INT, parse_ctype('const unsigned int'))
3346
end
3447

3548
def test_long_ctype
3649
assert_equal(TYPE_LONG, parse_ctype('long'))
50+
assert_equal(TYPE_LONG, parse_ctype('const long'))
3751
assert_equal(TYPE_LONG, parse_ctype('long int'))
52+
assert_equal(TYPE_LONG, parse_ctype('const long int'))
3853
assert_equal(TYPE_LONG, parse_ctype('signed long'))
54+
assert_equal(TYPE_LONG, parse_ctype('const signed long'))
3955
assert_equal(TYPE_LONG, parse_ctype('signed long int'))
56+
assert_equal(TYPE_LONG, parse_ctype('const signed long int'))
4057
assert_equal(-TYPE_LONG, parse_ctype('unsigned long'))
58+
assert_equal(-TYPE_LONG, parse_ctype('const unsigned long'))
4159
assert_equal(-TYPE_LONG, parse_ctype('unsigned long int'))
60+
assert_equal(-TYPE_LONG, parse_ctype('const unsigned long int'))
4261
end
4362

4463
def test_size_t_ctype
4564
assert_equal(TYPE_SIZE_T, parse_ctype("size_t"))
65+
assert_equal(TYPE_SIZE_T, parse_ctype("const size_t"))
4666
end
4767

4868
def test_ssize_t_ctype
4969
assert_equal(TYPE_SSIZE_T, parse_ctype("ssize_t"))
70+
assert_equal(TYPE_SSIZE_T, parse_ctype("const ssize_t"))
5071
end
5172

5273
def test_ptrdiff_t_ctype
5374
assert_equal(TYPE_PTRDIFF_T, parse_ctype("ptrdiff_t"))
75+
assert_equal(TYPE_PTRDIFF_T, parse_ctype("const ptrdiff_t"))
5476
end
5577

5678
def test_intptr_t_ctype
5779
assert_equal(TYPE_INTPTR_T, parse_ctype("intptr_t"))
80+
assert_equal(TYPE_INTPTR_T, parse_ctype("const intptr_t"))
5881
end
5982

6083
def test_uintptr_t_ctype
6184
assert_equal(TYPE_UINTPTR_T, parse_ctype("uintptr_t"))
85+
assert_equal(TYPE_UINTPTR_T, parse_ctype("const uintptr_t"))
6286
end
6387

6488
def test_undefined_ctype
6589
assert_raise(DLError) { parse_ctype('DWORD') }
6690
end
6791

6892
def test_undefined_ctype_with_type_alias
69-
assert_equal(-TYPE_LONG, parse_ctype('DWORD', {"DWORD" => "unsigned long"}))
93+
assert_equal(-TYPE_LONG,
94+
parse_ctype('DWORD', {"DWORD" => "unsigned long"}))
95+
assert_equal(-TYPE_LONG,
96+
parse_ctype('const DWORD', {"DWORD" => "unsigned long"}))
7097
end
7198

7299
def expand_struct_types(types)
@@ -83,11 +110,21 @@ def expand_struct_types(types)
83110
end
84111

85112
def test_struct_basic
86-
assert_equal [[TYPE_INT, TYPE_CHAR], ['i', 'c']], parse_struct_signature(['int i', 'char c'])
113+
assert_equal([[TYPE_INT, TYPE_CHAR], ['i', 'c']],
114+
parse_struct_signature(['int i', 'char c']))
115+
assert_equal([[TYPE_INT, TYPE_CHAR], ['i', 'c']],
116+
parse_struct_signature(['const int i', 'const char c']))
87117
end
88118

89119
def test_struct_array
90-
assert_equal [[[TYPE_CHAR,80],[TYPE_INT,5]], ['buffer','x']], parse_struct_signature(['char buffer[80]', 'int[5] x'])
120+
assert_equal([[[TYPE_CHAR, 80], [TYPE_INT, 5]],
121+
['buffer', 'x']],
122+
parse_struct_signature(['char buffer[80]',
123+
'int[5] x']))
124+
assert_equal([[[TYPE_CHAR, 80], [TYPE_INT, 5]],
125+
['buffer', 'x']],
126+
parse_struct_signature(['const char buffer[80]',
127+
'const int[5] x']))
91128
end
92129

93130
def test_struct_nested_struct
@@ -178,15 +215,22 @@ def test_struct_double_nested_struct_outer_array
178215
end
179216

180217
def test_struct_array_str
181-
assert_equal [[[TYPE_CHAR,80],[TYPE_INT,5]], ['buffer','x']], parse_struct_signature('char buffer[80], int[5] x')
218+
assert_equal([[[TYPE_CHAR, 80], [TYPE_INT, 5]],
219+
['buffer', 'x']],
220+
parse_struct_signature('char buffer[80], int[5] x'))
221+
assert_equal([[[TYPE_CHAR, 80], [TYPE_INT, 5]],
222+
['buffer', 'x']],
223+
parse_struct_signature('const char buffer[80], const int[5] x'))
182224
end
183225

184226
def test_struct_function_pointer
185-
assert_equal [[TYPE_VOIDP], ['cb']], parse_struct_signature(['void (*cb)(const char*)'])
227+
assert_equal([[TYPE_VOIDP], ['cb']],
228+
parse_struct_signature(['void (*cb)(const char*)']))
186229
end
187230

188231
def test_struct_function_pointer_str
189-
assert_equal [[TYPE_VOIDP,TYPE_VOIDP], ['cb', 'data']], parse_struct_signature('void (*cb)(const char*), const char* data')
232+
assert_equal([[TYPE_VOIDP, TYPE_VOIDP], ['cb', 'data']],
233+
parse_struct_signature('void (*cb)(const char*), const char* data'))
190234
end
191235

192236
def test_struct_string

0 commit comments

Comments
 (0)
0