Skip to content

Commit 68f8638

Browse files
author
zhuyunxing
committed
coverage. Add coverage-options=mcdc as gate for MC/DC instrument
1 parent e3181b0 commit 68f8638

File tree

10 files changed

+40
-15
lines changed

10 files changed

+40
-15
lines changed

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ fn test_unstable_options_tracking_hash() {
758758
);
759759
tracked!(codegen_backend, Some("abc".to_string()));
760760
tracked!(collapse_macro_debuginfo, CollapseMacroDebuginfo::Yes);
761-
tracked!(coverage_options, CoverageOptions { branch: true });
761+
tracked!(coverage_options, CoverageOptions { branch: true, mcdc: true });
762762
tracked!(crate_attr, vec!["abc".to_string()]);
763763
tracked!(cross_crate_inline_threshold, InliningThreshold::Always);
764764
tracked!(debug_info_for_profiling, true);

compiler/rustc_mir_build/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ mir_build_deref_raw_pointer_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
9797
.note = raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
9898
.label = dereference of raw pointer
9999
100+
mir_build_exceeds_mcdc_condition_num_limit = Conditions number of the decision ({$conditions_num}) exceeds limit ({$max_conditions_num}). MCDC analysis will not count this expression.
101+
100102
mir_build_extern_static_requires_unsafe =
101103
use of extern static is unsafe and requires unsafe block
102104
.note = extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior

compiler/rustc_mir_build/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,15 @@ pub struct NontrivialStructuralMatch<'tcx> {
818818
pub non_sm_ty: Ty<'tcx>,
819819
}
820820

821+
#[derive(Diagnostic)]
822+
#[diag(mir_build_exceeds_mcdc_condition_num_limit)]
823+
pub(crate) struct MCDCExceedsConditionNumLimit {
824+
#[primary_span]
825+
pub span: Span,
826+
pub conditions_num: usize,
827+
pub max_conditions_num: usize,
828+
}
829+
821830
#[derive(Diagnostic)]
822831
#[diag(mir_build_pattern_not_covered, code = E0005)]
823832
pub(crate) struct PatternNotCovered<'s, 'tcx> {

compiler/rustc_session/src/config.rs

+2
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

+12-10
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";
@@ -946,17 +946,19 @@ mod parse {
946946
let Some(v) = v else { return true };
947947

948948
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,
949+
match option {
950+
"no-branch" => {
951+
slot.branch = false;
952+
slot.mcdc = false;
953+
}
954+
"branch" => slot.branch = true,
955+
"mcdc" => {
956+
slot.branch = true;
957+
slot.mcdc = true;
958+
}
955959
_ => return false,
956-
};
957-
*slot = enabled;
960+
}
958961
}
959-
960962
true
961963
}
962964

compiler/rustc_session/src/session.rs

+4
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

+2-2
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

+1-1
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 as well as mcdc instrumentation, which is same as do not pass `branch` or `mcdc`.
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

+6
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)