Skip to content

Commit 23fb83c

Browse files
author
zhuyunxing
committed
coverage. Add mcdc coverage option
1 parent ccfcd95 commit 23fb83c

File tree

8 files changed

+41
-15
lines changed

8 files changed

+41
-15
lines changed

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ fn test_unstable_options_tracking_hash() {
759759
);
760760
tracked!(codegen_backend, Some("abc".to_string()));
761761
tracked!(collapse_macro_debuginfo, CollapseMacroDebuginfo::Yes);
762-
tracked!(coverage_options, CoverageOptions { branch: true });
762+
tracked!(coverage_options, CoverageOptions { branch: true, mcdc: true });
763763
tracked!(crate_attr, vec!["abc".to_string()]);
764764
tracked!(cross_crate_inline_threshold, InliningThreshold::Always);
765765
tracked!(debug_info_for_profiling, true);

compiler/rustc_session/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ pub enum InstrumentCoverage {
148148
pub struct CoverageOptions {
149149
/// Add branch coverage instrumentation.
150150
pub branch: bool,
151+
/// Add mcdc coverage instrumentation.
152+
pub mcdc: bool,
151153
}
152154

153155
/// Settings for `-Z instrument-xray` flag.

compiler/rustc_session/src/options.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ mod desc {
396396
pub const parse_optimization_fuel: &str = "crate=integer";
397397
pub const parse_dump_mono_stats: &str = "`markdown` (default) or `json`";
398398
pub const parse_instrument_coverage: &str = parse_bool;
399-
pub const parse_coverage_options: &str = "`branch` or `no-branch`";
399+
pub const parse_coverage_options: &str = "either `no-branch`, `branch` or `mcdc`";
400400
pub const parse_instrument_xray: &str = "either a boolean (`yes`, `no`, `on`, `off`, etc), or a comma separated list of settings: `always` or `never` (mutually exclusive), `ignore-loops`, `instruction-threshold=N`, `skip-entry`, `skip-exit`";
401401
pub const parse_unpretty: &str = "`string` or `string=string`";
402402
pub const parse_treat_err_as_bug: &str = "either no value or a non-negative number";
@@ -945,16 +945,30 @@ mod parse {
945945
pub(crate) fn parse_coverage_options(slot: &mut CoverageOptions, v: Option<&str>) -> bool {
946946
let Some(v) = v else { return true };
947947

948-
for option in v.split(',') {
949-
let (option, enabled) = match option.strip_prefix("no-") {
950-
Some(without_no) => (without_no, false),
951-
None => (option, true),
952-
};
953-
let slot = match option {
954-
"branch" => &mut slot.branch,
948+
let set_branch_option = |slot: &mut CoverageOptions, option: &str| {
949+
match option {
950+
"no-branch" => slot.branch = false,
951+
"branch" => slot.branch = true,
952+
"mcdc" => {
953+
slot.mcdc = true;
954+
slot.branch = true
955+
}
955956
_ => return false,
956-
};
957-
*slot = enabled;
957+
}
958+
true
959+
};
960+
961+
// Once an option is parsed we removed it from the array so that conflicting options such as "branch,no-branch" could be detected.
962+
let mut parsers_set: [Option<&dyn Fn(&mut CoverageOptions, &str) -> bool>; 1] =
963+
[Some(&set_branch_option)];
964+
965+
for option in v.split(',') {
966+
if !parsers_set
967+
.iter_mut()
968+
.any(|p| p.is_some_and(|parser| parser(slot, option)).then(|| p.take()).is_some())
969+
{
970+
return false;
971+
}
958972
}
959973

960974
true

compiler/rustc_session/src/session.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@ impl Session {
352352
self.instrument_coverage() && self.opts.unstable_opts.coverage_options.branch
353353
}
354354

355+
pub fn instrument_coverage_mcdc(&self) -> bool {
356+
self.instrument_coverage() && self.opts.unstable_opts.coverage_options.mcdc
357+
}
358+
355359
pub fn is_sanitizer_cfi_enabled(&self) -> bool {
356360
self.opts.unstable_opts.sanitizer.contains(SanitizerSet::CFI)
357361
}

src/doc/rustc/src/instrument-coverage.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ $ llvm-cov report \
351351
This unstable option provides finer control over some aspects of coverage
352352
instrumentation. Pass one or more of the following values, separated by commas.
353353

354-
- `branch` or `no-branch`
355-
- Enables or disables branch coverage instrumentation.
354+
- Either `no-branch`, `branch` or `mcdc`
355+
- `branch` enables branch coverage instrumentation and `mcdc` further enables modified condition/decision coverage instrumentation. `no-branch` disables branch coverage instrumentation, which is same as do not pass `branch` or `mcdc`.
356356

357357
## Other references
358358

src/doc/unstable-book/src/compiler-flags/coverage-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ This option controls details of the coverage instrumentation performed by
55

66
Multiple options can be passed, separated by commas. Valid options are:
77

8-
- `branch` or `no-branch`: Enables or disables branch coverage instrumentation.
8+
- `no-branch`, `branch` or `mcdc`: `branch` enables branch coverage instrumentation and `mcdc` further enables modified condition/decision coverage instrumentation. `no-branch` disables branch coverage instrumentation, which is same as do not pass `branch` or `mcdc`
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: incorrect value `bad` for unstable option `coverage-options` - `branch` or `no-branch` was expected
1+
error: incorrect value `bad` for unstable option `coverage-options` - either `no-branch`, `branch` or `mcdc` was expected
22

tests/ui/instrument-coverage/coverage-options.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
//@ [no-branch] check-pass
99
//@ [no-branch] compile-flags: -Zcoverage-options=no-branch
1010

11+
//@ [mcdc] check-pass
12+
//@ [mcdc] compile-flags: -Zcoverage-options=mcdc
13+
1114
//@ [bad] check-fail
1215
//@ [bad] compile-flags: -Zcoverage-options=bad
1316

17+
//@ [conflict] check-fail
18+
//@ [conflict] compile-flags: -Zcoverage-options=no-branch,mcdc
19+
1420
fn main() {}

0 commit comments

Comments
 (0)