Skip to content

Commit 15a7ec1

Browse files
committed
coverage: Only merge adjacent coverage spans
This also removes some manipulation of the function signature span that only made sense in the context of merging non-adjacent spans.
1 parent d2eadb7 commit 15a7ec1

File tree

145 files changed

+2831
-1833
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+2831
-1833
lines changed

compiler/rustc_mir_transform/src/coverage/mappings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub(super) fn extract_all_mapping_info_from_mir<'tcx>(
9191
// When debugging flag `-Zcoverage-options=no-mir-spans` is set, we need
9292
// to give the same treatment to _all_ functions, because `llvm-cov`
9393
// seems to ignore functions that don't have any ordinary code spans.
94-
if let Some(span) = hir_info.fn_sig_span_extended {
94+
if let Some(span) = hir_info.fn_sig_span {
9595
code_mappings.push(CodeMapping { span, bcb: START_BCB });
9696
}
9797
} else {

compiler/rustc_mir_transform/src/coverage/mod.rs

+11-21
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,9 @@ fn inject_statement(mir_body: &mut mir::Body<'_>, counter_kind: CoverageKind, bb
268268
struct ExtractedHirInfo {
269269
function_source_hash: u64,
270270
is_async_fn: bool,
271-
/// The span of the function's signature, extended to the start of `body_span`.
271+
/// The span of the function's signature, if available.
272272
/// Must have the same context and filename as the body span.
273-
fn_sig_span_extended: Option<Span>,
273+
fn_sig_span: Option<Span>,
274274
body_span: Span,
275275
/// "Holes" are regions within the function body (or its expansions) that
276276
/// should not be included in coverage spans for this function
@@ -308,30 +308,20 @@ fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHir
308308

309309
// The actual signature span is only used if it has the same context and
310310
// filename as the body, and precedes the body.
311-
let fn_sig_span_extended = maybe_fn_sig
312-
.map(|fn_sig| fn_sig.span)
313-
.filter(|&fn_sig_span| {
314-
let source_map = tcx.sess.source_map();
315-
let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo());
316-
317-
fn_sig_span.eq_ctxt(body_span)
318-
&& fn_sig_span.hi() <= body_span.lo()
319-
&& file_idx(fn_sig_span) == file_idx(body_span)
320-
})
321-
// If so, extend it to the start of the body span.
322-
.map(|fn_sig_span| fn_sig_span.with_hi(body_span.lo()));
311+
let fn_sig_span = maybe_fn_sig.map(|fn_sig| fn_sig.span).filter(|&fn_sig_span| {
312+
let source_map = tcx.sess.source_map();
313+
let file_idx = |span: Span| source_map.lookup_source_file_idx(span.lo());
314+
315+
fn_sig_span.eq_ctxt(body_span)
316+
&& fn_sig_span.hi() <= body_span.lo()
317+
&& file_idx(fn_sig_span) == file_idx(body_span)
318+
});
323319

324320
let function_source_hash = hash_mir_source(tcx, hir_body);
325321

326322
let hole_spans = extract_hole_spans_from_hir(tcx, hir_body);
327323

328-
ExtractedHirInfo {
329-
function_source_hash,
330-
is_async_fn,
331-
fn_sig_span_extended,
332-
body_span,
333-
hole_spans,
334-
}
324+
ExtractedHirInfo { function_source_hash, is_async_fn, fn_sig_span, body_span, hole_spans }
335325
}
336326

337327
fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) -> u64 {

compiler/rustc_mir_transform/src/coverage/spans.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,8 @@ pub(super) fn extract_refined_covspans<'tcx>(
4242
return;
4343
}
4444

45-
// Also add the adjusted function signature span, if available.
46-
// Otherwise, add a fake span at the start of the body, to avoid an ugly
47-
// gap between the start of the body and the first real span.
48-
// FIXME: Find a more principled way to solve this problem.
49-
covspans.push(SpanFromMir::for_fn_sig(
50-
hir_info.fn_sig_span_extended.unwrap_or_else(|| body_span.shrink_to_lo()),
51-
));
45+
// Also add the function signature span, if available.
46+
covspans.extend(hir_info.fn_sig_span.map(SpanFromMir::for_fn_sig));
5247

5348
// First, perform the passes that need macro information.
5449
covspans.sort_by(|a, b| graph.cmp_in_dominator_order(a.bcb, b.bcb));
@@ -227,19 +222,21 @@ struct Covspan {
227222
}
228223

