taiki-e / cargo-llvm-cov

Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).
Apache License 2.0
933 stars 57 forks source link

Ignore `cfg!`-gated code when disabled #158

Open stouset opened 2 years ago

stouset commented 2 years ago

I have some code that's gated by an

if cfg!(config_setting) {
    // do a thing
} else {
    // do a different thing
}

Given that only one of these branches can be hit statically, I'd appreciate being able to disable coverage for the lines that are excluded. Not sure how possible this is.

taiki-e commented 2 years ago

Using #[cfg] instead of cfg! will work as you expect (it is also consistent with how the compiler evaluates the code). For example:

https://github.com/taiki-e/cargo-llvm-cov/blob/a405503bbe508ceb3e9ab7246579eb72bcb49c51/tests/fixtures/coverage-reports/cargo_config/cargo_config.hide-instantiations.txt#L11-L14

Appling the same behavior to cfg! is not always good, considering that coverages generated under different test conditions can be merged. -- For now, I think it is reasonable to leave it up to the way the compiler evaluates the code for #[cfg] and cfg!.

stouset commented 2 years ago

I tried to merge two runs using the cfg flag on and off, and the coverage report would only ever show one or the other was hit. I followed the instructions pretty closely. This may be buggy.

Unfortunately while #[cfg(…)] works in the general case, it can also cause a ton of warnings as the Rust compiler no longer sees the other branch so might not see mut variables being mutated, imported modules being used, etc.

stouset commented 2 years ago

I don't have a minimal test-case for the first paragraph above yet. I'm neck-deep in some stuff, will try to get you one.