Skip to content

Commit

Permalink
coverage: Flatten BcbMappingKind into mappings::CodeMapping
Browse files Browse the repository at this point in the history
Now that branch and MC/DC mappings have been split out into separate types and
vectors, this enum is no longer needed, since it only represents ordinary
"code" regions.

(We can revisit this decision if we ever add support for other region kinds,
such as skipped regions or expansion regions. But at that point, we might just
add new structs/vectors for those kinds as well.)
  • Loading branch information
Zalathar committed May 2, 2024
1 parent 39de2ea commit 7898c18
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 32 deletions.
34 changes: 12 additions & 22 deletions compiler/rustc_mir_transform/src/coverage/mappings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,14 @@ use crate::coverage::spans::{
};
use crate::coverage::ExtractedHirInfo;

#[derive(Clone, Copy, Debug)]
pub(super) enum BcbMappingKind {
/// Associates an ordinary executable code span with its corresponding BCB.
Code(BasicCoverageBlock),
//
// Branch and MC/DC mappings are more complex, so they are represented
// separately.
}

/// Associates an ordinary executable code span with its corresponding BCB.
#[derive(Debug)]
pub(super) struct BcbMapping {
pub(super) kind: BcbMappingKind,
pub(super) struct CodeMapping {
pub(super) span: Span,
pub(super) bcb: BasicCoverageBlock,
}

/// This is separate from [`BcbMappingKind`] to help prepare for larger changes
/// This is separate from [`MCDCBranch`] to help prepare for larger changes
/// that will be needed for improved branch coverage in the future.
/// (See <https://github.com/rust-lang/rust/pull/124217>.)
#[derive(Debug)]
Expand Down Expand Up @@ -62,7 +54,7 @@ pub(super) struct MCDCDecision {

pub(super) struct CoverageSpans {
bcb_has_mappings: BitSet<BasicCoverageBlock>,
pub(super) mappings: Vec<BcbMapping>,
pub(super) code_mappings: Vec<CodeMapping>,
pub(super) branch_pairs: Vec<BcbBranchPair>,
test_vector_bitmap_bytes: u32,
pub(super) mcdc_branches: Vec<MCDCBranch>,
Expand All @@ -88,7 +80,7 @@ pub(super) fn generate_coverage_spans(
hir_info: &ExtractedHirInfo,
basic_coverage_blocks: &CoverageGraph,
) -> Option<CoverageSpans> {
let mut mappings = vec![];
let mut code_mappings = vec![];
let mut branch_pairs = vec![];
let mut mcdc_branches = vec![];
let mut mcdc_decisions = vec![];
Expand All @@ -99,10 +91,10 @@ pub(super) fn generate_coverage_spans(
// outer function will be unhelpful, so just keep the signature span
// and ignore all of the spans in the MIR body.
if let Some(span) = hir_info.fn_sig_span_extended {
mappings.push(BcbMapping { kind: BcbMappingKind::Code(START_BCB), span });
code_mappings.push(CodeMapping { span, bcb: START_BCB });
}
} else {
extract_refined_covspans(mir_body, hir_info, basic_coverage_blocks, &mut mappings);
extract_refined_covspans(mir_body, hir_info, basic_coverage_blocks, &mut code_mappings);

branch_pairs.extend(extract_branch_pairs(mir_body, hir_info, basic_coverage_blocks));

Expand All @@ -115,7 +107,7 @@ pub(super) fn generate_coverage_spans(
);
}

if mappings.is_empty()
if code_mappings.is_empty()
&& branch_pairs.is_empty()
&& mcdc_branches.is_empty()
&& mcdc_decisions.is_empty()
Expand All @@ -129,10 +121,8 @@ pub(super) fn generate_coverage_spans(
bcb_has_mappings.insert(bcb);
};

for &BcbMapping { kind, span: _ } in &mappings {
match kind {
BcbMappingKind::Code(bcb) => insert(bcb),
}
for &CodeMapping { span: _, bcb } in &code_mappings {
insert(bcb);
}
for &BcbBranchPair { true_bcb, false_bcb, .. } in &branch_pairs {
insert(true_bcb);
Expand All @@ -154,7 +144,7 @@ pub(super) fn generate_coverage_spans(

Some(CoverageSpans {
bcb_has_mappings,
mappings,
code_mappings,
branch_pairs,
test_vector_bitmap_bytes,
mcdc_branches,
Expand Down
10 changes: 4 additions & 6 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod tests;

use self::counters::{CounterIncrementSite, CoverageCounters};
use self::graph::{BasicCoverageBlock, CoverageGraph};
use self::mappings::{BcbBranchPair, BcbMapping, BcbMappingKind, CoverageSpans};
use self::mappings::{BcbBranchPair, CoverageSpans};

use crate::MirPass;

Expand Down Expand Up @@ -150,12 +150,10 @@ fn create_mappings<'tcx>(

let mut mappings = Vec::new();

mappings.extend(coverage_spans.mappings.iter().filter_map(
|&BcbMapping { kind: bcb_mapping_kind, span }| {
let kind = match bcb_mapping_kind {
BcbMappingKind::Code(bcb) => MappingKind::Code(term_for_bcb(bcb)),
};
mappings.extend(coverage_spans.code_mappings.iter().filter_map(
|&mappings::CodeMapping { span, bcb }| {
let code_region = region_for_span(span)?;
let kind = MappingKind::Code(term_for_bcb(bcb));
Some(Mapping { kind, code_region })
},
));
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_mir_transform/src/coverage/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc_middle::mir;
use rustc_span::{BytePos, Span};

use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};
use crate::coverage::mappings::{BcbMapping, BcbMappingKind};
use crate::coverage::mappings;
use crate::coverage::spans::from_mir::SpanFromMir;
use crate::coverage::ExtractedHirInfo;

Expand All @@ -17,14 +17,14 @@ pub(super) fn extract_refined_covspans(
mir_body: &mir::Body<'_>,
hir_info: &ExtractedHirInfo,
basic_coverage_blocks: &CoverageGraph,
mappings: &mut impl Extend<BcbMapping>,
code_mappings: &mut impl Extend<mappings::CodeMapping>,
) {
let sorted_spans =
from_mir::mir_to_initial_sorted_coverage_spans(mir_body, hir_info, basic_coverage_blocks);
let coverage_spans = SpansRefiner::refine_sorted_spans(sorted_spans);
mappings.extend(coverage_spans.into_iter().map(|RefinedCovspan { bcb, span, .. }| {
code_mappings.extend(coverage_spans.into_iter().map(|RefinedCovspan { bcb, span, .. }| {
// Each span produced by the generator represents an ordinary code region.
BcbMapping { kind: BcbMappingKind::Code(bcb), span }
mappings::CodeMapping { span, bcb }
}));
}

Expand Down

0 comments on commit 7898c18

Please sign in to comment.