Skip to content

Commit a3bf870

Browse files
committed
coverage: Test some edge cases involving macro expansion
1 parent 3ff30e7 commit a3bf870

File tree

6 files changed

+294
-0
lines changed

6 files changed

+294
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Function name: fn_in_macro::branch_in_macro
2+
Raw bytes (19): 0x[01, 01, 00, 03, 01, 22, 01, 00, 15, 01, 0b, 05, 00, 17, 01, 01, 01, 00, 02]
3+
Number of files: 1
4+
- file 0 => $DIR/fn-in-macro.rs
5+
Number of expressions: 0
6+
Number of file 0 mappings: 3
7+
- Code(Counter(0)) at (prev + 34, 1) to (start + 0, 21)
8+
- Code(Counter(0)) at (prev + 11, 5) to (start + 0, 23)
9+
- Code(Counter(0)) at (prev + 1, 1) to (start + 0, 2)
10+
Highest counter ID seen: c0
11+
12+
Function name: fn_in_macro::fn_in_macro
13+
Raw bytes (31): 0x[01, 01, 01, 01, 05, 05, 01, 0c, 09, 00, 19, 01, 01, 10, 00, 25, 05, 00, 2c, 02, 0e, 02, 02, 14, 02, 0e, 01, 03, 09, 00, 0a]
14+
Number of files: 1
15+
- file 0 => $DIR/fn-in-macro.rs
16+
Number of expressions: 1
17+
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
18+
Number of file 0 mappings: 5
19+
- Code(Counter(0)) at (prev + 12, 9) to (start + 0, 25)
20+
- Code(Counter(0)) at (prev + 1, 16) to (start + 0, 37)
21+
- Code(Counter(1)) at (prev + 0, 44) to (start + 2, 14)
22+
- Code(Expression(0, Sub)) at (prev + 2, 20) to (start + 2, 14)
23+
= (c0 - c1)
24+
- Code(Counter(0)) at (prev + 3, 9) to (start + 0, 10)
25+
Highest counter ID seen: c1
26+
27+
Function name: fn_in_macro::fn_not_in_macro
28+
Raw bytes (38): 0x[01, 01, 01, 01, 05, 06, 01, 19, 01, 00, 15, 01, 01, 08, 00, 1d, 20, 05, 02, 00, 08, 00, 23, 05, 00, 24, 02, 06, 02, 02, 0c, 02, 06, 01, 03, 01, 00, 02]
29+
Number of files: 1
30+
- file 0 => $DIR/fn-in-macro.rs
31+
Number of expressions: 1
32+
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
33+
Number of file 0 mappings: 6
34+
- Code(Counter(0)) at (prev + 25, 1) to (start + 0, 21)
35+
- Code(Counter(0)) at (prev + 1, 8) to (start + 0, 29)
36+
- Branch { true: Counter(1), false: Expression(0, Sub) } at (prev + 0, 8) to (start + 0, 35)
37+
true = c1
38+
false = (c0 - c1)
39+
- Code(Counter(1)) at (prev + 0, 36) to (start + 2, 6)
40+
- Code(Expression(0, Sub)) at (prev + 2, 12) to (start + 2, 6)
41+
= (c0 - c1)
42+
- Code(Counter(0)) at (prev + 3, 1) to (start + 0, 2)
43+
Highest counter ID seen: c1
44+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
LL| |#![feature(coverage_attribute)]
2+
LL| |//@ edition: 2024
3+
LL| |//@ compile-flags: -Zcoverage-options=branch
4+
LL| |//@ llvm-cov-flags: --show-branches=count
5+
LL| |
6+
LL| |// Snapshot test demonstrating how branch coverage interacts with code in macros.
7+
LL| |// This test captures current behavior, which is not necessarily "correct".
8+
LL| |
9+
LL| |macro_rules! define_fn {
10+
LL| | () => {
11+
LL| | /// Function defined entirely within a macro.
12+
LL| 1| fn fn_in_macro() {
13+
LL| 1| if core::hint::black_box(true) {
14+
LL| 1| say("true");
15+
LL| 1| } else {
16+
LL| 0| say("false");
17+
LL| 0| }
18+
LL| 1| }
19+
LL| | };
20+
LL| |}
21+
LL| |
22+
LL| |define_fn!();
23+
LL| |
24+
LL| |/// Function not in a macro at all, for comparison.
25+
LL| 1|fn fn_not_in_macro() {
26+
LL| 1| if core::hint::black_box(true) {
27+
------------------
28+
| Branch (LL:8): [True: 1, False: 0]
29+
------------------
30+
LL| 1| say("true");
31+
LL| 1| } else {
32+
LL| 0| say("false");
33+
LL| 0| }
34+
LL| 1|}
35+
LL| |
36+
LL| |/// Function that is not in a macro, containing a branch that is in a macro.
37+
LL| 1|fn branch_in_macro() {
38+
LL| | macro_rules! macro_with_branch {
39+
LL| | () => {{
40+
LL| | if core::hint::black_box(true) {
41+
LL| | say("true");
42+
LL| | } else {
43+
LL| | say("false");
44+
LL| | }
45+
LL| | }};
46+
LL| | }
47+
LL| |
48+
LL| 1| macro_with_branch!();
49+
LL| 1|}
50+
LL| |
51+
LL| |#[coverage(off)]
52+
LL| |fn main() {
53+
LL| | fn_in_macro();
54+
LL| | fn_not_in_macro();
55+
LL| | branch_in_macro();
56+
LL| |}
57+
LL| |
58+
LL| |#[coverage(off)]
59+
LL| |fn say(message: &str) {
60+
LL| | println!("{message}");
61+
LL| |}
62+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#![feature(coverage_attribute)]
2+
//@ edition: 2024
3+
//@ compile-flags: -Zcoverage-options=branch
4+
//@ llvm-cov-flags: --show-branches=count
5+
6+
// Snapshot test demonstrating how branch coverage interacts with code in macros.
7+
// This test captures current behavior, which is not necessarily "correct".
8+
9+
macro_rules! define_fn {
10+
() => {
11+
/// Function defined entirely within a macro.
12+
fn fn_in_macro() {
13+
if core::hint::black_box(true) {
14+
say("true");
15+
} else {
16+
say("false");
17+
}
18+
}
19+
};
20+
}
21+
22+
define_fn!();
23+
24+
/// Function not in a macro at all, for comparison.
25+
fn fn_not_in_macro() {
26+
if core::hint::black_box(true) {
27+
say("true");
28+
} else {
29+
say("false");
30+
}
31+
}
32+
33+
/// Function that is not in a macro, containing a branch that is in a macro.
34+
fn branch_in_macro() {
35+
macro_rules! macro_with_branch {
36+
() => {{
37+
if core::hint::black_box(true) {
38+
say("true");
39+
} else {
40+
say("false");
41+
}
42+
}};
43+
}
44+
45+
macro_with_branch!();
46+
}
47+
48+
#[coverage(off)]
49+
fn main() {
50+
fn_in_macro();
51+
fn_not_in_macro();
52+
branch_in_macro();
53+
}
54+
55+
#[coverage(off)]
56+
fn say(message: &str) {
57+
println!("{message}");
58+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Function name: call_site_body::fn_with_call_site_body
2+
Raw bytes (19): 0x[01, 01, 00, 03, 01, 15, 05, 00, 06, 01, 01, 09, 00, 0c, 01, 00, 0d, 00, 14]
3+
Number of files: 1
4+
- file 0 => $DIR/call-site-body.rs
5+
Number of expressions: 0
6+
Number of file 0 mappings: 3
7+
- Code(Counter(0)) at (prev + 21, 5) to (start + 0, 6)
8+
- Code(Counter(0)) at (prev + 1, 9) to (start + 0, 12)
9+
- Code(Counter(0)) at (prev + 0, 13) to (start + 0, 20)
10+
Highest counter ID seen: c0
11+
12+
Function name: call_site_body::fn_with_call_site_inner (unused)
13+
Raw bytes (14): 0x[01, 01, 00, 02, 00, 1e, 09, 02, 0f, 00, 05, 09, 00, 0a]
14+
Number of files: 1
15+
- file 0 => $DIR/call-site-body.rs
16+
Number of expressions: 0
17+
Number of file 0 mappings: 2
18+
- Code(Zero) at (prev + 30, 9) to (start + 2, 15)
19+
- Code(Zero) at (prev + 5, 9) to (start + 0, 10)
20+
Highest counter ID seen: (none)
21+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
LL| |#![feature(coverage_attribute)]
2+
LL| |//@ edition: 2024
3+
LL| |
4+
LL| |// Snapshot test demonstrating how the function signature span and body span
5+
LL| |// affect coverage instrumentation in the presence of macro expansion.
6+
LL| |// This test captures current behaviour, which is not necessarily "correct".
7+
LL| |
8+
LL| |// This macro uses an argument token tree directly as a function body.
9+
LL| |#[rustfmt::skip]
10+
LL| |macro_rules! with_call_site_body {
11+
LL| | ($body:tt) => {
12+
LL| | fn
13+
LL| | fn_with_call_site_body
14+
LL| | ()
15+
LL| | $body
16+
LL| | }
17+
LL| |}
18+
LL| |
19+
LL| |with_call_site_body!(
20+
LL| | // (force line break)
21+
LL| 1| {
22+
LL| 1| say("hello");
23+
LL| | }
24+
LL| |);
25+
LL| |
26+
LL| |// This macro uses as an argument token tree as code within an explicit body.
27+
LL| |#[rustfmt::skip]
28+
LL| |macro_rules! with_call_site_inner {
29+
LL| | ($inner:tt) => {
30+
LL| 0| fn
31+
LL| 0| fn_with_call_site_inner
32+
LL| 0| ()
33+
LL| | {
34+
LL| | $inner
35+
LL| 0| }
36+
LL| | };
37+
LL| |}
38+
LL| |
39+
LL| |with_call_site_inner!(
40+
LL| | // (force line break)
41+
LL| | {
42+
LL| | say("hello");
43+
LL| | }
44+
LL| |);
45+
LL| |
46+
LL| |#[coverage(off)]
47+
LL| |fn main() {
48+
LL| | fn_with_call_site_body();
49+
LL| |}
50+
LL| |
51+
LL| |#[coverage(off)]
52+
LL| |fn say(message: &str) {
53+
LL| | println!("{message}");
54+
LL| |}
55+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#![feature(coverage_attribute)]
2+
//@ edition: 2024
3+
4+
// Snapshot test demonstrating how the function signature span and body span
5+
// affect coverage instrumentation in the presence of macro expansion.
6+
// This test captures current behaviour, which is not necessarily "correct".
7+
8+
// This macro uses an argument token tree directly as a function body.
9+
#[rustfmt::skip]
10+
macro_rules! with_call_site_body {
11+
($body:tt) => {
12+
fn
13+
fn_with_call_site_body
14+
()
15+
$body
16+
}
17+
}
18+
19+
with_call_site_body!(
20+
// (force line break)
21+
{
22+
say("hello");
23+
}
24+
);
25+
26+
// This macro uses as an argument token tree as code within an explicit body.
27+
#[rustfmt::skip]
28+
macro_rules! with_call_site_inner {
29+
($inner:tt) => {
30+
fn
31+
fn_with_call_site_inner
32+
()
33+
{
34+
$inner
35+
}
36+
};
37+
}
38+
39+
with_call_site_inner!(
40+
// (force line break)
41+
{
42+
say("hello");
43+
}
44+
);
45+
46+
#[coverage(off)]
47+
fn main() {
48+
fn_with_call_site_body();
49+
}
50+
51+
#[coverage(off)]
52+
fn say(message: &str) {
53+
println!("{message}");
54+
}

0 commit comments

Comments
 (0)