229224
impl Covspan {
230-
/// If `self` and `other` can be merged (i.e. they have the same BCB),
231-
/// mutates `self.span` to also include `other.span` and returns true.
225+
/// If `self` and `other` can be merged, mutates `self.span` to also
226+
/// include `other.span` and returns true.
232227
///
233-
/// Note that compatible covspans can be merged even if their underlying
234-
/// spans are not overlapping/adjacent; any space between them will also be
235-
/// part of the merged covspan.
228+
/// Two covspans can be merged if they have the same BCB, and they are
229+
/// overlapping or adjacent.
236230
fn merge_if_eligible(&mut self, other: &Self) -> bool {
237-
if self.bcb != other.bcb {
238-
return false;
231+
let eligible_for_merge =
232+
|a: &Self, b: &Self| (a.bcb == b.bcb) && a.span.overlaps_or_adjacent(b.span);
233+
234+
if eligible_for_merge(self, other) {
235+
self.span = self.span.to(other.span);
236+
true
237+
} else {
238+
false
239239
}
240-
241-
self.span = self.span.to(other.span);
242-
true
243240
}
244241
}
245242

tests/coverage/abort.cov-map

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function name: abort::main
2-
Raw bytes (83): 0x[01, 01, 07, 05, 01, 05, 0b, 01, 09, 05, 13, 01, 0d, 05, 1b, 01, 11, 0d, 01, 0d, 01, 01, 1b, 05, 02, 0b, 00, 18, 02, 01, 0c, 00, 19, 09, 00, 1a, 02, 0a, 06, 02, 09, 00, 0a, 02, 02, 0c, 00, 19, 0d, 00, 1a, 00, 31, 0e, 00, 30, 00, 31, 02, 04, 0c, 00, 19, 11, 00, 1a, 00, 31, 16, 00, 30, 00, 31, 02, 01, 09, 00, 17, 01, 02, 05, 01, 02]
2+
Raw bytes (98): 0x[01, 01, 07, 05, 01, 05, 0b, 01, 09, 05, 13, 01, 0d, 05, 1b, 01, 11, 10, 01, 0d, 01, 00, 1c, 01, 01, 09, 00, 16, 01, 00, 19, 00, 1b, 05, 01, 0b, 00, 18, 02, 01, 0c, 00, 19, 09, 00, 1a, 02, 0a, 06, 02, 09, 00, 0a, 02, 02, 0c, 00, 19, 0d, 00, 1a, 00, 31, 0e, 00, 30, 00, 31, 02, 04, 0c, 00, 19, 11, 00, 1a, 00, 31, 16, 00, 30, 00, 31, 02, 01, 09, 00, 17, 01, 02, 05, 00, 0b, 01, 01, 01, 00, 02]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 7
@@ -10,9 +10,11 @@ Number of expressions: 7
1010
- expression 4 operands: lhs = Counter(0), rhs = Counter(3)
1111
- expression 5 operands: lhs = Counter(1), rhs = Expression(6, Add)
1212
- expression 6 operands: lhs = Counter(0), rhs = Counter(4)
13-
Number of file 0 mappings: 13
14-
- Code(Counter(0)) at (prev + 13, 1) to (start + 1, 27)
15-
- Code(Counter(1)) at (prev + 2, 11) to (start + 0, 24)
13+
Number of file 0 mappings: 16
14+
- Code(Counter(0)) at (prev + 13, 1) to (start + 0, 28)
15+
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 22)
16+
- Code(Counter(0)) at (prev + 0, 25) to (start + 0, 27)
17+
- Code(Counter(1)) at (prev + 1, 11) to (start + 0, 24)
1618
- Code(Expression(0, Sub)) at (prev + 1, 12) to (start + 0, 25)
1719
= (c1 - c0)
1820
- Code(Counter(2)) at (prev + 0, 26) to (start + 2, 10)
@@ -30,19 +32,25 @@ Number of file 0 mappings: 13
3032
= (c1 - (c0 + c4))
3133
- Code(Expression(0, Sub)) at (prev + 1, 9) to (start + 0, 23)
3234
= (c1 - c0)
33-
- Code(Counter(0)) at (prev + 2, 5) to (start + 1, 2)
35+
- Code(Counter(0)) at (prev + 2, 5) to (start + 0, 11)
36+
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
3437
Highest counter ID seen: c4
3538

