Skip to content

Commit 458ecbb

Browse files
committed
Fix to parse rb_define_global_const
ruby/ruby#12357
1 parent 7cd125e commit 458ecbb

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

lib/rdoc/parser/c.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ def do_constants
405405
\s*(.*?)\s*\)\s*;
406406
%xm) do |type, var_name, const_name, definition|
407407
var_name = "rb_cObject" if !var_name or var_name == "rb_mKernel"
408+
type = "const" if type == "global_const"
408409
handle_constants type, var_name, const_name, definition
409410
end
410411

@@ -760,6 +761,10 @@ def gen_const_table file_content
760761
rb_define_(?<type>\w+)\(\s*(?:\w+),\s*
761762
"(?<name>\w+)"\s*,
762763
.*?\)\s*;
764+
| (?<doc>(?>^\s*/\*.*?\*/\s+))
765+
rb_define_global_(?<type>const)\(\s*
766+
"(?<name>\w+)"\s*,
767+
.*?\)\s*;
763768
| (?<doc>(?>^\s*/\*.*?\*/\s+))
764769
rb_file_(?<type>const)\(\s*
765770
"(?<name>\w+)"\s*,

test/rdoc/test_rdoc_parser_c.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,38 @@ def test_do_constants
577577
assert constants.empty?, constants.inspect
578578
end
579579

580+
def test_do_constants_global
581+
content = <<-'EOF'
582+
#include <ruby.h>
583+
584+
void Init_foo(){
585+
586+
/* Toplevel const */
587+
rb_define_global_const("ANSWER", INT2FIX(42));
588+
589+
}
590+
EOF
591+
592+
@parser = util_parser content
593+
594+
@parser.do_classes_and_modules
595+
@parser.do_constants
596+
597+
klass = @parser.classes['rb_cObject']
598+
assert klass
599+
600+
constants = klass.constants
601+
assert !klass.constants.empty?
602+
603+
assert_equal @top_level, constants.first.file
604+
605+
constants = constants.map { |c| [c.name, c.value, c.comment.text] }
606+
assert_equal ['ANSWER', 'INT2FIX(42)', "Toplevel const "],
607+
constants.shift
608+
609+
assert constants.empty?, constants.inspect
610+
end
611+
580612
def test_do_constants_curses
581613
content = <<-EOF
582614
void Init_curses(){
@@ -1037,6 +1069,21 @@ def test_find_const_comment_rb_define
10371069
assert_equal "/*\n * A comment\n */\n", comment.text
10381070
end
10391071

1072+
def test_find_const_comment_rb_define_global
1073+
content = <<-EOF
1074+
/*
1075+
* A comment
1076+
*/
1077+
rb_define_global_const("CONST", value);
1078+
EOF
1079+
1080+
parser = util_parser content
1081+
1082+
comment = parser.find_const_comment 'const', 'CONST'
1083+
1084+
assert_equal "/*\n * A comment\n */\n", comment.text
1085+
end
1086+
10401087
def test_find_const_comment_document_const
10411088
content = <<-EOF
10421089
/*

0 commit comments

Comments
 (0)