43

I have enabled code coverage statistics in Xcode 7.0 and Objective C (like this) and it's working well.

Is it possible to mark some source lines so that they are ignored by the coverage report? If I was using lcov then I could use LCOV_EXCL_START and LCOV_EXCL_END markers (as in How to tell lcov to ignore lines in the source files) but Xcode doesn't recognize those.

Does Xcode have an alternative mechanism for doing this?

RK-
  • 12,099
  • 23
  • 89
  • 155
Ewan Mellor
  • 6,747
  • 1
  • 24
  • 39
  • 2
    When I was young and unexperienced I was doing that too, then I realized that it's futile attempting to reach 100% coverage by ignoring some parts of code. Don't try to play your reports by ignoring code. – Sulthan Sep 30 '15 at 00:47
  • The only way I can see is using a post-action for Test when you find the `Coverage.profdata` file in the build directory and update it. It's probably possible to export it to gcov using llvm-cov tool. – Sulthan Sep 30 '15 at 01:58
  • 8
    @Sulthan I am neither young nor inexperienced. I would like to exclude lines such as asserts that a particular line is unreachable. There's a lot of value in knowing at a glance that every reachable line in a function is covered, and not having to check whether the bright red line in Xcode is actually just an assert. – Ewan Mellor Sep 30 '15 at 04:37
  • @Sulthan I'm not interested in exporting to another tool; I could do that before. The point of this question is to use the display in Xcode 7. – Ewan Mellor Sep 30 '15 at 04:38
  • That's what I meant, However there is no such functionality by default so if you want to do that, you have to update the coverage file - which is a binary file. So, one way of doing that would be to convert the new profdata format to `gcov` format (which is human readable and to ignore lines you can just use a smart `sed` command) and then convert it back to `profdata`. and let Xcode read the updated file. The only technical problem is the profdata - gcov conversion. – Sulthan Sep 30 '15 at 11:33

1 Answers1

7

Xcode7 and later (based on some forum posts), the coverage system uses LLVM's coverage generation and reporting mechanisms, the format for which is detailed at http://llvm.org/docs/CoverageMappingFormat.html. As of Xcode 9, this format does not support any means of exclusion of lines (or other structures).

The resulting mapping is exported into a consumable format (txt or html) by llvm-cov, which also doesn't really have much in the way of exclusion mechanisms. llvm-cov does have some simple thresholding for only reporting on "greater than" or "less than" coverage for both lines and regions, but I suspect that's not entirely what you're after based on the question above.

heckj
  • 7,136
  • 3
  • 39
  • 50
  • 2
    I appreciate the answer, even if it is unsatisfactory. Developers are heavily encouraged to put Guard statements and other checks into the code, that under normal operations should never be invoked. This means, that we are encouraged to put line of code in that would never be covered. – Steve Sheets Apr 12 '19 at 21:19