Skip to content

Commit f9dca46

Browse files
committed
Auto merge of #124507 - Zalathar:coverage-level, r=compiler-errors
coverage: Replace boolean options with a `CoverageLevel` enum After #123409, and some discussion at #79649 (comment) and #124120, it became clear to me that we should have a unified concept of “coverage level”, instead of having several separate boolean flags that aren't actually independent. This PR therefore introduces a `CoverageLevel` enum, to replace the existing boolean flags for `branch` and `mcdc`. The `no-branch` value (for `-Zcoverage-options`) has been renamed to `block`, instructing the compiler to only instrument for block coverage, with no branch coverage or MD/DC instrumentation. `@rustbot` label +A-code-coverage cc `@ZhuUx` `@Lambdaris` `@RenjiSann`
2 parents 74a8df6 + f926337 commit f9dca46

File tree

8 files changed

+53
-39
lines changed

8 files changed

+53
-39
lines changed

compiler/rustc_interface/src/tests.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use rustc_data_structures::profiling::TimePassesFormat;
44
use rustc_errors::{emitter::HumanReadableErrorType, registry, ColorConfig};
55
use rustc_session::config::{
66
build_configuration, build_session_options, rustc_optgroups, BranchProtection, CFGuard, Cfg,
7-
CollapseMacroDebuginfo, CoverageOptions, DebugInfo, DumpMonoStatsFormat, ErrorOutputType,
8-
ExternEntry, ExternLocation, Externs, FunctionReturn, InliningThreshold, Input,
9-
InstrumentCoverage, InstrumentXRay, LinkSelfContained, LinkerPluginLto, LocationDetail, LtoCli,
10-
NextSolverConfig, OomStrategy, Options, OutFileName, OutputType, OutputTypes, PAuthKey, PacRet,
11-
Passes, Polonius, ProcMacroExecutionStrategy, Strip, SwitchWithOptPath, SymbolManglingVersion,
12-
WasiExecModel,
7+
CollapseMacroDebuginfo, CoverageLevel, CoverageOptions, DebugInfo, DumpMonoStatsFormat,
8+
ErrorOutputType, ExternEntry, ExternLocation, Externs, FunctionReturn, InliningThreshold,
9+
Input, InstrumentCoverage, InstrumentXRay, LinkSelfContained, LinkerPluginLto, LocationDetail,
10+
LtoCli, NextSolverConfig, OomStrategy, Options, OutFileName, OutputType, OutputTypes, PAuthKey,
11+
PacRet, Passes, Polonius, ProcMacroExecutionStrategy, Strip, SwitchWithOptPath,
12+
SymbolManglingVersion, WasiExecModel,
1313
};
1414
use rustc_session::lint::Level;
1515
use rustc_session::search_paths::SearchPath;
@@ -761,7 +761,7 @@ fn test_unstable_options_tracking_hash() {
761761
})
762762
);
763763
tracked!(codegen_backend, Some("abc".to_string()));
764-
tracked!(coverage_options, CoverageOptions { branch: true, mcdc: true });
764+
tracked!(coverage_options, CoverageOptions { level: CoverageLevel::Mcdc });
765765
tracked!(crate_attr, vec!["abc".to_string()]);
766766
tracked!(cross_crate_inline_threshold, InliningThreshold::Always);
767767
tracked!(debug_info_for_profiling, true);

