Skip to content

Commit ae631cb

Browse files
committed
fix: C variables should never show up in Ancestors tree
If a NormalClass's superclass is a C enclosure, then update the superclass to point to the RDoc::NormalClass. This is done in a single pass after all files have been parsed. Fixes #1205.
1 parent ac2a151 commit ae631cb

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

lib/rdoc/rdoc.rb

+2
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ def parse_files files
415415
parse_file filename
416416
end.compact
417417

418+
@store.resolve_c_references
419+
418420
@stats.done_adding
419421
@options = original_options
420422

lib/rdoc/store.rb

+13
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ def add_file absolute_name, relative_name: absolute_name, parser: nil
197197
top_level
198198
end
199199

200+
##
201+
# Make sure any references to C variable names are resolved to the corresponding class.
202+
#
203+
204+
def resolve_c_references
205+
# superclass references
206+
@classes_hash.each do |_, klass|
207+
if String === klass.superclass && (candidate = find_c_enclosure(klass.superclass))
208+
klass.superclass = candidate
209+
end
210+
end
211+
end
212+
200213
##
201214
# Sets the parser of +absolute_name+, unless it from a source code file.
202215

test/rdoc/test_rdoc_store.rb

+20
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,26 @@ def test_find_c_enclosure_from_cache_legacy
281281
assert_nil @s.find_c_enclosure('cObject')
282282
end
283283

284+
def test_resolve_c_references
285+
# first parse a child that references an unknown parent
286+
c_file1 = @s.add_file 'ext1.c'
287+
c_file1.add_class RDoc::NormalClass, 'Child', 'cExternParent'
288+
289+
# then parse the parent and register the C variable name as a C enclosure
290+
c_file2 = @s.add_file 'ext2.c'
291+
parent = c_file2.add_class RDoc::NormalClass, 'Parent', 'rb_cObject'
292+
293+
@s.add_c_enclosure('cExternParent', parent)
294+
295+
# at this point, the child's superclass is still the name of the C variable
296+
assert_equal("cExternParent", @s.classes_hash['Child'].superclass)
297+
298+
@s.resolve_c_references
299+
300+
# now the ancestor tree correctly references the NormalClass objects
301+
assert_equal(parent, @s.classes_hash['Child'].superclass)
302+
end
303+
284304
def test_find_class_named
285305
assert_equal @c1, @store.find_class_named('C1')
286306

0 commit comments

Comments
 (0)