Skip to content

Commit

Permalink
Add support for rb_define_alias
Browse files Browse the repository at this point in the history
Closes #413
  • Loading branch information
lsegal committed Dec 3, 2011
1 parent 1eac919 commit 9ef0928
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/yard/parser/c_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def parse
parse_modules
parse_classes
parse_methods
parse_aliases
parse_attributes
parse_constants
parse_includes
Expand Down Expand Up @@ -100,6 +101,30 @@ def handle_method(scope, var_name, name, func_name, source_file = nil)
obj
end

def handle_alias(var_name, new_name, old_name)
namespace = P(remove_var_prefix(var_name))
ensure_loaded!(namespace)
new_meth, old_meth = new_name.to_sym, old_name.to_sym
old_obj = namespace.child(:name => old_meth, :scope => :instance)
new_obj = YARD::CodeObjects::MethodObject.new(namespace, new_meth, :instance) do |o|
o.visibility = :public
o.scope = :instance
o.add_file(@file)
o.source_type = :c
end

if old_obj
new_obj.signature = old_obj.signature
new_obj.source = old_obj.source
new_obj.docstring = old_obj.docstring
new_obj.docstring.object = new_obj
else
new_obj.signature = "def #{new_meth}" # this is all we know.
end

namespace.aliases[new_obj] = old_meth
end

def handle_attribute(var_name, name, func_name, read, write, source_file = nil)
values = {:read => read.to_i, :write => write.to_i}
{:read => name, :write => "#{name}="}.each do |type, meth_name|
Expand Down Expand Up @@ -379,6 +404,18 @@ def parse_methods
end
end

def parse_aliases
@content.scan(%r{rb_define_alias
\s*\(\s*([\w\.]+),
\s*"([^"]+)",
\s*"([^"]+)"\s*\)
}xm) do |var_name, new_name, old_name|

var_name = "rb_cObject" if var_name == "rb_mKernel"
handle_alias(var_name, new_name, old_name)
end
end

def parse_attributes
@content.scan(%r{rb_define_attr
\s*\(\s*([\w\.]+),
Expand Down
22 changes: 22 additions & 0 deletions spec/parser/c_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,28 @@ def run(read, write, commented = nil)
Registry.at('Foo#foo=').should be_writer
end
end

describe 'Defining aliases' do
before do
Registry.clear
end

it "should allow defining of aliases (rb_define_alias)" do
@contents = <<-eof
/* FOO */
VALUE foo(VALUE x) { int value = x; }
void Init_Foo() {
rb_cFoo = rb_define_class("Foo", rb_cObject);
rb_define_method(rb_cFoo, "foo", foo, 1);
rb_define_alias(rb_cFoo, "bar", "foo");
}
eof
parse

Registry.at('Foo#bar').should be_is_alias
Registry.at('Foo#bar').docstring.should == 'FOO'
end
end
end

describe '#find_override_comment' do
Expand Down

0 comments on commit 9ef0928

Please sign in to comment.