@@ -3,86 +3,89 @@ use rustc_middle::mir::{
3
3
} ;
4
4
use rustc_span:: Span ;
5
5
6
- use crate :: coverage:: graph:: { BasicCoverageBlock , BasicCoverageBlockData } ;
7
- use crate :: coverage:: spans:: { CoverageSpan , CoverageSpansGenerator } ;
6
+ use crate :: coverage:: graph:: { BasicCoverageBlock , BasicCoverageBlockData , CoverageGraph } ;
7
+ use crate :: coverage:: spans:: CoverageSpan ;
8
8
9
- impl < ' a , ' tcx > CoverageSpansGenerator < ' a , ' tcx > {
10
- pub ( super ) fn mir_to_initial_sorted_coverage_spans ( & self ) -> Vec < CoverageSpan > {
11
- let mut initial_spans =
12
- Vec :: < CoverageSpan > :: with_capacity ( self . mir_body . basic_blocks . len ( ) * 2 ) ;
13
- for ( bcb, bcb_data) in self . basic_coverage_blocks . iter_enumerated ( ) {
14
- initial_spans. extend ( self . bcb_to_initial_coverage_spans ( bcb, bcb_data) ) ;
15
- }
9
+ pub ( super ) fn mir_to_initial_sorted_coverage_spans (
10
+ mir_body : & mir:: Body < ' _ > ,
11
+ fn_sig_span : Span ,
12
+ body_span : Span ,
13
+ basic_coverage_blocks : & CoverageGraph ,
14
+ ) -> Vec < CoverageSpan > {
15
+ let mut initial_spans = Vec :: < CoverageSpan > :: with_capacity ( mir_body. basic_blocks . len ( ) * 2 ) ;
16
+ for ( bcb, bcb_data) in basic_coverage_blocks. iter_enumerated ( ) {
17
+ initial_spans. extend ( bcb_to_initial_coverage_spans ( mir_body, body_span, bcb, bcb_data) ) ;
18
+ }
16
19
17
- if initial_spans. is_empty ( ) {
18
- // This can happen if, for example, the function is unreachable (contains only a
19
- // `BasicBlock`(s) with an `Unreachable` terminator).
20
- return initial_spans;
21
- }
20
+ if initial_spans. is_empty ( ) {
21
+ // This can happen if, for example, the function is unreachable (contains only a
22
+ // `BasicBlock`(s) with an `Unreachable` terminator).
23
+ return initial_spans;
24
+ }
22
25
23
- initial_spans. push ( CoverageSpan :: for_fn_sig ( self . fn_sig_span ) ) ;
26
+ initial_spans. push ( CoverageSpan :: for_fn_sig ( fn_sig_span) ) ;
24
27
25
- initial_spans. sort_by ( |a, b| {
26
- // First sort by span start.
27
- Ord :: cmp ( & a. span . lo ( ) , & b. span . lo ( ) )
28
- // If span starts are the same, sort by span end in reverse order.
29
- // This ensures that if spans A and B are adjacent in the list,
30
- // and they overlap but are not equal, then either:
31
- // - Span A extends further left, or
32
- // - Both have the same start and span A extends further right
33
- . then_with ( || Ord :: cmp ( & a. span . hi ( ) , & b. span . hi ( ) ) . reverse ( ) )
34
- // If both spans are equal, sort the BCBs in dominator order,
35
- // so that dominating BCBs come before other BCBs they dominate.
36
- . then_with ( || self . basic_coverage_blocks . cmp_in_dominator_order ( a. bcb , b. bcb ) )
37
- // If two spans are otherwise identical, put closure spans first,
38
- // as this seems to be what the refinement step expects.
39
- . then_with ( || Ord :: cmp ( & a. is_closure , & b. is_closure ) . reverse ( ) )
40
- } ) ;
28
+ initial_spans. sort_by ( |a, b| {
29
+ // First sort by span start.
30
+ Ord :: cmp ( & a. span . lo ( ) , & b. span . lo ( ) )
31
+ // If span starts are the same, sort by span end in reverse order.
32
+ // This ensures that if spans A and B are adjacent in the list,
33
+ // and they overlap but are not equal, then either:
34
+ // - Span A extends further left, or
35
+ // - Both have the same start and span A extends further right
36
+ . then_with ( || Ord :: cmp ( & a. span . hi ( ) , & b. span . hi ( ) ) . reverse ( ) )
37
+ // If both spans are equal, sort the BCBs in dominator order,
38
+ // so that dominating BCBs come before other BCBs they dominate.
39
+ . then_with ( || basic_coverage_blocks. cmp_in_dominator_order ( a. bcb , b. bcb ) )
40
+ // If two spans are otherwise identical, put closure spans first,
41
+ // as this seems to be what the refinement step expects.
42
+ . then_with ( || Ord :: cmp ( & a. is_closure , & b. is_closure ) . reverse ( ) )
43
+ } ) ;
41
44
42
- initial_spans
43
- }
45
+ initial_spans
46
+ }
44
47
45
- // Generate a set of `CoverageSpan`s from the filtered set of `Statement`s and `Terminator`s of
46
- // the `BasicBlock`(s) in the given `BasicCoverageBlockData`. One `CoverageSpan` is generated
47
- // for each `Statement` and `Terminator`. (Note that subsequent stages of coverage analysis will
48
- // merge some `CoverageSpan`s, at which point a `CoverageSpan` may represent multiple
49
- // `Statement`s and/or `Terminator`s.)
50
- fn bcb_to_initial_coverage_spans (
51
- & self ,
52
- bcb : BasicCoverageBlock ,
53
- bcb_data : & ' a BasicCoverageBlockData ,
54
- ) -> Vec < CoverageSpan > {
55
- bcb_data
56
- . basic_blocks
57
- . iter ( )
58
- . flat_map ( |& bb| {
59
- let data = & self . mir_body [ bb] ;
60
- data. statements
61
- . iter ( )
62
- . enumerate ( )
63
- . filter_map ( move |( index, statement) | {
64
- filtered_statement_span ( statement) . map ( |span| {
65
- CoverageSpan :: for_statement (
66
- statement,
67
- function_source_span ( span, self . body_span ) ,
68
- span,
69
- bcb,
70
- bb,
71
- index,
72
- )
73
- } )
74
- } )
75
- . chain ( filtered_terminator_span ( data. terminator ( ) ) . map ( |span| {
76
- CoverageSpan :: for_terminator (
77
- function_source_span ( span, self . body_span ) ,
48
+ // Generate a set of `CoverageSpan`s from the filtered set of `Statement`s and `Terminator`s of
49
+ // the `BasicBlock`(s) in the given `BasicCoverageBlockData`. One `CoverageSpan` is generated
50
+ // for each `Statement` and `Terminator`. (Note that subsequent stages of coverage analysis will
51
+ // merge some `CoverageSpan`s, at which point a `CoverageSpan` may represent multiple
52
+ // `Statement`s and/or `Terminator`s.)
53
+ fn bcb_to_initial_coverage_spans (
54
+ mir_body : & mir:: Body < ' _ > ,
55
+ body_span : Span ,
56
+ bcb : BasicCoverageBlock ,
57
+ bcb_data : & BasicCoverageBlockData ,
58
+ ) -> Vec < CoverageSpan > {
59
+ bcb_data
60
+ . basic_blocks
61
+ . iter ( )
62
+ . flat_map ( |& bb| {
63
+ let data = & mir_body[ bb] ;
64
+ data. statements
65
+ . iter ( )
66
+ . enumerate ( )
67
+ . filter_map ( move |( index, statement) | {
68
+ filtered_statement_span ( statement) . map ( |span| {
69
+ CoverageSpan :: for_statement (
70
+ statement,
71
+ function_source_span ( span, body_span) ,
78
72
span,
79
73
bcb,
80
74
bb,
75
+ index,
81
76
)
82
- } ) )
83
- } )
84
- . collect ( )
85
- }
77
+ } )
78
+ } )
79
+ . chain ( filtered_terminator_span ( data. terminator ( ) ) . map ( |span| {
80
+ CoverageSpan :: for_terminator (
81
+ function_source_span ( span, body_span) ,
82
+ span,
83
+ bcb,
84
+ bb,
85
+ )
86
+ } ) )
87
+ } )
88
+ . collect ( )
86
89
}
87
90
88
91
/// If the MIR `Statement` has a span contributive to computing coverage spans,
0 commit comments