@@ -2,6 +2,7 @@ use super::graph::{BasicCoverageBlock, BasicCoverageBlockData, CoverageGraph, ST
22
33use itertools:: Itertools ;
44use rustc_data_structures:: graph:: WithNumNodes ;
5+ use rustc_index:: bit_set:: BitSet ;
56use rustc_middle:: mir:: spanview:: source_range_no_file;
67use rustc_middle:: mir:: {
78 self , AggregateKind , BasicBlock , FakeReadCause , Rvalue , Statement , StatementKind , Terminator ,
@@ -13,6 +14,42 @@ use rustc_span::{BytePos, ExpnKind, MacroKind, Span, Symbol};
1314
1415use std:: cell:: OnceCell ;
1516
17+ pub ( super ) struct CoverageSpans {
18+ coverage_spans : Vec < CoverageSpan > ,
19+ bcbs_with_coverage_spans : BitSet < BasicCoverageBlock > ,
20+ }
21+
22+ impl CoverageSpans {
23+ pub ( super ) fn generate_coverage_spans (
24+ mir_body : & mir:: Body < ' _ > ,
25+ fn_sig_span : Span ,
26+ body_span : Span ,
27+ basic_coverage_blocks : & CoverageGraph ,
28+ ) -> Self {
29+ let coverage_spans = CoverageSpansGenerator :: generate_coverage_spans (
30+ mir_body,
31+ fn_sig_span,
32+ body_span,
33+ basic_coverage_blocks,
34+ ) ;
35+
36+ let mut bcbs_with_coverage_spans = BitSet :: new_empty ( basic_coverage_blocks. num_nodes ( ) ) ;
37+ for coverage_span in & coverage_spans {
38+ bcbs_with_coverage_spans. insert ( coverage_span. bcb ) ;
39+ }
40+
41+ Self { coverage_spans, bcbs_with_coverage_spans }
42+ }
43+
44+ pub ( super ) fn bcb_has_coverage_spans ( & self , bcb : BasicCoverageBlock ) -> bool {
45+ self . bcbs_with_coverage_spans . contains ( bcb)
46+ }
47+
48+ pub ( super ) fn iter ( & self ) -> impl Iterator < Item = & CoverageSpan > {
49+ self . coverage_spans . iter ( )
50+ }
51+ }
52+
1653#[ derive( Debug , Copy , Clone ) ]
1754pub ( super ) enum CoverageStatement {
1855 Statement ( BasicBlock , Span , usize ) ,
@@ -211,7 +248,7 @@ impl CoverageSpan {
211248/// * Merge spans that represent continuous (both in source code and control flow), non-branching
212249/// execution
213250/// * Carve out (leave uncovered) any span that will be counted by another MIR (notably, closures)
214- pub struct CoverageSpans < ' a , ' tcx > {
251+ struct CoverageSpansGenerator < ' a , ' tcx > {
215252 /// The MIR, used to look up `BasicBlockData`.
216253 mir_body : & ' a mir:: Body < ' tcx > ,
217254
@@ -267,7 +304,7 @@ pub struct CoverageSpans<'a, 'tcx> {
267304 refined_spans : Vec < CoverageSpan > ,
268305}
269306
270- impl < ' a , ' tcx > CoverageSpans < ' a , ' tcx > {
307+ impl < ' a , ' tcx > CoverageSpansGenerator < ' a , ' tcx > {
271308 /// Generate a minimal set of `CoverageSpan`s, each representing a contiguous code region to be
272309 /// counted.
273310 ///
@@ -295,7 +332,7 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
295332 body_span : Span ,
296333 basic_coverage_blocks : & ' a CoverageGraph ,
297334 ) -> Vec < CoverageSpan > {
298- let mut coverage_spans = CoverageSpans {
335+ let mut coverage_spans = Self {
299336 mir_body,
300337 fn_sig_span,
301338 body_span,
@@ -783,7 +820,7 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
783820
784821/// If the MIR `Statement` has a span contributive to computing coverage spans,
785822/// return it; otherwise return `None`.
786- pub ( super ) fn filtered_statement_span ( statement : & Statement < ' _ > ) -> Option < Span > {
823+ fn filtered_statement_span ( statement : & Statement < ' _ > ) -> Option < Span > {
787824 match statement. kind {
788825 // These statements have spans that are often outside the scope of the executed source code
789826 // for their parent `BasicBlock`.
@@ -830,7 +867,7 @@ pub(super) fn filtered_statement_span(statement: &Statement<'_>) -> Option<Span>
830867
831868/// If the MIR `Terminator` has a span contributive to computing coverage spans,
832869/// return it; otherwise return `None`.
833- pub ( super ) fn filtered_terminator_span ( terminator : & Terminator < ' _ > ) -> Option < Span > {
870+ fn filtered_terminator_span ( terminator : & Terminator < ' _ > ) -> Option < Span > {
834871 match terminator. kind {
835872 // These terminators have spans that don't positively contribute to computing a reasonable
836873 // span of actually executed source code. (For example, SwitchInt terminators extracted from
@@ -877,7 +914,7 @@ pub(super) fn filtered_terminator_span(terminator: &Terminator<'_>) -> Option<Sp
877914/// [^1]Expansions result from Rust syntax including macros, syntactic sugar,
878915/// etc.).
879916#[ inline]
880- pub ( super ) fn function_source_span ( span : Span , body_span : Span ) -> Span {
917+ fn function_source_span ( span : Span , body_span : Span ) -> Span {
881918 let original_span = original_sp ( span, body_span) . with_ctxt ( body_span. ctxt ( ) ) ;
882919 if body_span. contains ( original_span) { original_span } else { body_span }
883920}
0 commit comments