diff --git a/ChangeLog b/ChangeLog index a3eb6995e4..a82d5d8dd6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +Thu Dec 13 16:53:10 2012 Eric Hodel + + * lib/rdoc/class_module.rb: Fixed duplicate comments for classes and + modules from C. + * test/rdoc/test_rdoc_class_module.rb: Test for the above. + + * lib/rdoc/parser/c.rb: Reload C variable names to allow proper + updates of an ri store for C files. + * lib/rdoc/rdoc.rb: ditto. + * lib/rdoc/store.rb: ditto. + * test/rdoc/test_rdoc_parser_c.rb: Test for the above. + * test/rdoc/test_rdoc_store.rb: ditto. + Thu Dec 13 14:20:00 2012 Zachary Scott * lib/irb*: merge doc from doc/irb/ird.rd and improve overall diff --git a/lib/rdoc/class_module.rb b/lib/rdoc/class_module.rb index a520503598..1dbe57bdf1 100644 --- a/lib/rdoc/class_module.rb +++ b/lib/rdoc/class_module.rb @@ -31,7 +31,7 @@ class RDoc::ClassModule < RDoc::Context ## # Comment and the location it came from. Use #add_comment to add comments - attr_reader :comment_location + attr_accessor :comment_location attr_accessor :diagram # :nodoc: @@ -134,6 +134,9 @@ class RDoc::ClassModule < RDoc::Context else normalize_comment comment end + + @comment_location.delete_if { |(_, l)| l == location } + @comment_location << [comment, location] self.comment = original diff --git a/lib/rdoc/parser/c.rb b/lib/rdoc/parser/c.rb index d371ed0e6a..968ca3c789 100644 --- a/lib/rdoc/parser/c.rb +++ b/lib/rdoc/parser/c.rb @@ -122,6 +122,11 @@ class RDoc::Parser::C < RDoc::Parser include RDoc::Text + ## + # Maps C variable names to names of ruby classes or modules + + attr_reader :classes + ## # C file the parser is parsing @@ -150,16 +155,23 @@ class RDoc::Parser::C < RDoc::Parser attr_reader :singleton_classes ## - # Prepare to parse a C file + # The TopLevel items in the parsed file belong to - def initialize(top_level, file_name, content, options, stats) + attr_reader :top_level + + ## + # Prepares for parsing a C file. See RDoc::Parser#initialize for details on + # the arguments. + + def initialize top_level, file_name, content, options, stats super @known_classes = RDoc::KNOWN_CLASSES.dup - @content = handle_tab_width handle_ifdefs_in(@content) - @classes = {} - @singleton_classes = {} - @file_dir = File.dirname(@file_name) + @content = handle_tab_width handle_ifdefs_in @content + @file_dir = File.dirname @file_name + + @classes = load_variable_map :c_class_variables + @singleton_classes = load_variable_map :c_singleton_class_variables # missing variable => [handle_class_module arguments] @missing_dependencies = {} @@ -386,13 +398,12 @@ class RDoc::Parser::C < RDoc::Parser def do_includes @content.scan(/rb_include_module\s*\(\s*(\w+?),\s*(\w+?)\s*\)/) do |c,m| - if cls = @classes[c] - m = @known_classes[m] || m + next unless cls = @classes[c] + m = @known_classes[m] || m - comment = RDoc::Comment.new '', @top_level - incl = cls.add_include RDoc::Include.new(m, comment) - incl.record_location @top_level - end + comment = RDoc::Comment.new '', @top_level + incl = cls.add_include RDoc::Include.new(m, comment) + incl.record_location @top_level end end @@ -801,7 +812,7 @@ class RDoc::Parser::C < RDoc::Parser parent_name = @known_classes[parent] || parent if in_module then - enclosure = @classes[in_module] || @store.c_enclosure_classes[in_module] + enclosure = @classes[in_module] || @store.find_c_enclosure(in_module) if enclosure.nil? and enclosure = @known_classes[in_module] then enc_type = /^rb_m/ =~ in_module ? :module : :class @@ -848,8 +859,8 @@ class RDoc::Parser::C < RDoc::Parser end @classes[var_name] = cm - @store.c_enclosure_classes[var_name] = cm @known_classes[var_name] = cm.full_name + @store.add_c_enclosure var_name, cm end ## @@ -1004,6 +1015,30 @@ class RDoc::Parser::C < RDoc::Parser end end + ## + # Loads the variable map with the given +name+ from the RDoc::Store, if + # present. + + def load_variable_map map_name + return {} unless files = @store.cache[map_name] + return {} unless name_map = files[@file_name] + + class_map = {} + + name_map.each do |variable, name| + next unless mod = @store.find_class_or_module(name) + + class_map[variable] = if map_name == :c_class_variables then + mod + else + name + end + @known_classes[variable] = name + end + + class_map + end + ## # Look for directives in a normal comment block: # @@ -1136,6 +1171,9 @@ class RDoc::Parser::C < RDoc::Parser do_includes do_aliases do_attrs + + @store.add_c_variables self + @top_level end diff --git a/lib/rdoc/rdoc.rb b/lib/rdoc/rdoc.rb index 76da5b4289..cff5e78a20 100644 --- a/lib/rdoc/rdoc.rb +++ b/lib/rdoc/rdoc.rb @@ -476,9 +476,12 @@ The internal error was: @store.dry_run = @options.dry_run @store.main = @options.main_page @store.title = @options.title + @store.path = @options.op_dir @start_time = Time.now + @store.load_cache + file_info = parse_files @options.files @options.default_title = "RDoc Documentation" diff --git a/lib/rdoc/store.rb b/lib/rdoc/store.rb index cf9ee07625..9beced6423 100644 --- a/lib/rdoc/store.rb +++ b/lib/rdoc/store.rb @@ -69,7 +69,19 @@ class RDoc::Store # Stores the name of the C variable a class belongs to. This helps wire up # classes defined from C across files. - attr_reader :c_enclosure_classes + attr_reader :c_enclosure_classes # :nodoc: + + attr_reader :c_enclosure_names # :nodoc: + + ## + # Maps C variables to class or module names for each parsed C file. + + attr_reader :c_class_variables + + ## + # Maps C variables to singleton class names for each parsed C file. + + attr_reader :c_singleton_class_variables ## # If true this Store will not write any files @@ -114,15 +126,17 @@ class RDoc::Store @type = type @cache = { - :ancestors => {}, - :attributes => {}, - :class_methods => {}, - :encoding => @encoding, - :instance_methods => {}, - :main => nil, - :modules => [], - :pages => [], - :title => nil, + :ancestors => {}, + :attributes => {}, + :class_methods => {}, + :c_class_variables => {}, + :c_singleton_class_variables => {}, + :encoding => @encoding, + :instance_methods => {}, + :main => nil, + :modules => [], + :pages => [], + :title => nil, } @classes_hash = {} @@ -130,11 +144,34 @@ class RDoc::Store @files_hash = {} @c_enclosure_classes = {} + @c_enclosure_names = {} + + @c_class_variables = {} + @c_singleton_class_variables = {} @unique_classes = nil @unique_modules = nil end + ## + # Adds +module+ as an enclosure (namespace) for the given +variable+ for C + # files. + + def add_c_enclosure variable, namespace + @c_enclosure_classes[variable] = namespace + end + + ## + # Adds C variables from an RDoc::Parser::C + + def add_c_variables c_parser + filename = c_parser.top_level.relative_name + + @c_class_variables[filename] = make_variable_map c_parser.classes + + @c_singleton_class_variables[filename] = c_parser.singleton_classes + end + ## # Adds the file with +name+ as an RDoc::TopLevel to the store. Returns the # created RDoc::TopLevel. @@ -304,6 +341,28 @@ class RDoc::Store @files_hash end + ## + # Finds the enclosure (namespace) for the given C +variable+. + + def find_c_enclosure variable + @c_enclosure_classes.fetch variable do + break unless name = @c_enclosure_names[variable] + + mod = find_class_or_module name + + unless mod then + loaded_mod = load_class_data name + + file = loaded_mod.in_files.first + file.store = self + + mod = file.add_module RDoc::NormalModule, name + end + + @c_enclosure_classes[variable] = mod + end + end + ## # Finds the class with +name+ in all discovered classes @@ -500,22 +559,26 @@ class RDoc::Store @encoding = load_enc unless @encoding - @cache[:pages] ||= [] - @cache[:main] ||= nil + @cache[:pages] ||= [] + @cache[:main] ||= nil + @cache[:c_class_variables] ||= {} + @cache[:c_singleton_class_variables] ||= {} + + @cache[:c_class_variables].each do |_, map| + map.each do |variable, name| + @c_enclosure_names[variable] = name + end + end @cache rescue Errno::ENOENT end ## - # Loads ri data for +klass_name+ + # Loads ri data for +klass_name+ and hooks it up to this store. def load_class klass_name - file = class_file klass_name - - obj = open file, 'rb' do |io| - Marshal.load io.read - end + obj = load_class_data klass_name obj.store = self @@ -525,6 +588,17 @@ class RDoc::Store when RDoc::NormalModule then @modules_hash[klass_name] = obj end + end + + ## + # Loads ri data for +klass_name+ + + def load_class_data klass_name + file = class_file klass_name + + obj = open file, 'rb' do |io| + Marshal.load io.read + end rescue Errno::ENOENT => e error = MissingFileError.new(self, file, klass_name) error.set_backtrace e.backtrace @@ -583,6 +657,20 @@ class RDoc::Store @cache[:main] = page end + ## + # Converts the variable => ClassModule map +variables+ from a C parser into + # a variable => class name map. + + def make_variable_map variables + map = {} + + variables.each { |variable, class_module| + map[variable] = class_module.full_name + } + + map + end + ## # Path to the ri data for +method_name+ in +klass_name+ @@ -688,6 +776,9 @@ class RDoc::Store @cache[:encoding] = @encoding # this gets set twice due to assert_cache + @cache[:c_class_variables].merge! @c_class_variables + @cache[:c_singleton_class_variables].merge! @c_singleton_class_variables + return if @dry_run marshal = Marshal.dump @cache diff --git a/test/rdoc/test_rdoc_class_module.rb b/test/rdoc/test_rdoc_class_module.rb index e5d855b2cd..670f732290 100644 --- a/test/rdoc/test_rdoc_class_module.rb +++ b/test/rdoc/test_rdoc_class_module.rb @@ -41,6 +41,16 @@ class TestRDocClassModule < XrefTestCase assert_equal 'comment', cm.comment.text end + def test_add_comment_duplicate + tl1 = @store.add_file 'one.rb' + + cm = RDoc::ClassModule.new 'Klass' + cm.add_comment '# comment 1', tl1 + cm.add_comment '# comment 2', tl1 + + assert_equal [['comment 2', tl1]], cm.comment_location + end + def test_add_comment_stopdoc tl = @store.add_file 'file.rb' diff --git a/test/rdoc/test_rdoc_parser_c.rb b/test/rdoc/test_rdoc_parser_c.rb index e739476b74..bc93be803b 100644 --- a/test/rdoc/test_rdoc_parser_c.rb +++ b/test/rdoc/test_rdoc_parser_c.rb @@ -40,12 +40,6 @@ assert call-seq correct =end -class RDoc::Parser::C - attr_accessor :classes - - public :do_classes, :do_constants -end - class TestRDocParserC < RDoc::TestCase def setup @@ -83,6 +77,37 @@ class TestRDocParserC < RDoc::TestCase assert_equal c_parser, c_parser.can_parse('file.y') end + def test_initialize + some_ext = @top_level.add_class RDoc::NormalClass, 'SomeExt' + @top_level.add_class RDoc::SingleClass, 'SomeExtSingle' + + @store.cache[:c_class_variables] = { + @fn => { 'cSomeExt' => 'SomeExt' } + } + + @store.cache[:c_singleton_class_variables] = { + @fn => { 'cSomeExtSingle' => 'SomeExtSingle' } + } + + parser = RDoc::Parser::C.new @top_level, @fn, '', @options, @stats + + expected = { 'cSomeExt' => some_ext } + assert_equal expected, parser.classes + + expected = { 'cSomeExtSingle' => 'SomeExtSingle' } + assert_equal expected, parser.singleton_classes + + expected = { + 'cSomeExt' => 'SomeExt', + 'cSomeExtSingle' => 'SomeExtSingle', + } + known_classes = parser.known_classes.delete_if do |key, _| + RDoc::KNOWN_CLASSES.keys.include? key + end + + assert_equal expected, known_classes + end + def test_do_attr_rb_attr content = <<-EOF void Init_Blah(void) { @@ -1317,6 +1342,81 @@ void Init_Blah(void) { assert_equal 'not_handled', @top_level.metadata['other'] end + def test_load_variable_map + some_ext = @top_level.add_class RDoc::NormalClass, 'SomeExt' + @top_level.add_class RDoc::NormalClass, 'OtherExt' + + @store.cache[:c_class_variables][@fn] = { 'cSomeExt' => 'SomeExt' } + @store.cache[:c_class_variables]['other.c'] = { 'cOtherExt' => 'OtherExt' } + + parser = util_parser + + map = parser.load_variable_map :c_class_variables + + expected = { 'cSomeExt' => some_ext } + + assert_equal expected, map + + assert_equal 'SomeExt', parser.known_classes['cSomeExt'] + assert_nil parser.known_classes['cOtherExt'] + end + + def test_load_variable_map_empty + parser = util_parser + + map = parser.load_variable_map :c_class_variables + + assert_empty map + end + + def test_load_variable_map_legacy + @store.cache[:c_class_variables] = nil + + parser = util_parser + + map = parser.load_variable_map :c_class_variables + + assert_empty map + end + + def test_load_variable_map_singleton + @top_level.add_class RDoc::NormalClass, 'SomeExt' + @top_level.add_class RDoc::NormalClass, 'OtherExt' + + @store.cache[:c_singleton_class_variables][@fn] = + { 'cSomeExt' => 'SomeExt' } + @store.cache[:c_singleton_class_variables]['other.c'] = + { 'cOtherExt' => 'OtherExt' } + + parser = util_parser + + map = parser.load_variable_map :c_singleton_class_variables + + expected = { 'cSomeExt' => 'SomeExt' } + + assert_equal expected, map + + assert_equal 'SomeExt', parser.known_classes['cSomeExt'] + assert_nil parser.known_classes['cOtherExt'] + end + + def test_load_variable_map_trim + a = @top_level.add_class RDoc::NormalClass, 'A' + + @store.cache[:c_class_variables][@fn] = { + 'cA' => 'A', + 'cB' => 'B', + } + + parser = util_parser + + map = parser.load_variable_map :c_class_variables + + expected = { 'cA' => a } + + assert_equal expected, map + end + def test_define_method content = <<-EOF /*Method Comment! */ @@ -1523,6 +1623,29 @@ Init_IO(void) { parser.rb_scan_args('rb_scan_args(a, b, "*:&",)') end + def test_scan + parser = util_parser <<-C +void Init(void) { + mM = rb_define_module("M"); + cC = rb_define_class("C", rb_cObject); + sC = rb_singleton_class(cC); +} + C + + parser.scan + + expected = { + @fn => { + 'mM' => 'M', + 'cC' => 'C', }} + assert_equal expected, @store.c_class_variables + + expected = { + @fn => { + 'sC' => 'C' } } + assert_equal expected, @store.c_singleton_class_variables + end + def test_scan_order_dependent parser = util_parser <<-C void a(void) { diff --git a/test/rdoc/test_rdoc_store.rb b/test/rdoc/test_rdoc_store.rb index a5a5adec7b..3b4f7cf2ff 100644 --- a/test/rdoc/test_rdoc_store.rb +++ b/test/rdoc/test_rdoc_store.rb @@ -18,6 +18,7 @@ class TestRDocStore < XrefTestCase @klass = @top_level.add_class RDoc::NormalClass, 'Object' @klass.add_comment 'original', @top_level + @klass.record_location @top_level @cmeth = RDoc::AnyMethod.new nil, 'cmethod' @cmeth.singleton = true @@ -87,15 +88,17 @@ class TestRDocStore < XrefTestCase @s.clean_cache_collection ancestors expected = { - :ancestors => ancestors, - :attributes => attrs, - :class_methods => cmethods, - :encoding => nil, - :instance_methods => imethods, - :modules => modules, - :pages => pages, - :main => main, - :title => title, + :ancestors => ancestors, + :attributes => attrs, + :class_methods => cmethods, + :c_class_variables => {}, + :c_singleton_class_variables => {}, + :encoding => nil, + :instance_methods => imethods, + :modules => modules, + :pages => pages, + :main => main, + :title => title, } @s.save_cache @@ -115,6 +118,38 @@ class TestRDocStore < XrefTestCase refute File.exist?(path), "#{path} exists" end + def test_add_c_enclosure + @s.add_c_enclosure 'cC1', @c1 + + expected = { 'cC1' => @c1 } + + assert_equal expected, @s.c_enclosure_classes + end + + def test_add_c_variables + options = RDoc::Options.new + + c_file = @s.add_file 'ext.c' + + some_ext = c_file.add_class RDoc::NormalClass, 'SomeExt' + c_file.add_class RDoc::SingleClass, 'SomeExtSingle' + + c_parser = RDoc::Parser::C.new c_file, 'ext.c', '', options, nil + + c_parser.classes['cSomeExt'] = some_ext + c_parser.singleton_classes['s_cSomeExt'] = 'SomeExtSingle' + + @s.add_c_variables c_parser + + expected = { 'ext.c' => { 'cSomeExt' => 'SomeExt' } } + + assert_equal expected, @s.c_class_variables + + expected = { 'ext.c' => { 's_cSomeExt' => 'SomeExtSingle' } } + + assert_equal expected, @s.c_singleton_class_variables + end + def test_add_file top_level = @store.add_file 'file.rb' @@ -214,6 +249,31 @@ class TestRDocStore < XrefTestCase refute_empty a1.aliases end + def test_find_c_enclosure + assert_nil @s.find_c_enclosure 'cC1' + + @s.add_c_enclosure 'cC1', @c1 + + assert_equal @c1, @s.find_c_enclosure('cC1') + end + + def test_find_c_enclosure_from_cache + @s.save_class @klass + @s.classes_hash.clear + + assert_nil @s.find_c_enclosure 'cObject' + + @s.c_enclosure_names['cObject'] = 'Object' + + klass = @s.find_c_enclosure('cObject') + assert_equal @klass, klass + + assert_empty klass.comment_location + assert_equal @top_level, klass.parent + + assert_includes @s.c_enclosure_classes, 'cObject' + end + def test_find_class_named assert_equal @c1, @store.find_class_named('C1') @@ -329,11 +389,15 @@ class TestRDocStore < XrefTestCase def test_load_cache cache = { - :encoding => :encoding_value, - :methods => { "Object" => %w[Object#method] }, - :main => @page.full_name, - :modules => %w[Object], - :pages => [], + :c_class_variables => + { 'file.c' => { 'cKlass' => 'Klass' } }, + :c_singleton_class_variables => + { 'file.c' => { 'sKlass' => 'KlassSingle' } }, + :encoding => :encoding_value, + :methods => { "Object" => %w[Object#method] }, + :main => @page.full_name, + :modules => %w[Object], + :pages => [], } Dir.mkdir @tmpdir @@ -348,17 +412,28 @@ class TestRDocStore < XrefTestCase assert_equal :encoding_value, @s.encoding assert_equal 'README.txt', @s.main + + expected = { 'file.c' => { 'cKlass' => 'Klass' } } + assert_equal expected, @s.cache[:c_class_variables] + + expected = { 'file.c' => { 'sKlass' => 'KlassSingle' } } + assert_equal expected, @s.cache[:c_singleton_class_variables] + + expected = { 'cKlass' => 'Klass' } + assert_equal expected, @s.c_enclosure_names end def test_load_cache_encoding_differs skip "Encoding not implemented" unless Object.const_defined? :Encoding cache = { - :encoding => Encoding::ISO_8859_1, - :main => nil, - :methods => { "Object" => %w[Object#method] }, - :modules => %w[Object], - :pages => [], + :c_class_variables => {}, + :c_singleton_class_variables => {}, + :encoding => Encoding::ISO_8859_1, + :main => nil, + :methods => { "Object" => %w[Object#method] }, + :modules => %w[Object], + :pages => [], } Dir.mkdir @tmpdir @@ -378,15 +453,17 @@ class TestRDocStore < XrefTestCase def test_load_cache_no_cache cache = { - :ancestors => {}, - :attributes => {}, - :class_methods => {}, - :encoding => nil, - :instance_methods => {}, - :main => nil, - :modules => [], - :pages => [], - :title => nil, + :ancestors => {}, + :attributes => {}, + :class_methods => {}, + :c_class_variables => {}, + :c_singleton_class_variables => {}, + :encoding => nil, + :instance_methods => {}, + :main => nil, + :modules => [], + :pages => [], + :title => nil, } @s.load_cache @@ -404,6 +481,8 @@ class TestRDocStore < XrefTestCase :modules => %w[Object], # no :pages # no :main + # no :c_class_variables + # no :c_singleton_class_variables } Dir.mkdir @tmpdir @@ -415,14 +494,16 @@ class TestRDocStore < XrefTestCase @s.load_cache expected = { - :ancestors => {}, - :attributes => {}, - :class_methods => {}, - :encoding => :encoding_value, - :instance_methods => { "Object" => %w[Object#method] }, - :main => nil, - :modules => %w[Object], - :pages => [], + :ancestors => {}, + :attributes => {}, + :class_methods => {}, + :c_class_variables => {}, + :c_singleton_class_variables => {}, + :encoding => :encoding_value, + :instance_methods => { "Object" => %w[Object#method] }, + :main => nil, + :modules => %w[Object], + :pages => [], } assert_equal expected, @s.cache @@ -534,6 +615,8 @@ class TestRDocStore < XrefTestCase }, :attributes => { 'Object' => ['attr_accessor attr'] }, :class_methods => { 'Object' => %w[cmethod] }, + :c_class_variables => {}, + :c_singleton_class_variables => {}, :instance_methods => { 'Object' => %w[attr method method! method_bang], 'Object::SubClass' => %w[method], @@ -564,16 +647,44 @@ class TestRDocStore < XrefTestCase @s.main = @page.full_name @s.title = 'title' + options = RDoc::Options.new + + c_file = @s.add_file 'ext.c' + + some_ext = c_file.add_class RDoc::NormalClass, 'SomeExt' + c_file.add_class RDoc::SingleClass, 'SomeExtSingle' + + c_parser = RDoc::Parser::C.new c_file, 'ext.c', '', options, nil + + c_parser.classes['cSomeExt'] = some_ext + c_parser.singleton_classes['s_cSomeExt'] = 'SomeExtSingle' + + @s.add_c_variables c_parser + @s.save_cache assert_file File.join(@tmpdir, 'cache.ri') + c_class_variables = { + 'ext.c' => { + 'cSomeExt' => 'SomeExt' + } + } + + c_singleton_class_variables = { + 'ext.c' => { + 's_cSomeExt' => 'SomeExtSingle' + } + } + expected = { :ancestors => { 'Object::SubClass' => %w[Incl Object], }, :attributes => { 'Object' => ['attr_accessor attr'] }, :class_methods => { 'Object' => %w[cmethod] }, + :c_class_variables => c_class_variables, + :c_singleton_class_variables => c_singleton_class_variables, :instance_methods => { 'Object' => %w[method method! method_bang], 'Object::SubClass' => %w[method],