From 7791a445cf0662405a4a1a8be973ef739cc7f366 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Thu, 12 May 2022 08:13:38 +0900 Subject: [PATCH] Set cfg(coverage_nightly) when nightly compiler is used --- CHANGELOG.md | 3 +++ README.md | 14 ++++++++++---- src/cargo.rs | 2 ++ src/cli.rs | 5 ++++- src/main.rs | 6 ++++++ tests/long-help.txt | 6 +++++- tests/short-help.txt | 6 +++++- 7 files changed, 35 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c560ae7..bbd096ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/README.md b/README.md index 63f652d7..4d7bb2aa 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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() { // ... } diff --git a/src/cargo.rs b/src/cargo.rs index 41e4d942..572ab825 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -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, } @@ -84,6 +85,7 @@ impl Workspace { profdata_file, cargo: cargo.into(), rustc, + nightly, stable_coverage, }) } diff --git a/src/cli.rs b/src/cli.rs index 604ec3f5..a249d91d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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, diff --git a/src/main.rs b/src/main.rs index 7d1201ab..1770c227 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 @@ -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); diff --git a/tests/long-help.txt b/tests/long-help.txt index 206f79e2..310d05cb 100644 --- a/tests/long-help.txt +++ b/tests/long-help.txt @@ -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 diff --git a/tests/short-help.txt b/tests/short-help.txt index 76495e8c..307cdebc 100644 --- a/tests/short-help.txt +++ b/tests/short-help.txt @@ -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