Skip to content

Commit

Permalink
Add --mcdc flag, and handle MC/DC fields correctly in LLVM JSON
Browse files Browse the repository at this point in the history
This adds a new unstable `--mcdc` flag, similar to `--branch`.

Apart from that, the LLVM JSON parser also needed changes to recognize the mcdc-related fields, as it would otherwise silently drop those.
  • Loading branch information
Swatinem committed Aug 21, 2024
1 parent 7d4bb5e commit e9370d2
Show file tree
Hide file tree
Showing 41 changed files with 406 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ jobs:
pushd tests/fixtures/crates/cargo_config >/dev/null
# TODO: --fail-under-branches?
cargo llvm-cov test --branch --text --fail-under-lines 80
cargo llvm-cov test --mcdc --text --fail-under-lines 80
popd >/dev/null
pushd easytime >/dev/null
cargo llvm-cov test --branch --doctests --text --fail-under-lines 30
cargo llvm-cov test --mcdc --doctests --text --fail-under-lines 30
popd >/dev/null
# Test minimum runnable Cargo version.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ OPTIONS:
--branch
Enable branch coverage. (unstable)

--mcdc
Enable mcdc coverage. (unstable)

--doctests
Including doc tests (unstable)

Expand Down
3 changes: 3 additions & 0 deletions docs/cargo-llvm-cov-run.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ OPTIONS:
--branch
Enable branch coverage. (unstable)

--mcdc
Enable mcdc coverage. (unstable)

--ignore-run-fail
Run all tests regardless of failure and generate report

Expand Down
3 changes: 3 additions & 0 deletions docs/cargo-llvm-cov-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ OPTIONS:
--branch
Enable branch coverage. (unstable)

--mcdc
Enable mcdc coverage. (unstable)

--doctests
Including doc tests (unstable)

Expand Down
3 changes: 3 additions & 0 deletions docs/cargo-llvm-cov.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ OPTIONS:
--branch
Enable branch coverage. (unstable)

--mcdc
Enable mcdc coverage. (unstable)

--doctests
Including doc tests (unstable)

Expand Down
7 changes: 7 additions & 0 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ pub(crate) struct Workspace {
}

