Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: C variables should never show up in Ancestors tree #1217

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(#superclass can return either string or RDoc::NormalClass really hurts my brain 🙈)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And of course the bug in #1221 is related to this confusion. 🤦

end

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

Expand Down