Skip to content

Commit d8182b6

Browse files
committed
Tag the callback types
1 parent a7cb8a3 commit d8182b6

File tree

4 files changed

+89
-19
lines changed

4 files changed

+89
-19
lines changed

lib/docurium/docparser.rb

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,15 @@ def extract_typedef(cursor)
111111
#puts "typed struct, #{cursor.spelling}"
112112
rec.merge! extract_struct(child)
113113
when :cursor_parm_decl
114-
#puts "have parm #{cursor.spelling}, #{cursor.display_name}"
115-
subject, desc = extract_subject_desc(cursor.comment)
116-
rec[:decl] = cursor.spelling
117-
rec[:description] = subject
118-
rec[:comments] = desc
114+
rec.merge! extract_function(cursor)
115+
rec[:type] = :callback
116+
# this is wasteful, but we don't get the array from outside
117+
cmt = extract_function_comment(cursor.comment)
118+
ret = {
119+
:type => extract_callback_result(rec[:underlying_type]),
120+
:comment => cmt[:return]
121+
}
122+
rec[:return] = ret
119123
else
120124
raise "No idea how to handle #{child.kind}"
121125
end
@@ -125,21 +129,14 @@ def extract_typedef(cursor)
125129
rec
126130
end
127131

128-
def extract_subject_desc(comment)
129-
subject = comment.child.text
130-
desc = (comment.find_all { |cmt| cmt.kind == :comment_paragraph }).drop(1).map(&:text).join("\n\n")
131-
return subject, desc
132+
def extract_callback_result(type)
133+
type[0..(type.index('(') - 1)].strip
132134
end
133135

134-
def extract_function(cursor)
135-
comment = cursor.comment
136-
137-
#puts "looking at function #{cursor.spelling}, #{cursor.display_name}"
138-
cmt = extract_function_comment(comment)
139-
136+
def extract_function_args(cursor, cmt)
140137
# We only want to look at parm_decl to avoid looking at a return
141138
# struct as a parameter
142-
args = children(cursor)
139+
children(cursor)
143140
.select {|c| c.kind == :cursor_parm_decl }
144141
.map do |arg|
145142
{
@@ -148,6 +145,20 @@ def extract_function(cursor)
148145
:comment => cmt[:args][arg.display_name],
149146
}
150147
end
148+
end
149+
150+
def extract_subject_desc(comment)
151+
subject = comment.child.text
152+
desc = (comment.find_all { |cmt| cmt.kind == :comment_paragraph }).drop(1).map(&:text).join("\n\n")
153+
return subject, desc
154+
end
155+
156+
def extract_function(cursor)
157+
comment = cursor.comment
158+
159+
#puts "looking at function #{cursor.spelling}, #{cursor.display_name}"
160+
cmt = extract_function_comment(comment)
161+
args = extract_function_args(cursor, cmt)
151162
#args = args.reject { |arg| arg[:comment].nil? }
152163

153164
ret = {

test/fixtures/git2/callback.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* This file is to create a function which takes a callback in order
3+
* to test that we detect it correctly and will write it out.
4+
*/
5+
6+
#include "common.h"
7+
8+
/**
9+
* Worker which does soemthing to its pointer
10+
*/
11+
typedef int (*git_callback_do_work)(int *foo);
12+
13+
/**
14+
* Schedule some work to happen for a particular function
15+
*/
16+
GIT_EXTERN(int) git_work_schedule(git_callback_do_work worker);

test/parser_test.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,41 @@ def test_type_reference
406406

407407
end
408408

409+
def test_callaback
410+
name = 'typeref.h'
411+
contents = <<EOF
412+
/**
413+
* This is a callback type
414+
*
415+
* @return whether to reschedule
416+
*/
417+
typedef int (*some_callback)(int *foo);
418+
EOF
419+
420+
actual = parse(name, contents)
421+
expected = [{
422+
:file => "typeref.h",
423+
:line => 6,
424+
:lineto => 6,
425+
:tdef => :typedef,
426+
:name => "some_callback",
427+
:underlying_type => "int (*)(int *)",
428+
:type => :callback,
429+
:body => ' some_callback(int *foo);',
430+
:description => ' This is a callback type',
431+
:comments => ' ',
432+
:sig => "int *",
433+
:args => [{:name => "foo",
434+
:type => "int *",
435+
:comment => nil,
436+
}],
437+
:return => {:type => 'int',
438+
:comment => " whether to reschedule"},
439+
:decl => ' some_callback(int *foo)',
440+
:argline => 'int *foo',
441+
}]
442+
443+
assert_equal actual, expected
444+
end
445+
409446
end

test/repo_test.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def teardown
3939

4040
def test_can_parse_headers
4141
keys = @data.keys.map { |k| k.to_s }.sort
42-
assert_equal ['files', 'functions', 'globals', 'groups', 'prefix', 'types'], keys
43-
assert_equal 153, @data[:functions].size
42+
assert_equal ['callbacks', 'files', 'functions', 'globals', 'groups', 'prefix', 'types'], keys
43+
assert_equal 154, @data[:functions].size
4444
end
4545

4646
def test_can_extract_enum_from_define
@@ -114,7 +114,7 @@ def test_can_get_the_full_description_from_multi_liners
114114
end
115115

116116
def test_can_group_functions
117-
assert_equal 14, @data[:groups].size
117+
assert_equal 15, @data[:groups].size
118118
group, funcs = @data[:groups].first
119119
assert_equal 'blob', group
120120
assert_equal 6, funcs.size
@@ -127,4 +127,10 @@ def test_can_store_mutliple_enum_doc_sections
127127
assert_equal 2, idxentry[1][:sections].size
128128
end
129129

130+
def test_can_parse_callback
131+
cb = @data[:callbacks]['git_callback_do_work']
132+
assert_equal 'int', cb[:return][:type]
133+
134+
end
135+
130136
end

0 commit comments

Comments
 (0)