Skip to content

Commit

Permalink
Reapply "[ruby/rdoc] fix: C variables should never show up in Ancesto…
Browse files Browse the repository at this point in the history
…rs tree"

This reverts commit 0fe82ae.
  • Loading branch information
hsbt committed Dec 3, 2024
1 parent 59d2317 commit d85e8b5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/rdoc/rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ def parse_files files
parse_file filename
end.compact

@store.resolve_c_superclasses

@stats.done_adding
@options = original_options

Expand Down
12 changes: 12 additions & 0 deletions lib/rdoc/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ def add_file absolute_name, relative_name: absolute_name, parser: nil
top_level
end

##
# Make sure any references to C variable names are resolved to the corresponding class.
#

def resolve_c_superclasses
@classes_hash.each_value do |klass|
if klass.superclass.is_a?(String) && (candidate = find_c_enclosure(klass.superclass))
klass.superclass = candidate
end
end
end

##
# Sets the parser of +absolute_name+, unless it from a source code file.

Expand Down
20 changes: 20 additions & 0 deletions test/rdoc/test_rdoc_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,26 @@ def test_find_c_enclosure_from_cache_legacy
assert_nil @s.find_c_enclosure('cObject')
end

def test_resolve_c_superclasses
# first parse a child that references an unknown parent
c_file1 = @s.add_file 'ext1.c'
c_file1.add_class RDoc::NormalClass, 'Child', 'cExternParent'

# then parse the parent and register the C variable name as a C enclosure
c_file2 = @s.add_file 'ext2.c'
parent = c_file2.add_class RDoc::NormalClass, 'Parent', 'rb_cObject'

@s.add_c_enclosure('cExternParent', parent)

# at this point, the child's superclass is still the name of the C variable
assert_equal("cExternParent", @s.classes_hash['Child'].superclass)

@s.resolve_c_superclasses

# now the ancestor tree correctly references the NormalClass objects
assert_equal(parent, @s.classes_hash['Child'].superclass)
end

def test_find_class_named
assert_equal @c1, @store.find_class_named('C1')

Expand Down

0 comments on commit d85e8b5

Please sign in to comment.