3639
Function name: abort::might_abort
37-
Raw bytes (21): 0x[01, 01, 01, 01, 05, 03, 01, 03, 01, 01, 14, 05, 02, 09, 01, 0f, 02, 02, 0c, 03, 02]
40+
Raw bytes (41): 0x[01, 01, 01, 01, 05, 07, 01, 03, 01, 00, 2e, 01, 01, 08, 00, 14, 05, 01, 09, 00, 11, 05, 00, 12, 00, 1f, 05, 01, 09, 00, 0f, 02, 01, 0c, 02, 06, 02, 03, 01, 00, 02]
3841
Number of files: 1
3942
- file 0 => global file 1
4043
Number of expressions: 1
4144
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
42-
Number of file 0 mappings: 3
43-
- Code(Counter(0)) at (prev + 3, 1) to (start + 1, 20)
44-
- Code(Counter(1)) at (prev + 2, 9) to (start + 1, 15)
45-
- Code(Expression(0, Sub)) at (prev + 2, 12) to (start + 3, 2)
45+
Number of file 0 mappings: 7
46+
- Code(Counter(0)) at (prev + 3, 1) to (start + 0, 46)
47+
- Code(Counter(0)) at (prev + 1, 8) to (start + 0, 20)
48+
- Code(Counter(1)) at (prev + 1, 9) to (start + 0, 17)
49+
- Code(Counter(1)) at (prev + 0, 18) to (start + 0, 31)
50+
- Code(Counter(1)) at (prev + 1, 9) to (start + 0, 15)
51+
- Code(Expression(0, Sub)) at (prev + 1, 12) to (start + 2, 6)
52+
= (c0 - c1)
53+
- Code(Expression(0, Sub)) at (prev + 3, 1) to (start + 0, 2)
4654
= (c0 - c1)
4755
Highest counter ID seen: c1
4856

tests/coverage/assert-ne.cov-map

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
Function name: assert_ne::main
2-
Raw bytes (28): 0x[01, 01, 02, 01, 05, 01, 09, 04, 01, 08, 01, 03, 15, 05, 04, 0d, 00, 13, 02, 02, 0d, 00, 13, 06, 03, 05, 01, 02]
2+
Raw bytes (55): 0x[01, 01, 03, 01, 05, 01, 09, 01, 09, 09, 01, 08, 01, 00, 0a, 01, 01, 05, 00, 0f, 01, 01, 09, 00, 12, 01, 00, 13, 00, 19, 01, 01, 0c, 00, 15, 05, 01, 0d, 00, 13, 02, 02, 0d, 00, 13, 0a, 03, 05, 00, 07, 0a, 01, 01, 00, 02]
33
Number of files: 1
44
- file 0 => global file 1
5-
Number of expressions: 2
5+
Number of expressions: 3
66
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
77
- expression 1 operands: lhs = Counter(0), rhs = Counter(2)
8-
Number of file 0 mappings: 4
9-
- Code(Counter(0)) at (prev + 8, 1) to (start + 3, 21)
10-
- Code(Counter(1)) at (prev + 4, 13) to (start + 0, 19)
8+
- expression 2 operands: lhs = Counter(0), rhs = Counter(2)
9+
Number of file 0 mappings: 9
10+
- Code(Counter(0)) at (prev + 8, 1) to (start + 0, 10)
11+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 15)
12+
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 18)
13+
- Code(Counter(0)) at (prev + 0, 19) to (start + 0, 25)
14+
- Code(Counter(0)) at (prev + 1, 12) to (start + 0, 21)
15+
- Code(Counter(1)) at (prev + 1, 13) to (start + 0, 19)
1116
- Code(Expression(0, Sub)) at (prev + 2, 13) to (start + 0, 19)
1217
= (c0 - c1)
13-
- Code(Expression(1, Sub)) at (prev + 3, 5) to (start + 1, 2)
18+
- Code(Expression(2, Sub)) at (prev + 3, 5) to (start + 0, 7)
19+
= (c0 - c2)
20+
- Code(Expression(2, Sub)) at (prev + 1, 1) to (start + 0, 2)
1421
= (c0 - c2)
1522
Highest counter ID seen: c1
1623

