Method: RDoc::Parser::C#find_body

Defined in:
lib/rdoc/parser/c.rb

#find_body(class_name, meth_name, meth_obj, file_content, quiet = false) ⇒ Object

Find the C code corresponding to a Ruby method



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# File 'lib/rdoc/parser/c.rb', line 599

def find_body class_name, meth_name, meth_obj, file_content, quiet = false
  if file_content
    @body_table ||= {}
    @body_table[file_content] ||= gen_body_table file_content
    type, *args = @body_table[file_content][meth_name]
  end

  case type
  when :func_def
    comment = new_comment args[0], @top_level, :c
    body = args[1]
    offset, = args[2]

    comment.remove_private if comment

    # try to find the whole body
    body = $& if /#{Regexp.escape body}[^(]*?\{.*?^\}/m =~ file_content

    # The comment block may have been overridden with a 'Document-method'
    # block. This happens in the interpreter when multiple methods are
    # vectored through to the same C method but those methods are logically
    # distinct (for example Kernel.hash and Kernel.object_id share the same
    # implementation

    override_comment = find_override_comment class_name, meth_obj
    comment = override_comment if override_comment

    comment.normalize
    find_modifiers comment, meth_obj if comment

    #meth_obj.params = params
    meth_obj.start_collecting_tokens
    tk = { :line_no => 1, :char_no => 1, :text => body }
    meth_obj.add_token tk
    meth_obj.comment = comment
    meth_obj.line    = file_content[0, offset].count("\n") + 1

    body
  when :macro_def
    comment = new_comment args[0], @top_level, :c
    body = args[1]
    offset, = args[2]

    find_body class_name, args[3], meth_obj, file_content, true

    comment.normalize
    find_modifiers comment, meth_obj

    meth_obj.start_collecting_tokens
    tk = { :line_no => 1, :char_no => 1, :text => body }
    meth_obj.add_token tk
    meth_obj.comment = comment
    meth_obj.line    = file_content[0, offset].count("\n") + 1

    body
  when :macro_alias
    # with no comment we hope the aliased definition has it and use it's
    # definition

    body = find_body(class_name, args[0], meth_obj, file_content, true)

    return body if body

    @options.warn "No definition for #{meth_name}"
    false
  else # No body, but might still have an override comment
    comment = find_override_comment class_name, meth_obj

    if comment then
      comment.normalize
      find_modifiers comment, meth_obj
      meth_obj.comment = comment

      ''
    else
      @options.warn "No definition for #{meth_name}"
      false
    end
  end
end