@@ -2,7 +2,9 @@ use crate::coverageinfo::ffi::{Counter, CounterExpression, ExprKind};
22
33use rustc_data_structures:: fx:: FxIndexSet ;
44use rustc_index:: IndexVec ;
5- use rustc_middle:: mir:: coverage:: { CodeRegion , CounterId , ExpressionId , Op , Operand } ;
5+ use rustc_middle:: mir:: coverage:: {
6+ CodeRegion , CounterId , ExpressionId , FunctionCoverageInfo , Op , Operand ,
7+ } ;
68use rustc_middle:: ty:: Instance ;
79use rustc_middle:: ty:: TyCtxt ;
810
@@ -27,8 +29,8 @@ pub struct Expression {
2729/// line."
2830#[ derive( Debug ) ]
2931pub struct FunctionCoverage < ' tcx > {
30- instance : Instance < ' tcx > ,
31- source_hash : u64 ,
32+ /// Coverage info that was attached to this function by the instrumentor.
33+ function_coverage_info : & ' tcx FunctionCoverageInfo ,
3234 is_used : bool ,
3335 counters : IndexVec < CounterId , Option < Vec < CodeRegion > > > ,
3436 expressions : IndexVec < ExpressionId , Option < Expression > > ,
@@ -37,24 +39,36 @@ pub struct FunctionCoverage<'tcx> {
3739
3840impl < ' tcx > FunctionCoverage < ' tcx > {
3941 /// Creates a new set of coverage data for a used (called) function.
40- pub fn new ( tcx : TyCtxt < ' tcx > , instance : Instance < ' tcx > ) -> Self {
41- Self :: create ( tcx, instance, true )
42+ pub fn new (
43+ tcx : TyCtxt < ' tcx > ,
44+ instance : Instance < ' tcx > ,
45+ function_coverage_info : & ' tcx FunctionCoverageInfo ,
46+ ) -> Self {
47+ Self :: create ( tcx, instance, function_coverage_info, true )
4248 }
4349
4450 /// Creates a new set of coverage data for an unused (never called) function.
45- pub fn unused ( tcx : TyCtxt < ' tcx > , instance : Instance < ' tcx > ) -> Self {
46- Self :: create ( tcx, instance, false )
51+ pub fn unused (
52+ tcx : TyCtxt < ' tcx > ,
53+ instance : Instance < ' tcx > ,
54+ function_coverage_info : & ' tcx FunctionCoverageInfo ,
55+ ) -> Self {
56+ Self :: create ( tcx, instance, function_coverage_info, false )
4757 }
4858
49- fn create ( tcx : TyCtxt < ' tcx > , instance : Instance < ' tcx > , is_used : bool ) -> Self {
59+ fn create (
60+ tcx : TyCtxt < ' tcx > ,
61+ instance : Instance < ' tcx > ,
62+ function_coverage_info : & ' tcx FunctionCoverageInfo ,
63+ is_used : bool ,
64+ ) -> Self {
5065 let coverageinfo = tcx. coverageinfo ( instance. def ) ;
5166 debug ! (
5267 "FunctionCoverage::create(instance={:?}) has coverageinfo={:?}. is_used={}" ,
5368 instance, coverageinfo, is_used
5469 ) ;
5570 Self {
56- instance,
57- source_hash : 0 , // will be set with the first `add_counter()`
71+ function_coverage_info,
5872 is_used,
5973 counters : IndexVec :: from_elem_n ( None , coverageinfo. num_counters as usize ) ,
6074 expressions : IndexVec :: from_elem_n ( None , coverageinfo. num_expressions as usize ) ,
@@ -67,16 +81,6 @@ impl<'tcx> FunctionCoverage<'tcx> {
6781 self . is_used
6882 }
6983
70- /// Sets the function source hash value. If called multiple times for the same function, all
71- /// calls should have the same hash value.
72- pub fn set_function_source_hash ( & mut self , source_hash : u64 ) {
73- if self . source_hash == 0 {
74- self . source_hash = source_hash;
75- } else {
76- debug_assert_eq ! ( source_hash, self . source_hash) ;
77- }
78- }
79-
8084 /// Adds code regions to be counted by an injected counter intrinsic.
8185 #[ instrument( level = "debug" , skip( self ) ) ]
8286 pub ( crate ) fn add_counter ( & mut self , id : CounterId , code_regions : & [ CodeRegion ] ) {
@@ -195,7 +199,7 @@ impl<'tcx> FunctionCoverage<'tcx> {
195199 /// Return the source hash, generated from the HIR node structure, and used to indicate whether
196200 /// or not the source code structure changed between different compilations.
197201 pub fn source_hash ( & self ) -> u64 {
198- self . source_hash
202+ if self . is_used { self . function_coverage_info . function_source_hash } else { 0 }
199203 }
200204
201205 /// Generate an array of CounterExpressions, and an iterator over all `Counter`s and their
@@ -204,12 +208,6 @@ impl<'tcx> FunctionCoverage<'tcx> {
204208 pub fn get_expressions_and_counter_regions (
205209 & self ,
206210 ) -> ( Vec < CounterExpression > , impl Iterator < Item = ( Counter , & CodeRegion ) > ) {
207- assert ! (
208- self . source_hash != 0 || !self . is_used,
209- "No counters provided the source_hash for used function: {:?}" ,
210- self . instance
211- ) ;
212-
213211 let counter_expressions = self . counter_expressions ( ) ;
214212 // Expression IDs are indices into `self.expressions`, and on the LLVM
215213 // side they will be treated as indices into `counter_expressions`, so
0 commit comments