Skip to content

Commit

Permalink
Fix to parse rb_define_global_const
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Dec 16, 2024
1 parent 7cd125e commit 458ecbb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/rdoc/parser/c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def do_constants
\s*(.*?)\s*\)\s*;
%xm) do |type, var_name, const_name, definition|
var_name = "rb_cObject" if !var_name or var_name == "rb_mKernel"
type = "const" if type == "global_const"
handle_constants type, var_name, const_name, definition
end

Expand Down Expand Up @@ -760,6 +761,10 @@ def gen_const_table file_content
rb_define_(?<type>\w+)\(\s*(?:\w+),\s*
"(?<name>\w+)"\s*,
.*?\)\s*;
| (?<doc>(?>^\s*/\*.*?\*/\s+))
rb_define_global_(?<type>const)\(\s*
"(?<name>\w+)"\s*,
.*?\)\s*;
| (?<doc>(?>^\s*/\*.*?\*/\s+))
rb_file_(?<type>const)\(\s*
"(?<name>\w+)"\s*,
Expand Down
47 changes: 47 additions & 0 deletions test/rdoc/test_rdoc_parser_c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,38 @@ def test_do_constants
assert constants.empty?, constants.inspect
end

def test_do_constants_global
content = <<-'EOF'
#include <ruby.h>
void Init_foo(){
/* Toplevel const */
rb_define_global_const("ANSWER", INT2FIX(42));
}
EOF

@parser = util_parser content

@parser.do_classes_and_modules
@parser.do_constants

klass = @parser.classes['rb_cObject']
assert klass

constants = klass.constants
assert !klass.constants.empty?

assert_equal @top_level, constants.first.file

constants = constants.map { |c| [c.name, c.value, c.comment.text] }
assert_equal ['ANSWER', 'INT2FIX(42)', "Toplevel const "],
constants.shift

assert constants.empty?, constants.inspect
end

def test_do_constants_curses
content = <<-EOF
void Init_curses(){
Expand Down Expand Up @@ -1037,6 +1069,21 @@ def test_find_const_comment_rb_define
assert_equal "/*\n * A comment\n */\n", comment.text
end

def test_find_const_comment_rb_define_global
content = <<-EOF
/*
* A comment
*/
rb_define_global_const("CONST", value);
EOF

parser = util_parser content

comment = parser.find_const_comment 'const', 'CONST'

assert_equal "/*\n * A comment\n */\n", comment.text
end

def test_find_const_comment_document_const
content = <<-EOF
/*
Expand Down

0 comments on commit 458ecbb

Please sign in to comment.