1
1
use rustc_middle:: hir;
2
2
use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
3
- use rustc_middle:: mir;
4
- use rustc_middle:: mir:: coverage :: BranchSpan ;
3
+ use rustc_middle:: mir:: coverage :: { BlockMarkerId , BranchSpan , CoverageKind } ;
4
+ use rustc_middle:: mir:: { self , BasicBlock , SourceInfo , Statement , StatementKind } ;
5
5
use rustc_middle:: ty:: TyCtxt ;
6
6
use rustc_span:: def_id:: LocalDefId ;
7
7
use rustc_span:: { ExpnKind , Span } ;
8
8
9
+ use crate :: build:: Builder ;
10
+
9
11
pub ( crate ) struct HirInfoBuilder {
10
12
pub ( crate ) branch_coverage_enabled : bool ,
11
13
pub ( crate ) num_block_markers : usize ,
@@ -25,6 +27,12 @@ impl HirInfoBuilder {
25
27
}
26
28
}
27
29
30
+ fn next_block_marker_id ( & mut self ) -> BlockMarkerId {
31
+ let id = BlockMarkerId :: from_usize ( self . num_block_markers ) ;
32
+ self . num_block_markers += 1 ;
33
+ id
34
+ }
35
+
28
36
pub ( crate ) fn finish ( self , tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> Box < mir:: coverage:: HirInfo > {
29
37
Box :: new ( make_coverage_hir_info ( tcx, def_id, self ) )
30
38
}
@@ -138,3 +146,38 @@ fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx rustc_hir::Body<'tcx
138
146
. to_smaller_hash ( )
139
147
. as_u64 ( )
140
148
}
149
+
150
+ impl < ' a , ' tcx > Builder < ' a , ' tcx > {
151
+ pub ( crate ) fn coverage_add_branch (
152
+ & mut self ,
153
+ cond_source_info : SourceInfo ,
154
+ then_blk : BasicBlock ,
155
+ else_blk : BasicBlock ,
156
+ ) {
157
+ let hir_info_builder = self . coverage . as_mut ( ) . unwrap ( ) ;
158
+ assert ! ( hir_info_builder. branch_coverage_enabled) ;
159
+
160
+ let mut inject_branch_marker = |bb : BasicBlock | {
161
+ let id = hir_info_builder. next_block_marker_id ( ) ;
162
+
163
+ let statement = Statement {
164
+ source_info : cond_source_info,
165
+ kind : StatementKind :: Coverage ( Box :: new ( mir:: Coverage {
166
+ kind : CoverageKind :: BlockMarker { id } ,
167
+ } ) ) ,
168
+ } ;
169
+
170
+ self . cfg . push ( bb, statement) ;
171
+ id
172
+ } ;
173
+
174
+ let true_marker = inject_branch_marker ( then_blk) ;
175
+ let false_marker = inject_branch_marker ( else_blk) ;
176
+
177
+ hir_info_builder. branch_spans . push ( BranchSpan {
178
+ span : cond_source_info. span ,
179
+ true_marker,
180
+ false_marker,
181
+ } ) ;
182
+ }
183
+ }
0 commit comments