]> git.lizzy.rs Git - rust.git/blob - src/test/codegen/coverage-experiments/src/if.rs
ad50f6be190040b503aef689bc0ff43203ad2e40
[rust.git] / src / test / codegen / coverage-experiments / src / if.rs
1 #![feature(core_intrinsics)]
2
3 pub fn __llvm_incr_counter(_region_loc: &str) {
4 }
5
6 #[inline(always)]
7 pub fn __incr_cov<T>(region_loc: &str, result: T) -> T {
8     __llvm_incr_counter(region_loc);
9     result
10 }
11
12 static TEST_FUNC_NAME: &'static [u8; 6] = b"main()";
13
14 fn main() {
15     let mut countdown = 10;
16     if __incr_cov("start", countdown > 0) {
17
18
19         // // TEST CALLING INTRINSIC:
20         unsafe { core::intrinsics::instrprof_increment(TEST_FUNC_NAME as *const u8, 1234 as u64, 314 as u32, 31 as u32) };
21         // // Results in:
22         // //   LLVM ERROR: Cannot select: intrinsic %llvm.instrprof.increment
23         // // I may need to pass one or more of the following flags (or equivalent opts) to LLVM to enable this:
24         // //   -fprofile-instr-generate -fcoverage-mapping
25
26
27         countdown -= 1;
28         __incr_cov("if block",());
29     } else if countdown > 5 {
30         countdown -= 2;
31         __incr_cov("else if block",());
32     } else {
33         countdown -= 3;
34     }
35
36     let mut countdown = 10;
37     if { let _tcov = countdown > 0; __llvm_incr_counter("start", ); _tcov } {
38         countdown -= 1;
39         __incr_cov("if block",());
40     } else if countdown > 5 {
41         countdown -= 2;
42         __incr_cov("else if block",());
43     } else {
44         countdown -= 3;
45     }
46 }
47
48 // NOTE: hir REDUNDANTLY lowers the manually inlined counter in the second if block to:
49 //
50 // match {
51 //   let _t =
52 //       {
53 //           let _tcov = countdown > 0;
54 //           __llvm_incr_counter("start");
55 //           _tcov
56 //       };
57 //   _t
58 // } {
59
60 // I don't know if optimization phases will fix this or not.
61 // Otherwise, a more optimal (but definitely special case) way to handle this would be
62 // to inject the counter between the hir-introduced temp `_t` assignment and the block result
63 // line returning `_t`:
64 //
65 // match {
66 //   let _t = countdown > 0;
67 //   __llvm_incr_counter("start"); // <-- the only thing inserted for coverage here
68 //   _t
69 // }
70 //
71 // UNFORTUNATELY THIS IS NOT A PATTERN WE CAN ALWAYS LEVERAGE, FOR EXPRESSIONS THAT HAVE VALUES
72 // WHERE WE NEED TO INJECT THE COUNTER AFTER THE EXPRESSION BUT BEFORE IT IS USED.
73 //
74 // IT DOES APPEAR TO BE THE CASE FOR WHILE EXPRESSIONS, (BECOMES loop { match { let _t = condition; _t} { true => {...} _ => break, }})
75 // AND IS TRUE FOR IF EXPRESSIONS AS NOTED
76 // BUT NOT FOR RETURN STATEMENT (and I'm guessing not for loop { break value; } ? )
77 //
78 // AND NOT FOR LAZY BOOLEAN EXPRESSIONS!
79 //
80 // AND NOT FOR MATCH EXPRESSIONS IN THE ORIGINAL SOURCE!