tests/coverage/assert.cov-map

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function name: assert::main
2-
Raw bytes (61): 0x[01, 01, 06, 05, 01, 05, 17, 01, 09, 05, 13, 17, 0d, 01, 09, 09, 01, 09, 01, 01, 1b, 05, 02, 0b, 00, 18, 02, 01, 0c, 00, 1a, 09, 00, 1b, 02, 0a, 06, 02, 13, 00, 20, 0d, 00, 21, 02, 0a, 0e, 02, 09, 00, 0a, 02, 01, 09, 00, 17, 01, 02, 05, 01, 02]
2+
Raw bytes (76): 0x[01, 01, 06, 05, 01, 05, 17, 01, 09, 05, 13, 17, 0d, 01, 09, 0c, 01, 09, 01, 00, 1c, 01, 01, 09, 00, 16, 01, 00, 19, 00, 1b, 05, 01, 0b, 00, 18, 02, 01, 0c, 00, 1a, 09, 00, 1b, 02, 0a, 06, 02, 13, 00, 20, 0d, 00, 21, 02, 0a, 0e, 02, 09, 00, 0a, 02, 01, 09, 00, 17, 01, 02, 05, 00, 0b, 01, 01, 01, 00, 02]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 6
@@ -9,9 +9,11 @@ Number of expressions: 6
99
- expression 3 operands: lhs = Counter(1), rhs = Expression(4, Add)
1010
- expression 4 operands: lhs = Expression(5, Add), rhs = Counter(3)
1111
- expression 5 operands: lhs = Counter(0), rhs = Counter(2)
12-
Number of file 0 mappings: 9
13-
- Code(Counter(0)) at (prev + 9, 1) to (start + 1, 27)
14-
- Code(Counter(1)) at (prev + 2, 11) to (start + 0, 24)
12+
Number of file 0 mappings: 12
13+
- Code(Counter(0)) at (prev + 9, 1) to (start + 0, 28)
14+
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 22)
15+
- Code(Counter(0)) at (prev + 0, 25) to (start + 0, 27)
16+
- Code(Counter(1)) at (prev + 1, 11) to (start + 0, 24)
1517
- Code(Expression(0, Sub)) at (prev + 1, 12) to (start + 0, 26)
1618
= (c1 - c0)
1719
- Code(Counter(2)) at (prev + 0, 27) to (start + 2, 10)
@@ -22,18 +24,22 @@ Number of file 0 mappings: 9
2224
= (c1 - ((c0 + c2) + c3))
2325
- Code(Expression(0, Sub)) at (prev + 1, 9) to (start + 0, 23)
2426
= (c1 - c0)
25-
- Code(Counter(0)) at (prev + 2, 5) to (start + 1, 2)
27+
- Code(Counter(0)) at (prev + 2, 5) to (start + 0, 11)
28+
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
2629
Highest counter ID seen: c3
2730

2831
Function name: assert::might_fail_assert
29-
Raw bytes (21): 0x[01, 01, 01, 01, 05, 03, 01, 04, 01, 02, 0f, 02, 02, 25, 00, 3d, 05, 01, 01, 00, 02]
32+
Raw bytes (36): 0x[01, 01, 01, 01, 05, 06, 01, 04, 01, 00, 28, 01, 01, 05, 00, 0d, 01, 00, 0e, 00, 20, 01, 01, 05, 00, 0f, 02, 00, 25, 00, 3d, 05, 01, 01, 00, 02]
3033
Number of files: 1
3134
- file 0 => global file 1
3235
Number of expressions: 1
3336
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
34-
Number of file 0 mappings: 3
35-
- Code(Counter(0)) at (prev + 4, 1) to (start + 2, 15)
36-
- Code(Expression(0, Sub)) at (prev + 2, 37) to (start + 0, 61)
37+
Number of file 0 mappings: 6
38+
- Code(Counter(0)) at (prev + 4, 1) to (start + 0, 40)
39+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 13)
40+
- Code(Counter(0)) at (prev + 0, 14) to (start + 0, 32)
41+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 15)
42+
- Code(Expression(0, Sub)) at (prev + 0, 37) to (start + 0, 61)
3743
= (c0 - c1)
3844
- Code(Counter(1)) at (prev + 1, 1) to (start + 0, 2)
3945
Highest counter ID seen: c1

tests/coverage/assert_not.cov-map

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
Function name: assert_not::main
2-
Raw bytes (29): 0x[01, 01, 00, 05, 01, 06, 01, 01, 11, 01, 02, 05, 00, 13, 01, 01, 05, 00, 13, 01, 01, 05, 00, 15, 01, 01, 01, 00, 02]
2+
Raw bytes (54): 0x[01, 01, 00, 0a, 01, 06, 01, 00, 0a, 01, 01, 05, 00, 0c, 01, 00, 0d, 00, 11, 01, 01, 05, 00, 0c, 01, 00, 0d, 00, 13, 01, 01, 05, 00, 0c, 01, 00, 0d, 00, 13, 01, 01, 05, 00, 0c, 01, 00, 0d, 00, 15, 01, 01, 01, 00, 02]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 0
6-
Number of file 0 mappings: 5
7-
- Code(Counter(0)) at (prev + 6, 1) to (start + 1, 17)
8-
- Code(Counter(0)) at (prev + 2, 5) to (start + 0, 19)
9-
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 19)
10-
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 21)
6+
Number of file 0 mappings: 10
7+
- Code(Counter(0)) at (prev + 6, 1) to (start + 0, 10)
8+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 12)
9+
- Code(Counter(0)) at (prev + 0, 13) to (start + 0, 17)
10+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 12)
11+
- Code(Counter(0)) at (prev + 0, 13) to (start + 0, 19)
12+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 12)
13+
- Code(Counter(0)) at (prev + 0, 13) to (start + 0, 19)
14+
- Code(Counter(0)) at (prev + 1, 5) to (start + 0, 12)
15+
- Code(Counter(0)) at (prev + 0, 13) to (start + 0, 21)
1116
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
1217
Highest counter ID seen: c0
1318

0 commit comments

Comments
 (0)