Skip to content
Open
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
28 changes: 25 additions & 3 deletions lib/rdoc/rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,25 @@ def parse_file(filename)
this is not your library please report a bug to the author.
EOF
rescue => e
$stderr.puts <<-EOF
syntax_check_command = syntax_check_command_for filename, parser&.class
syntax_check_message = if syntax_check_command
<<~MESSAGE
Before reporting this, could you check that the file you're documenting
has proper syntax:

#{Gem.ruby} -c #{filename}
#{syntax_check_command}
MESSAGE
else
<<~MESSAGE
Before reporting this, could you check that the file you're documenting
has proper syntax for its language?
MESSAGE
end

RDoc is not a full Ruby parser and will fail when fed invalid ruby programs.
$stderr.puts <<-EOF
#{syntax_check_message}
RDoc's parsers are not full language parsers and may fail when fed invalid
source files.

The internal error was:

Expand All @@ -376,6 +388,16 @@ def parse_file(filename)
raise e
end

def syntax_check_command_for(filename, parser_class = RDoc::Parser.can_parse_by_name(filename))
if parser_class == RDoc::Parser::Ruby
"#{Gem.ruby} -c #{filename}"
elsif parser_class == RDoc::Parser::C
cc = ENV['CC']
cc = 'cc' if cc.nil? || cc.empty?
"#{cc} -fsyntax-only #{filename}"
end
end

##
# Returns the relative path for +filename+ against +options.root+ (and
# +options.page_dir+ when set). This is the key used by RDoc::Store to
Expand Down
17 changes: 17 additions & 0 deletions test/rdoc/rdoc_rdoc_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,23 @@ def test_parse_file_forbidden
tf.close!
end

def test_syntax_check_command_for_c_file
command = @rdoc.syntax_check_command_for 'extension.c'

assert_includes command, ' -fsyntax-only extension.c'
refute_includes command, "#{Gem.ruby} -c"
end

def test_syntax_check_command_for_ruby_file
command = @rdoc.syntax_check_command_for 'library.rb'

assert_equal "#{Gem.ruby} -c library.rb", command
end

def test_syntax_check_command_for_unknown_file_type
assert_nil @rdoc.syntax_check_command_for('README')
end

def test_remove_unparseable
file_list = %w[
blah.class
Expand Down
Loading