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

Use llvm-cov export to detect source files per binary #398

Conversation

blackm00n
Copy link
Contributor

When binary contains single source file, output of llvm-cov show does not contain the file name. That leads to source_file_pathname to contain actual source code (first line of it) instead of a path.

Previously, result of find_source_files method was used to understand if a binary is a single source:

source_files = find_source_files || []
…
# If a single source file is used, the resulting output does not contain the file name.
coverage_file.source_file_pathname = source_files.first if source_files.count == 1

Though that approach won’t work for projects which contains several binaries (and then source_files will be a mix of files from different binaries) or when you don’t provide list of source file in configuration (it’s what find_source_files returns). So we either need to provide the list of files per binary or somehow get the info from profdata file.

llvm-cov export tool exports the coverage data in JSON format and it doesn’t depend on whether there is a single source file or more in a binary. So I propose to use it and parse JSON output to get a list of source files prior to actually running llvm-cov show command.

That makes optimizations on lines 134-138 obsolete.

When binary contains single source file, output of `llvm-cov show` does not contain the file name. That leads to `source_file_pathname` to contain actual source code (first line of it) instead of a path.

Previously, result of `find_source_files` method was used to understand if a binary is a single source:
```
source_files = find_source_files || []
…
# If a single source file is used, the resulting output does not contain the file name.
coverage_file.source_file_pathname = source_files.first if source_files.count == 1
```
Though that approach won’t work for projects which contains several binaries (and then `source_files` will be a mix of files from different binaries) or when you don’t provide list of source file in configuration (it’s what `find_source_files` returns). So we either need to provide the list of files per binary or somehow get the info from profdata file.

`llvm-cov export` tool exports the coverage data in JSON format and it doesn’t depend on whether there is a single source file or more in a binary. So I propose to use it and parse JSON output to get a list of source files prior to actually running `llvm-cov show` command.

That makes optimizations on lines 134-138 obsolete.
@coveralls
Copy link

coveralls commented Jun 6, 2018

Coverage Status

Coverage decreased (-0.1%) to 95.636% when pulling 2d05457 on blackm00n:use.export.summary.for.source.files.detection into 69b7411 on SlatherOrg:master.

@blackm00n
Copy link
Contributor Author

Stack trace for slather coverage --html --show in case of Xcode 9.3.1, multibinary project containing one binary with single source file:

NoMethodError: undefined method `each' for nil:NilClass
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/slather-036f14725a82/lib/slather/profdata_coverage_file.rb:45:in `source_file_pathname'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/slather-036f14725a82/lib/slather/profdata_coverage_file.rb:197:in `ignored?'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/slather-036f14725a82/lib/slather/project.rb:132:in `block (2 levels) in profdata_coverage_files'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/slather-036f14725a82/lib/slather/project.rb:128:in `map'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/slather-036f14725a82/lib/slather/project.rb:128:in `block in profdata_coverage_files'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/slather-036f14725a82/lib/slather/project.rb:125:in `each'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/slather-036f14725a82/lib/slather/project.rb:125:in `profdata_coverage_files'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/slather-036f14725a82/lib/slather/project.rb:98:in `coverage_files'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/slather-036f14725a82/lib/slather/coverage_service/html_output.rb:26:in `post'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/slather-036f14725a82/lib/slather/command/coverage_command.rb:97:in `post'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/slather-036f14725a82/lib/slather/command/coverage_command.rb:59:in `execute'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/clamp-0.6.5/lib/clamp/command.rb:67:in `run'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/clamp-0.6.5/lib/clamp/subcommand/execution.rb:11:in `execute'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/clamp-0.6.5/lib/clamp/command.rb:67:in `run'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/clamp-0.6.5/lib/clamp/command.rb:132:in `run'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/slather-036f14725a82/bin/slather:17:in `<top (required)>'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bin/slather:23:in `load'
  /Users/alexey.kozhevnikov/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bin/slather:23:in `<top (required)>'

For Xcode <= 9.2 compatibility
@blackm00n
Copy link
Contributor Author

I removed -summary-only parameter, as it was introduced only in LLVM 6.0.0 (Xcode 9.3 and later).

@blackm00n
Copy link
Contributor Author

I will fix the tests if the overall approach is tolerable.

@KeimyPlaza
Copy link

@blackm00n any delivery planning for this issue?

@blackm00n
Copy link
Contributor Author

@ksuther please check this PR when you have free time

Small coverage decrease is caused by the introduction of unsafe_llvm_cov_export_output which is almost identical to previously existing unsafe_profdata_llvm_cov_output, doesn't look critical to me

@ksuther ksuther merged commit ad8415d into SlatherOrg:master Jul 10, 2018
@ksuther
Copy link
Contributor

ksuther commented Jul 10, 2018

Thanks for the changes! Using the JSON output definitely provides some more certainty. In the long run it looks like the way forward is xccov, but that's not something I have time to get into right now.

gcov has been unused for long enough that any xccov implementation should probably drop gcov and llvm-cov and use xccov exclusively (and become slather 3). That's all hypothetical at this point :)

@ksuther
Copy link
Contributor

ksuther commented Jul 10, 2018

Also, if some other people could confirm that this PR fixes their issues I'll push an update to RubyGems.

@dunyakirkali
Copy link

@ksuther It did fix my issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants