]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gcov-profile/114715 - missing coverage for switch
authorRichard Biener <rguenther@suse.de>
Mon, 15 Apr 2024 09:09:17 +0000 (11:09 +0200)
committerRichard Biener <rguenther@suse.de>
Mon, 15 Apr 2024 11:35:15 +0000 (13:35 +0200)
The following avoids missing coverage for the line of a switch statement
which happens when gimplification emits a BIND_EXPR wrapping the switch
as that prevents us from setting locations on the containing statements
via annotate_all_with_location.  Instead set the location of the GIMPLE
switch directly.

PR gcov-profile/114715
* gimplify.cc (gimplify_switch_expr): Set the location of the
GIMPLE switch.

* gcc.misc-tests/gcov-24.c: New testcase.

gcc/gimplify.cc
gcc/testsuite/gcc.misc-tests/gcov-24.c [new file with mode: 0644]

index 3b731525f15c96772c83b0555c3c60df87a536bf..457b33a4293f7d10c2f4213bbb6dfe640200843a 100644 (file)
@@ -3013,6 +3013,7 @@ gimplify_switch_expr (tree *expr_p, gimple_seq *pre_p)
 
       switch_stmt = gimple_build_switch (SWITCH_COND (switch_expr),
                                         default_case, labels);
+      gimple_set_location (switch_stmt, EXPR_LOCATION (switch_expr));
       /* For the benefit of -Wimplicit-fallthrough, if switch_body_seq
         ends with a GIMPLE_LABEL holding SWITCH_BREAK_LABEL_P LABEL_DECL,
         wrap the GIMPLE_SWITCH up to that GIMPLE_LABEL into a GIMPLE_BIND,
diff --git a/gcc/testsuite/gcc.misc-tests/gcov-24.c b/gcc/testsuite/gcc.misc-tests/gcov-24.c
new file mode 100644 (file)
index 0000000..395099b
--- /dev/null
@@ -0,0 +1,30 @@
+/* { dg-options "-fprofile-arcs -ftest-coverage" } */
+/* { dg-do run { target native } } */
+
+int main()
+{
+  int a = 1;
+  int b = 2;
+  int c = -3;
+  switch(a) /* count(1) */
+    {
+    case 1: /* count(1) */
+    c = 3;
+    switch(b) { /* count(1) */
+      case 1: /* count(#####) */
+      c = 4;
+      break;
+      case 2: /* count(1) */
+      c = 5;
+      break;
+    }
+    break;
+    case 2: /* count(#####) */
+    c = 6;
+    break;
+    default: /* count(#####) */
+    break;
+    }
+}
+
+/* { dg-final { run-gcov gcov-24.c } } */