compiler/rustc_session/src/config.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,26 @@ pub enum InstrumentCoverage {
146146
/// Individual flag values controlled by `-Z coverage-options`.
147147
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
148148
pub struct CoverageOptions {
149-
/// Add branch coverage instrumentation.
150-
pub branch: bool,
151-
/// Add mcdc coverage instrumentation.
152-
pub mcdc: bool,
149+
pub level: CoverageLevel,
150+
// Other boolean or enum-valued options might be added here.
151+
}
152+
153+
/// Controls whether branch coverage or MC/DC coverage is enabled.
154+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
155+
pub enum CoverageLevel {
156+
/// Instrument for coverage at the MIR block level.
157+
Block,
158+
/// Also instrument branch points (includes block coverage).
159+
Branch,
160+
/// Instrument for MC/DC. Mostly a superset of branch coverage, but might
161+
/// differ in some corner cases.
162+
Mcdc,
163+
}
164+
165+
impl Default for CoverageLevel {
166+
fn default() -> Self {
167+
Self::Block
168+
}
153169
}
154170

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

compiler/rustc_session/src/options.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ mod desc {
394394
pub const parse_optimization_fuel: &str = "crate=integer";
395395
pub const parse_dump_mono_stats: &str = "`markdown` (default) or `json`";
396396
pub const parse_instrument_coverage: &str = parse_bool;
397-
pub const parse_coverage_options: &str = "either `no-branch`, `branch` or `mcdc`";
397+
pub const parse_coverage_options: &str = "`block` | `branch` | `mcdc`";
398398
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`";
399399
pub const parse_unpretty: &str = "`string` or `string=string`";
400400
pub const parse_treat_err_as_bug: &str = "either no value or a non-negative number";
@@ -946,15 +946,9 @@ mod parse {
946946

947947
for option in v.split(',') {
948948
match option {
949-
"no-branch" => {
950-
slot.branch = false;
951-
slot.mcdc = false;
952-
}
953-
"branch" => slot.branch = true,
954-
"mcdc" => {
955-
slot.branch = true;
956-
slot.mcdc = true;
957-
}
949+
"block" => slot.level = CoverageLevel::Block,
950+
"branch" => slot.level = CoverageLevel::Branch,
951+
"mcdc" => slot.level = CoverageLevel::Mcdc,
958952
_ => return false,
959953
}
960954
}

compiler/rustc_session/src/session.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::code_stats::CodeStats;
22
pub use crate::code_stats::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
33
use crate::config::{
4-
self, CrateType, FunctionReturn, InstrumentCoverage, OptLevel, OutFileName, OutputType,
5-
RemapPathScopeComponents, SwitchWithOptPath,
4+
self, CoverageLevel, CrateType, FunctionReturn, InstrumentCoverage, OptLevel, OutFileName,
5+
OutputType, RemapPathScopeComponents, SwitchWithOptPath,
66
};
77
use crate::config::{ErrorOutputType, Input};
88
use crate::errors;
@@ -349,11 +349,13 @@ impl Session {
349349
}
350350

351351
pub fn instrument_coverage_branch(&self) -> bool {
352-
self.instrument_coverage() && self.opts.unstable_opts.coverage_options.branch
352+
self.instrument_coverage()
353+
&& self.opts.unstable_opts.coverage_options.level >= CoverageLevel::Branch
353354
}
354355

355356
pub fn instrument_coverage_mcdc(&self) -> bool {
356-
self.instrument_coverage() && self.opts.unstable_opts.coverage_options.mcdc
357+
self.instrument_coverage()
358+
&& self.opts.unstable_opts.coverage_options.level >= CoverageLevel::Mcdc
357359
}
358360

359361
pub fn is_sanitizer_cfi_enabled(&self) -> bool {

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,7 @@ $ llvm-cov report \
348348

349349
## `-Z coverage-options=<options>`
350350

351-
This unstable option provides finer control over some aspects of coverage
352-
instrumentation. Pass one or more of the following values, separated by commas.
353-
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`.
351+
[This unstable option is described in the Unstable Book.](../unstable-book/compiler-flags/coverage-options.html)
356352

357353
## Other references
358354

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,13 @@ 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-
- `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`.
8+
- `block`, `branch`, `mcdc`:
9+
Sets the level of coverage instrumentation.
10+
Setting the level will override any previously-specified level.
11+
- `block` (default):
12+
Blocks in the control-flow graph will be instrumented for coverage.
13+
- `branch`:
14+
In addition to block coverage, also enables branch coverage instrumentation.
15+
- `mcdc`:
16+
In addition to block and branch coverage, also enables MC/DC instrumentation.
17+
(Branch coverage instrumentation may differ in some cases.)
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: incorrect value `bad` for unstable option `coverage-options` - either `no-branch`, `branch` or `mcdc` was expected
1+
error: incorrect value `bad` for unstable option `coverage-options` - `block` | `branch` | `mcdc` was expected
22

Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
//@ needs-profiler-support
2-
//@ revisions: branch no-branch bad
2+
//@ revisions: block branch bad
33
//@ compile-flags -Cinstrument-coverage
44

5+
//@ [block] check-pass
6+
//@ [block] compile-flags: -Zcoverage-options=block
7+
58
//@ [branch] check-pass
69
//@ [branch] compile-flags: -Zcoverage-options=branch
710

8-
//@ [no-branch] check-pass
9-
//@ [no-branch] compile-flags: -Zcoverage-options=no-branch
10-
1111
//@ [mcdc] check-pass
1212
//@ [mcdc] compile-flags: -Zcoverage-options=mcdc
1313

1414
//@ [bad] check-fail
1515
//@ [bad] compile-flags: -Zcoverage-options=bad
1616

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

0 commit comments

Comments
 (0)