From 458ecbb7f7d67f1f66ab05ece06cf5c24ce9b0ad Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 16 Dec 2024 23:49:00 +0900 Subject: [PATCH] Fix to parse `rb_define_global_const` https://github.com/ruby/ruby/pull/12357 --- lib/rdoc/parser/c.rb | 5 ++++ test/rdoc/test_rdoc_parser_c.rb | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lib/rdoc/parser/c.rb b/lib/rdoc/parser/c.rb index 8a1bf821ce..7e83a6151f 100644 --- a/lib/rdoc/parser/c.rb +++ b/lib/rdoc/parser/c.rb @@ -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 @@ -760,6 +761,10 @@ def gen_const_table file_content rb_define_(?\w+)\(\s*(?:\w+),\s* "(?\w+)"\s*, .*?\)\s*; + | (?(?>^\s*/\*.*?\*/\s+)) + rb_define_global_(?const)\(\s* + "(?\w+)"\s*, + .*?\)\s*; | (?(?>^\s*/\*.*?\*/\s+)) rb_file_(?const)\(\s* "(?\w+)"\s*, diff --git a/test/rdoc/test_rdoc_parser_c.rb b/test/rdoc/test_rdoc_parser_c.rb index ab4f149869..31702a7cd0 100644 --- a/test/rdoc/test_rdoc_parser_c.rb +++ b/test/rdoc/test_rdoc_parser_c.rb @@ -577,6 +577,38 @@ def test_do_constants assert constants.empty?, constants.inspect end + def test_do_constants_global + content = <<-'EOF' +#include + +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(){ @@ -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 /*