about summary refs log tree commit diff
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2024-04-19 15:35:23 +1000
committerZalathar <Zalathar@users.noreply.github.com>2024-04-22 21:55:33 +1000
commita892c2387e64ba080a6503d422931f3a0916826a (patch)
tree2c6e3a5fa40627f66ecf28b743b5cc0c7d9668c5
parentda37b14121d0fed6e49a4ed27abd2b9e7fc1c486 (diff)
coverage: Add a mir-opt test for branch coverage of match arms
-rw-r--r--tests/mir-opt/coverage/branch_match_arms.main.InstrumentCoverage.diff138
-rw-r--r--tests/mir-opt/coverage/branch_match_arms.rs27
2 files changed, 165 insertions, 0 deletions
diff --git a/tests/mir-opt/coverage/branch_match_arms.main.InstrumentCoverage.diff b/tests/mir-opt/coverage/branch_match_arms.main.InstrumentCoverage.diff
new file mode 100644
index 00000000000..e60f71f47b1
--- /dev/null
+++ b/tests/mir-opt/coverage/branch_match_arms.main.InstrumentCoverage.diff
@@ -0,0 +1,138 @@
+- // MIR for `main` before InstrumentCoverage
++ // MIR for `main` after InstrumentCoverage
+  
+  fn main() -> () {
+      let mut _0: ();
+      let mut _1: Enum;
+      let mut _2: isize;
+      let _3: u32;
+      let mut _4: u32;
+      let _5: u32;
+      let mut _6: u32;
+      let _7: u32;
+      let mut _8: u32;
+      let _9: u32;
+      let mut _10: u32;
+      scope 1 {
+          debug d => _3;
+      }
+      scope 2 {
+          debug c => _5;
+      }
+      scope 3 {
+          debug b => _7;
+      }
+      scope 4 {
+          debug a => _9;
+      }
+  
++     coverage ExpressionId(0) => Expression { lhs: Counter(1), op: Add, rhs: Counter(2) };
++     coverage ExpressionId(1) => Expression { lhs: Expression(0), op: Add, rhs: Counter(3) };
++     coverage ExpressionId(2) => Expression { lhs: Counter(0), op: Subtract, rhs: Expression(1) };
++     coverage ExpressionId(3) => Expression { lhs: Counter(3), op: Add, rhs: Counter(2) };
++     coverage ExpressionId(4) => Expression { lhs: Expression(3), op: Add, rhs: Counter(1) };
++     coverage ExpressionId(5) => Expression { lhs: Expression(4), op: Add, rhs: Expression(2) };
++     coverage Code(Counter(0)) => $DIR/branch_match_arms.rs:14:1 - 15:21;
++     coverage Code(Counter(3)) => $DIR/branch_match_arms.rs:16:17 - 16:33;
++     coverage Code(Counter(2)) => $DIR/branch_match_arms.rs:17:17 - 17:33;
++     coverage Code(Counter(1)) => $DIR/branch_match_arms.rs:18:17 - 18:33;
++     coverage Code(Expression(2)) => $DIR/branch_match_arms.rs:19:17 - 19:33;
++     coverage Code(Expression(5)) => $DIR/branch_match_arms.rs:21:1 - 21:2;
++ 
+      bb0: {
++         Coverage::CounterIncrement(0);
+          StorageLive(_1);
+          _1 = Enum::A(const 0_u32);
+          PlaceMention(_1);
+          _2 = discriminant(_1);
+          switchInt(move _2) -> [0: bb5, 1: bb4, 2: bb3, 3: bb2, otherwise: bb1];
+      }
+  
+      bb1: {
+          FakeRead(ForMatchedPlace(None), _1);
+          unreachable;
+      }
+  
+      bb2: {
++         Coverage::CounterIncrement(3);
+          falseEdge -> [real: bb6, imaginary: bb3];
+      }
+  
+      bb3: {
++         Coverage::CounterIncrement(2);
+          falseEdge -> [real: bb8, imaginary: bb4];
+      }
+  
+      bb4: {
++         Coverage::CounterIncrement(1);
+          falseEdge -> [real: bb10, imaginary: bb5];
+      }
+  
+      bb5: {
++         Coverage::ExpressionUsed(2);
+          StorageLive(_9);
+          _9 = ((_1 as A).0: u32);
+          StorageLive(_10);
+          _10 = _9;
+          _0 = consume(move _10) -> [return: bb12, unwind: bb14];
+      }
+  
+      bb6: {
+          StorageLive(_3);
+          _3 = ((_1 as D).0: u32);
+          StorageLive(_4);
+          _4 = _3;
+          _0 = consume(move _4) -> [return: bb7, unwind: bb14];
+      }
+  
+      bb7: {
+          StorageDead(_4);
+          StorageDead(_3);
+          goto -> bb13;
+      }
+  
+      bb8: {
+          StorageLive(_5);
+          _5 = ((_1 as C).0: u32);
+          StorageLive(_6);
+          _6 = _5;
+          _0 = consume(move _6) -> [return: bb9, unwind: bb14];
+      }
+  
+      bb9: {
+          StorageDead(_6);
+          StorageDead(_5);
+          goto -> bb13;
+      }
+  
+      bb10: {
+          StorageLive(_7);
+          _7 = ((_1 as B).0: u32);
+          StorageLive(_8);
+          _8 = _7;
+          _0 = consume(move _8) -> [return: bb11, unwind: bb14];
+      }
+  
+      bb11: {
+          StorageDead(_8);
+          StorageDead(_7);
+          goto -> bb13;
+      }
+  
+      bb12: {
+          StorageDead(_10);
+          StorageDead(_9);
+          goto -> bb13;
+      }
+  
+      bb13: {
++         Coverage::ExpressionUsed(5);
+          StorageDead(_1);
+          return;
+      }
+  
+      bb14 (cleanup): {
+          resume;
+      }
+  }
+  
diff --git a/tests/mir-opt/coverage/branch_match_arms.rs b/tests/mir-opt/coverage/branch_match_arms.rs
new file mode 100644
index 00000000000..18764b38d6e
--- /dev/null
+++ b/tests/mir-opt/coverage/branch_match_arms.rs
@@ -0,0 +1,27 @@
+#![feature(coverage_attribute)]
+//@ test-mir-pass: InstrumentCoverage
+//@ compile-flags: -Cinstrument-coverage -Zno-profiler-runtime -Zcoverage-options=branch
+// skip-filecheck
+
+enum Enum {
+    A(u32),
+    B(u32),
+    C(u32),
+    D(u32),
+}
+
+// EMIT_MIR branch_match_arms.main.InstrumentCoverage.diff
+fn main() {
+    match Enum::A(0) {
+        Enum::D(d) => consume(d),
+        Enum::C(c) => consume(c),
+        Enum::B(b) => consume(b),
+        Enum::A(a) => consume(a),
+    }
+}
+
+#[inline(never)]
+#[coverage(off)]
+fn consume(x: u32) {
+    core::hint::black_box(x);
+}