impl Workspace {
#[allow(clippy::fn_params_excessive_bools)]
pub(crate) fn new(
options: &ManifestOptions,
target: Option<&str>,
doctests: bool,
branch: bool,
mcdc: bool,
show_env: bool,
) -> Result<Self> {
// Metadata and config
Expand All @@ -64,6 +66,11 @@ impl Workspace {
if branch && !rustc_version.nightly {
bail!("--branch flag requires nightly toolchain; consider using `cargo +nightly llvm-cov`")
}
if mcdc && !rustc_version.nightly {
bail!(
"--mcdc flag requires nightly toolchain; consider using `cargo +nightly llvm-cov`"
)
}
let stable_coverage =
rustc.clone().args(["-C", "help"]).read()?.contains("instrument-coverage");
if !stable_coverage && !rustc_version.nightly {
Expand Down
2 changes: 1 addition & 1 deletion src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
};

pub(crate) fn run(args: &mut Args) -> Result<()> {
let ws = Workspace::new(&args.manifest, None, false, false, false)?;
let ws = Workspace::new(&args.manifest, None, false, false, false, false)?;
cli::merge_config_to_args(&ws, &mut None, &mut args.verbose, &mut args.color);
term::set_coloring(&mut args.color);

Expand Down
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ pub(crate) struct LlvmCovOptions {
pub(crate) skip_functions: bool,
/// Enable branch coverage. (unstable)
pub(crate) branch: bool,
/// Enable mcdc coverage. (unstable)
pub(crate) mcdc: bool,
}

impl LlvmCovOptions {
Expand Down Expand Up @@ -511,6 +513,7 @@ impl Args {
let mut dep_coverage = None;
let mut skip_functions = false;
let mut branch = false;
let mut mcdc = false;

// build options
let mut release = false;
Expand Down Expand Up @@ -670,6 +673,7 @@ impl Args {
Long("summary-only") => parse_flag!(summary_only),
Long("skip-functions") => parse_flag!(skip_functions),
Long("branch") => parse_flag!(branch),
Long("mcdc") => parse_flag!(mcdc),
Long("output-path") => parse_opt!(output_path),
Long("output-dir") => parse_opt!(output_dir),
Long("failure-mode") => parse_opt!(failure_mode),
Expand Down Expand Up @@ -1190,6 +1194,7 @@ impl Args {
dep_coverage,
skip_functions,
branch,
mcdc,
},
show_env: ShowEnvOptions { export_prefix },
doctests,
Expand Down
4 changes: 4 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl Context {
args.target.as_deref(),
args.doctests,
args.cov.branch,
args.cov.mcdc,
show_env,
)?;
cli::merge_config_to_args(&ws, &mut args.target, &mut args.verbose, &mut args.color);
Expand All @@ -74,6 +75,9 @@ impl Context {
if args.cov.branch {
warn!("--branch option is unstable");
}
if args.cov.mcdc {
warn!("--mcdc option is unstable");
}
if args.doc {
warn!("--doc option is unstable");
} else if args.doctests {
Expand Down
10 changes: 10 additions & 0 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ pub struct File {
/// This is None if report is summary-only.
#[serde(skip_serializing_if = "Option::is_none")]
branches: Option<Vec<serde_json::Value>>,
/// List of MC/DC records contained in the file
///
/// This is None if report is summary-only.
#[serde(skip_serializing_if = "Option::is_none")]
mcdc_records: Option<Vec<serde_json::Value>>,
/// List of expansion records
///
/// This is None if report is summary-only.
Expand Down Expand Up @@ -406,6 +411,8 @@ impl fmt::Debug for Segment {
#[cfg_attr(test, serde(deny_unknown_fields))]
struct Function {
branches: Vec<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
mcdc_records: Option<Vec<serde_json::Value>>,
count: u64,
/// List of filenames that the function relates to
filenames: Vec<String>,
Expand Down Expand Up @@ -500,6 +507,9 @@ impl RegionLocation {
struct Summary {
/// Object summarizing branch coverage
branches: CoverageCounts,
/// Object summarizing mcdc coverage
#[serde(skip_serializing_if = "Option::is_none")]
mcdc: Option<CoverageCounts>,
/// Object summarizing function coverage
functions: CoverageCounts,
instantiations: CoverageCounts,
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ fn set_env(cx: &Context, env: &mut dyn EnvTarget, IsNextest(is_nextest): IsNexte
flags.push("codegen-units=1");
}
}
if cx.args.cov.branch {
if cx.args.cov.mcdc {
// Tracking issue: https://github.com/rust-lang/rust/issues/124144
flags.push("-Z");
flags.push("coverage-options=mcdc");
} else if cx.args.cov.branch {
// Tracking issue: https://github.com/rust-lang/rust/issues/79649
flags.push("-Z");
flags.push("coverage-options=branch");
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/coverage-reports/bin_crate/bin_crate.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 1,
"covered": 1,
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/coverage-reports/bin_crate/run.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 1,
"covered": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 2,
"covered": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 2,
"covered": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 1,
"covered": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 2,
"covered": 2,
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/coverage-reports/merge/clean_ws.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 2,
"covered": 2,
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/coverage-reports/merge/merge.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 2,
"covered": 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 4,
"covered": 4,
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/coverage-reports/no_coverage/no_coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 2,
"covered": 2,
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/coverage-reports/no_test/link_dead_code.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 1,
"covered": 1,
Expand Down Expand Up @@ -43,6 +49,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 1,
"covered": 0,
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/coverage-reports/no_test/no_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 1,
"covered": 1,
Expand Down Expand Up @@ -43,6 +49,12 @@
"notcovered": 0,
"percent": 0.0
},
"mcdc": {
"count": 0,
"covered": 0,
"notcovered": 0,
"percent": 0.0
},
"functions": {
"count": 1,
"covered": 0,
Expand Down
Loading

0 comments on commit e9370d2

Please sign in to comment.