Skip to content

Commit a2008fc

Browse files
committed
Only grab adjacent comments when looking for class comments
1 parent b55defb commit a2008fc

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

History.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
* Fixed relative link generation.
2828
* Fix crash, RDoc now ignores comments above local variable assignments in
2929
modules.
30+
* RDoc now only accepts adjacent comments for rb_define_module and
31+
rb_define_class
3032

3133
=== 2.2.1 / 2008-09-24
3234
This version provides some minor fixes and enhancements to 2.2.0 intended

lib/rdoc/parser/c.rb

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,21 @@ class RDoc::Parser::C < RDoc::Parser
9696

9797
parse_files_matching(/\.(?:([CcHh])\1?|c([+xp])\2|y)\z/)
9898

99-
@@enclosure_classes = {}
100-
@@known_bodies = {}
99+
##
100+
# C file the parser is parsing
101+
102+
attr_accessor :content
103+
104+
##
105+
# Resets cross-file state. Call when parsing different projects that need
106+
# separate documentation.
107+
108+
def self.reset
109+
@@enclosure_classes = {}
110+
@@known_bodies = {}
111+
end
112+
113+
reset
101114

102115
##
103116
# Prepare to parse a C file
@@ -365,25 +378,18 @@ def find_class(raw_name, name)
365378

366379
def find_class_comment(class_name, class_meth)
367380
comment = nil
381+
368382
if @content =~ %r{((?>/\*.*?\*/\s+))
369383
(static\s+)?void\s+Init_#{class_name}\s*(?:_\(\s*)?\(\s*(?:void\s*)\)}xmi then
370384
comment = $1
371-
elsif @content =~ %r{Document-(?:class|module):\s#{class_name}\s*?(?:<\s+[:,\w]+)?\n((?>.*?\*/))}m
385+
elsif @content =~ %r{Document-(?:class|module):\s+#{class_name}\s*?(?:<\s+[:,\w]+)?\n((?>.*?\*/))}m then
386+
comment = $1
387+
elsif @content =~ %r{((?>/\*.*?\*/\s+))
388+
([\w\.\s]+\s* = \s+)?rb_define_(class|module).*?"(#{class_name})"}xm then
372389
comment = $1
373-
else
374-
if @content =~ /rb_define_(class|module)/m then
375-
class_name = class_name.split("::").last
376-
comments = []
377-
@content.split(/(\/\*.*?\*\/)\s*?\n/m).each_with_index do |chunk, index|
378-
comments[index] = chunk
379-
if chunk =~ /rb_define_(class|module).*?"(#{class_name})"/m then
380-
comment = comments[index-1]
381-
break
382-
end
383-
end
384-
end
385390
end
386-
class_meth.comment = mangle_comment(comment) if comment
391+
392+
class_meth.comment = mangle_comment comment if comment
387393
end
388394

389395
##
@@ -492,9 +498,10 @@ def handle_class_module(var_name, class_mod, class_name, parent, in_module)
492498
@stats.add_module cm
493499
end
494500

495-
cm.record_location(enclosure.toplevel)
501+
cm.record_location enclosure.toplevel
502+
503+
find_class_comment cm.full_name, cm
496504

497-
find_class_comment(cm.full_name, cm)
498505
@classes[var_name] = cm
499506
@@enclosure_classes[var_name] = cm
500507
@known_classes[var_name] = cm.full_name

test/test_rdoc_parser_c.rb

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ def setup
2121
@fn = filename
2222
@options = RDoc::Options.new
2323
@stats = RDoc::Stats.new 0
24+
25+
RDoc::Parser::C.reset
26+
RDoc::TopLevel.reset
2427
end
2528

2629
def teardown
@@ -137,12 +140,12 @@ def test_do_constants
137140
}
138141
EOF
139142

140-
parser = util_parser content
143+
@parser = util_parser content
141144

142-
parser.do_classes
143-
parser.do_constants
145+
@parser.do_classes
146+
@parser.do_constants
144147

145-
klass = parser.classes['cFoo']
148+
klass = @parser.classes['cFoo']
146149
assert klass
147150

148151
constants = klass.constants
@@ -223,7 +226,7 @@ def test_find_class_comment_define_class
223226

224227
klass = util_get_class content, 'foo'
225228

226-
assert_equal " \n a comment for class Foo\n ", klass.comment
229+
assert_equal " \n a comment for class Foo\n \n", klass.comment
227230
end
228231

229232
def test_find_class_comment_define_class_Init_Foo
@@ -245,6 +248,26 @@ def test_find_class_comment_define_class_Init_Foo
245248
assert_equal " \n a comment for class Foo on Init\n \n", klass.comment
246249
end
247250

251+
def test_find_class_comment_define_class_bogus_comment
252+
content = <<-EOF
253+
/*
254+
* a comment for other_function
255+
*/
256+
void
257+
other_function() {
258+
}
259+
260+
void
261+
Init_Foo(void) {
262+
VALUE foo = rb_define_class("Foo", rb_cObject);
263+
}
264+
EOF
265+
266+
klass = util_get_class content, 'foo'
267+
268+
assert_equal '', klass.comment
269+
end
270+
248271
def test_define_method
249272
content = <<-EOF
250273
/*Method Comment! */
@@ -273,9 +296,9 @@ def test_define_method
273296
end
274297

275298
def util_get_class(content, name)
276-
parser = util_parser content
277-
parser.scan
278-
parser.classes[name]
299+
@parser = util_parser content
300+
@parser.scan
301+
@parser.classes[name]
279302
end
280303

281304
def util_parser(content)

0 commit comments

Comments
 (0)