coverage: Rename CodeRegion to SourceRegion

LLVM uses the word "code" to refer to a particular kind of coverage mapping.
This unrelated usage of the word is confusing, and makes it harder to introduce
types whose names correspond to the LLVM classification of coverage kinds.
This commit is contained in:
Zalathar 2024-08-28 18:46:49 +10:00
parent 5e162a8f48
commit 46e1b5b6dd
5 changed files with 35 additions and 34 deletions

View File

@ -1,5 +1,5 @@
use rustc_middle::mir::coverage::{ use rustc_middle::mir::coverage::{
CodeRegion, ConditionInfo, CounterId, CovTerm, DecisionInfo, ExpressionId, MappingKind, ConditionInfo, CounterId, CovTerm, DecisionInfo, ExpressionId, MappingKind, SourceRegion,
}; };
/// Must match the layout of `LLVMRustCounterKind`. /// Must match the layout of `LLVMRustCounterKind`.
@ -236,9 +236,10 @@ impl CounterMappingRegion {
pub(crate) fn from_mapping( pub(crate) fn from_mapping(
mapping_kind: &MappingKind, mapping_kind: &MappingKind,
local_file_id: u32, local_file_id: u32,
code_region: &CodeRegion, source_region: &SourceRegion,
) -> Self { ) -> Self {
let &CodeRegion { file_name: _, start_line, start_col, end_line, end_col } = code_region; let &SourceRegion { file_name: _, start_line, start_col, end_line, end_col } =
source_region;
match *mapping_kind { match *mapping_kind {
MappingKind::Code(term) => Self::code_region( MappingKind::Code(term) => Self::code_region(
Counter::from_term(term), Counter::from_term(term),

View File

@ -2,8 +2,8 @@ use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::fx::FxIndexSet;
use rustc_index::bit_set::BitSet; use rustc_index::bit_set::BitSet;
use rustc_middle::mir::coverage::{ use rustc_middle::mir::coverage::{
CodeRegion, CounterId, CovTerm, Expression, ExpressionId, FunctionCoverageInfo, Mapping, CounterId, CovTerm, Expression, ExpressionId, FunctionCoverageInfo, Mapping, MappingKind, Op,
MappingKind, Op, SourceRegion,
}; };
use rustc_middle::ty::Instance; use rustc_middle::ty::Instance;
use rustc_span::Symbol; use rustc_span::Symbol;
@ -201,7 +201,7 @@ impl<'tcx> FunctionCoverage<'tcx> {
/// Returns an iterator over all filenames used by this function's mappings. /// Returns an iterator over all filenames used by this function's mappings.
pub(crate) fn all_file_names(&self) -> impl Iterator<Item = Symbol> + Captures<'_> { pub(crate) fn all_file_names(&self) -> impl Iterator<Item = Symbol> + Captures<'_> {
self.function_coverage_info.mappings.iter().map(|mapping| mapping.code_region.file_name) self.function_coverage_info.mappings.iter().map(|mapping| mapping.source_region.file_name)
} }
/// Convert this function's coverage expression data into a form that can be /// Convert this function's coverage expression data into a form that can be
@ -230,12 +230,12 @@ impl<'tcx> FunctionCoverage<'tcx> {
/// that will be used by `mapgen` when preparing for FFI. /// that will be used by `mapgen` when preparing for FFI.
pub(crate) fn counter_regions( pub(crate) fn counter_regions(
&self, &self,
) -> impl Iterator<Item = (MappingKind, &CodeRegion)> + ExactSizeIterator { ) -> impl Iterator<Item = (MappingKind, &SourceRegion)> + ExactSizeIterator {
self.function_coverage_info.mappings.iter().map(move |mapping| { self.function_coverage_info.mappings.iter().map(move |mapping| {
let Mapping { kind, code_region } = mapping; let Mapping { kind, source_region } = mapping;
let kind = let kind =
kind.map_terms(|term| if self.is_zero_term(term) { CovTerm::Zero } else { term }); kind.map_terms(|term| if self.is_zero_term(term) { CovTerm::Zero } else { term });
(kind, code_region) (kind, source_region)
}) })
} }

View File

@ -163,7 +163,7 @@ impl Debug for CoverageKind {
#[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, Eq, PartialOrd, Ord)] #[derive(Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq, Eq, PartialOrd, Ord)]
#[derive(TypeFoldable, TypeVisitable)] #[derive(TypeFoldable, TypeVisitable)]
pub struct CodeRegion { pub struct SourceRegion {
pub file_name: Symbol, pub file_name: Symbol,
pub start_line: u32, pub start_line: u32,
pub start_col: u32, pub start_col: u32,
@ -171,7 +171,7 @@ pub struct CodeRegion {
pub end_col: u32, pub end_col: u32,
} }
impl Debug for CodeRegion { impl Debug for SourceRegion {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
write!( write!(
fmt, fmt,
@ -242,7 +242,7 @@ impl MappingKind {
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)] #[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
pub struct Mapping { pub struct Mapping {
pub kind: MappingKind, pub kind: MappingKind,
pub code_region: CodeRegion, pub source_region: SourceRegion,
} }
/// Stores per-function coverage information attached to a `mir::Body`, /// Stores per-function coverage information attached to a `mir::Body`,

View File

@ -547,8 +547,8 @@ fn write_function_coverage_info(
for (id, expression) in expressions.iter_enumerated() { for (id, expression) in expressions.iter_enumerated() {
writeln!(w, "{INDENT}coverage {id:?} => {expression:?};")?; writeln!(w, "{INDENT}coverage {id:?} => {expression:?};")?;
} }
for coverage::Mapping { kind, code_region } in mappings { for coverage::Mapping { kind, source_region } in mappings {
writeln!(w, "{INDENT}coverage {kind:?} => {code_region:?};")?; writeln!(w, "{INDENT}coverage {kind:?} => {source_region:?};")?;
} }
writeln!(w)?; writeln!(w)?;

View File

@ -13,7 +13,7 @@ use rustc_hir::intravisit::{walk_expr, Visitor};
use rustc_middle::hir::map::Map; use rustc_middle::hir::map::Map;
use rustc_middle::hir::nested_filter; use rustc_middle::hir::nested_filter;
use rustc_middle::mir::coverage::{ use rustc_middle::mir::coverage::{
CodeRegion, CoverageKind, DecisionInfo, FunctionCoverageInfo, Mapping, MappingKind, CoverageKind, DecisionInfo, FunctionCoverageInfo, Mapping, MappingKind, SourceRegion,
}; };
use rustc_middle::mir::{ use rustc_middle::mir::{
self, BasicBlock, BasicBlockData, SourceInfo, Statement, StatementKind, Terminator, self, BasicBlock, BasicBlockData, SourceInfo, Statement, StatementKind, Terminator,
@ -159,7 +159,7 @@ fn create_mappings<'tcx>(
.expect("all BCBs with spans were given counters") .expect("all BCBs with spans were given counters")
.as_term() .as_term()
}; };
let region_for_span = |span: Span| make_code_region(source_map, file_name, span, body_span); let region_for_span = |span: Span| make_source_region(source_map, file_name, span, body_span);
// Fully destructure the mappings struct to make sure we don't miss any kinds. // Fully destructure the mappings struct to make sure we don't miss any kinds.
let ExtractedMappings { let ExtractedMappings {
@ -175,9 +175,9 @@ fn create_mappings<'tcx>(
mappings.extend(code_mappings.iter().filter_map( mappings.extend(code_mappings.iter().filter_map(
// Ordinary code mappings are the simplest kind. // Ordinary code mappings are the simplest kind.
|&mappings::CodeMapping { span, bcb }| { |&mappings::CodeMapping { span, bcb }| {
let code_region = region_for_span(span)?; let source_region = region_for_span(span)?;
let kind = MappingKind::Code(term_for_bcb(bcb)); let kind = MappingKind::Code(term_for_bcb(bcb));
Some(Mapping { kind, code_region }) Some(Mapping { kind, source_region })
}, },
)); ));
@ -186,29 +186,29 @@ fn create_mappings<'tcx>(
let true_term = term_for_bcb(true_bcb); let true_term = term_for_bcb(true_bcb);
let false_term = term_for_bcb(false_bcb); let false_term = term_for_bcb(false_bcb);
let kind = MappingKind::Branch { true_term, false_term }; let kind = MappingKind::Branch { true_term, false_term };
let code_region = region_for_span(span)?; let source_region = region_for_span(span)?;
Some(Mapping { kind, code_region }) Some(Mapping { kind, source_region })
}, },
)); ));
mappings.extend(mcdc_branches.iter().filter_map( mappings.extend(mcdc_branches.iter().filter_map(
|&mappings::MCDCBranch { span, true_bcb, false_bcb, condition_info, decision_depth: _ }| { |&mappings::MCDCBranch { span, true_bcb, false_bcb, condition_info, decision_depth: _ }| {
let code_region = region_for_span(span)?; let source_region = region_for_span(span)?;
let true_term = term_for_bcb(true_bcb); let true_term = term_for_bcb(true_bcb);
let false_term = term_for_bcb(false_bcb); let false_term = term_for_bcb(false_bcb);
let kind = match condition_info { let kind = match condition_info {
Some(mcdc_params) => MappingKind::MCDCBranch { true_term, false_term, mcdc_params }, Some(mcdc_params) => MappingKind::MCDCBranch { true_term, false_term, mcdc_params },
None => MappingKind::Branch { true_term, false_term }, None => MappingKind::Branch { true_term, false_term },
}; };
Some(Mapping { kind, code_region }) Some(Mapping { kind, source_region })
}, },
)); ));
mappings.extend(mcdc_decisions.iter().filter_map( mappings.extend(mcdc_decisions.iter().filter_map(
|&mappings::MCDCDecision { span, bitmap_idx, num_conditions, .. }| { |&mappings::MCDCDecision { span, bitmap_idx, num_conditions, .. }| {
let code_region = region_for_span(span)?; let source_region = region_for_span(span)?;
let kind = MappingKind::MCDCDecision(DecisionInfo { bitmap_idx, num_conditions }); let kind = MappingKind::MCDCDecision(DecisionInfo { bitmap_idx, num_conditions });
Some(Mapping { kind, code_region }) Some(Mapping { kind, source_region })
}, },
)); ));
@ -363,12 +363,12 @@ fn inject_statement(mir_body: &mut mir::Body<'_>, counter_kind: CoverageKind, bb
/// or other expansions), and if it does happen then skipping a span or function is /// or other expansions), and if it does happen then skipping a span or function is
/// better than an ICE or `llvm-cov` failure that the user might have no way to avoid. /// better than an ICE or `llvm-cov` failure that the user might have no way to avoid.
#[instrument(level = "debug", skip(source_map))] #[instrument(level = "debug", skip(source_map))]
fn make_code_region( fn make_source_region(
source_map: &SourceMap, source_map: &SourceMap,
file_name: Symbol, file_name: Symbol,
span: Span, span: Span,
body_span: Span, body_span: Span,
) -> Option<CodeRegion> { ) -> Option<SourceRegion> {
let lo = span.lo(); let lo = span.lo();
let hi = span.hi(); let hi = span.hi();
@ -418,7 +418,7 @@ fn make_code_region(
start_line = source_map.doctest_offset_line(&file.name, start_line); start_line = source_map.doctest_offset_line(&file.name, start_line);
end_line = source_map.doctest_offset_line(&file.name, end_line); end_line = source_map.doctest_offset_line(&file.name, end_line);
check_code_region(CodeRegion { check_source_region(SourceRegion {
file_name, file_name,
start_line: start_line as u32, start_line: start_line as u32,
start_col: start_col as u32, start_col: start_col as u32,
@ -427,12 +427,12 @@ fn make_code_region(
}) })
} }
/// If `llvm-cov` sees a code region that is improperly ordered (end < start), /// If `llvm-cov` sees a source region that is improperly ordered (end < start),
/// it will immediately exit with a fatal error. To prevent that from happening, /// it will immediately exit with a fatal error. To prevent that from happening,
/// discard regions that are improperly ordered, or might be interpreted in a /// discard regions that are improperly ordered, or might be interpreted in a
/// way that makes them improperly ordered. /// way that makes them improperly ordered.
fn check_code_region(code_region: CodeRegion) -> Option<CodeRegion> { fn check_source_region(source_region: SourceRegion) -> Option<SourceRegion> {
let CodeRegion { file_name: _, start_line, start_col, end_line, end_col } = code_region; let SourceRegion { file_name: _, start_line, start_col, end_line, end_col } = source_region;
// Line/column coordinates are supposed to be 1-based. If we ever emit // Line/column coordinates are supposed to be 1-based. If we ever emit
// coordinates of 0, `llvm-cov` might misinterpret them. // coordinates of 0, `llvm-cov` might misinterpret them.
@ -445,17 +445,17 @@ fn check_code_region(code_region: CodeRegion) -> Option<CodeRegion> {
let is_ordered = (start_line, start_col) <= (end_line, end_col); let is_ordered = (start_line, start_col) <= (end_line, end_col);
if all_nonzero && end_col_has_high_bit_unset && is_ordered { if all_nonzero && end_col_has_high_bit_unset && is_ordered {
Some(code_region) Some(source_region)
} else { } else {
debug!( debug!(
?code_region, ?source_region,
?all_nonzero, ?all_nonzero,
?end_col_has_high_bit_unset, ?end_col_has_high_bit_unset,
?is_ordered, ?is_ordered,
"Skipping code region that would be misinterpreted or rejected by LLVM" "Skipping source region that would be misinterpreted or rejected by LLVM"
); );
// If this happens in a debug build, ICE to make it easier to notice. // If this happens in a debug build, ICE to make it easier to notice.
debug_assert!(false, "Improper code region: {code_region:?}"); debug_assert!(false, "Improper source region: {source_region:?}");
None None
} }
} }