Skip to content

Commit

Permalink
Merge #164
Browse files Browse the repository at this point in the history
164: Set cfg(coverage_nightly) when nightly compiler is used r=taiki-e a=taiki-e

Closes #157

Co-authored-by: Taiki Endo <te316e89@gmail.com>
  • Loading branch information
bors[bot] and taiki-e authored May 11, 2022
2 parents 3a8b3e1 + 7791a44 commit fa586d0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com
## [Unreleased]

- cargo-llvm-cov no longer changes the current directory when running cargo. ([#161](https://github.com/taiki-e/cargo-llvm-cov/pull/161))

- Exclude build script from report by default. ([#163](https://github.com/taiki-e/cargo-llvm-cov/pull/163))
You can use `--include-build-script` flag to include build script in report.

- Set `cfg(coverage_nightly)` when nightly compiler is used. ([#164](https://github.com/taiki-e/cargo-llvm-cov/pull/164))

## [0.3.3] - 2022-05-06

- Fix an issue where codes in the target directory are not being properly excluded from reports when using `show-env` subcommand. ([#156](https://github.com/taiki-e/cargo-llvm-cov/pull/156))
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ OPTIONS:
Hide instantiations from report

--no-cfg-coverage
Unset cfg(coverage)
Unset cfg(coverage), which is enabled when code is built using cargo-llvm-cov

--no-cfg-coverage-nightly
Unset cfg(coverage_nightly), which is enabled when code is built using cargo-llvm-cov
and nightly compiler

--no-report
Run tests, but don't generate coverage report
Expand Down Expand Up @@ -362,12 +366,14 @@ cargo llvm-cov --open --ignore-filename-regex build

To exclude the specific function from coverage, use the [`#[no_coverage]` attribute][rust-lang/rust#84605].

Since `#[no_coverage]` is unstable, it is recommended to use it together with `cfg(coverage)` set by cargo-llvm-cov.
Since `#[no_coverage]` is unstable, it is recommended to use it together with `cfg(coverage)` or `cfg(coverage_nightly)` set by cargo-llvm-cov.

```rust
#![cfg_attr(coverage, feature(no_coverage))]
// cfg(coverage) is true when code is built using cargo-llvm-cov
// cfg(coverage_nightly) is true when code is built using cargo-llvm-cov and nightly compiler
#![cfg_attr(coverage_nightly, feature(no_coverage))]

#[cfg_attr(coverage, no_coverage)]
#[cfg_attr(coverage_nightly, no_coverage)]
fn exclude_from_coverage() {
// ...
}
Expand Down
2 changes: 2 additions & 0 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub(crate) struct Workspace {

cargo: PathBuf,
rustc: PathBuf,
pub(crate) nightly: bool,
/// Whether `-C instrument-coverage` is available.
pub(crate) stable_coverage: bool,
}
Expand Down Expand Up @@ -84,6 +85,7 @@ impl Workspace {
profdata_file,
cargo: cargo.into(),
rustc,
nightly,
stable_coverage,
})
}
Expand Down
5 changes: 4 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,12 @@ pub(crate) struct LlvmCovOptions {
/// Hide instantiations from report
#[clap(long)]
pub(crate) hide_instantiations: bool,
/// Unset cfg(coverage)
/// Unset cfg(coverage), which is enabled when code is built using cargo-llvm-cov.
#[clap(long)]
pub(crate) no_cfg_coverage: bool,
/// Unset cfg(coverage_nightly), which is enabled when code is built using cargo-llvm-cov and nightly compiler.
#[clap(long)]
pub(crate) no_cfg_coverage_nightly: bool,
/// Run tests, but don't generate coverage report
#[clap(long)]
pub(crate) no_report: bool,
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ fn set_env(cx: &Context, target: &mut impl EnvTarget) {
if !cx.cov.no_cfg_coverage {
rustflags.push_str(" --cfg coverage");
}
if cx.ws.nightly && !cx.cov.no_cfg_coverage_nightly {
rustflags.push_str(" --cfg coverage_nightly");
}
if cx.build.target.is_none() {
// https://github.com/dtolnay/trybuild/pull/121
// https://github.com/dtolnay/trybuild/issues/122
Expand All @@ -273,6 +276,9 @@ fn set_env(cx: &Context, target: &mut impl EnvTarget) {
if !cx.cov.no_cfg_coverage {
rustdocflags.push_str(" --cfg coverage");
}
if cx.ws.nightly && !cx.cov.no_cfg_coverage_nightly {
rustdocflags.push_str(" --cfg coverage_nightly");
}
}

target.set("RUSTFLAGS", rustflags);
Expand Down
6 changes: 5 additions & 1 deletion tests/long-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ OPTIONS:
Hide instantiations from report

--no-cfg-coverage
Unset cfg(coverage)
Unset cfg(coverage), which is enabled when code is built using cargo-llvm-cov

--no-cfg-coverage-nightly
Unset cfg(coverage_nightly), which is enabled when code is built using cargo-llvm-cov
and nightly compiler

--no-report
Run tests, but don't generate coverage report
Expand Down
6 changes: 5 additions & 1 deletion tests/short-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ OPTIONS:
Hide instantiations from report

--no-cfg-coverage
Unset cfg(coverage)
Unset cfg(coverage), which is enabled when code is built using cargo-llvm-cov

--no-cfg-coverage-nightly
Unset cfg(coverage_nightly), which is enabled when code is built using cargo-llvm-cov
and nightly compiler

--no-report
Run tests, but don't generate coverage report
Expand Down

0 comments on commit fa586d0

Please sign in to comment.