[clang] [clang][CodeCoverage] Fix CoverageMapping for binary conditionals ops (PR #82141)

David Tellenbach via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 18 14:29:58 PST 2024


https://github.com/dtellenbach updated https://github.com/llvm/llvm-project/pull/82141

>From f8e11fed8b4b6b0cc359e2915e4f2f32c3f08bb5 Mon Sep 17 00:00:00 2001
From: David Tellenbach <dtellenbach at apple.com>
Date: Sat, 17 Feb 2024 15:16:39 -0800
Subject: [PATCH 1/2] [clang][CodeCoverage] Fix CoverageMapping for binary
 conditionals ops

Fix an issue that produces a wrong coverage mapping when using binary
conditional operators as show in the example below.

Before this patch:

    1|      1|int binary_cond(int x) {
    2|      1|  x = x ?: 4;
    3|      1|  int y = 0;
    4|      0|  return x;       <-- Not covered
    5|      1|}

After this patch:

    1|      1|int binary_cond(int x) {
    2|      1|  x = x ?: 4;
    3|      1|  int y = 0;
    4|      1|  return x;       <-- Covered
    5|      1|}
---
 clang/lib/CodeGen/CoverageMappingGen.cpp      |  2 ++
 .../CoverageMapping/conditional-operator.c    | 25 +++++++++++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 clang/test/CoverageMapping/conditional-operator.c

diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp b/clang/lib/CodeGen/CoverageMappingGen.cpp
index c10d85ea89ee61..d8fa69d825b8d6 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -1942,6 +1942,8 @@ struct CounterCoverageMappingBuilder
 
       extendRegion(E->getTrueExpr());
       OutCount = propagateCounts(TrueCount, E->getTrueExpr());
+    } else {
+      OutCount = TrueCount;
     }
 
     extendRegion(E->getFalseExpr());
diff --git a/clang/test/CoverageMapping/conditional-operator.c b/clang/test/CoverageMapping/conditional-operator.c
new file mode 100644
index 00000000000000..5f3eb9c03e79fb
--- /dev/null
+++ b/clang/test/CoverageMapping/conditional-operator.c
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -mllvm -emptyline-comment-coverage=false -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only %s | FileCheck %s
+
+// CHECK-LABEL:       binary_conditional:
+// CHECK-NEXT:          File 0, [[@LINE+4]]:31 -> {{[0-9]+}}:2 = #0
+// CHECK-NEXT:          File 0, [[@LINE+4]]:7 -> [[@LINE+4]]:8 = #0
+// CHECK-NEXT:          Branch,File 0, [[@LINE+3]]:7 -> [[@LINE+3]]:8 = #1, (#0 - #1)
+// CHECK-NEXT:          File 0, [[@LINE+2]]:13 -> [[@LINE+2]]:14 = (#0 - #1)
+int binary_conditional(int x) {
+  x = x ? : 4;
+  int y = x;
+  return y;
+}
+
+// CHECK-LABEL:       tenary_conditional:
+// CHECK-NEXT:          File 0, [[@LINE+6]]:31 -> {{[0-9]+}}:2 = #0
+// CHECK-NEXT:          File 0, [[@LINE+6]]:7 -> [[@LINE+6]]:8 = #0
+// CHECK-NEXT:          Branch,File 0, [[@LINE+5]]:7 -> [[@LINE+5]]:8 = #1, (#0 - #1)
+// CHECK-NEXT:          Gap,File 0, [[@LINE+4]]:10 -> [[@LINE+4]]:11 = #1
+// CHECK-NEXT:          File 0, [[@LINE+3]]:11 -> [[@LINE+3]]:12 = #1
+// CHECK-NEXT:          File 0, [[@LINE+2]]:15 -> [[@LINE+2]]:16 = (#0 - #1)
+int tenary_conditional(int x) {
+  x = x ? x : 4;
+  int y = x;
+  return y;
+}

>From 28737890db53bd2cef7a6fad4b260e20e325f899 Mon Sep 17 00:00:00 2001
From: David Tellenbach <dtellenbach at apple.com>
Date: Sun, 18 Feb 2024 14:28:58 -0800
Subject: [PATCH 2/2] Fix typo in test

---
 clang/test/CoverageMapping/conditional-operator.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/CoverageMapping/conditional-operator.c b/clang/test/CoverageMapping/conditional-operator.c
index 5f3eb9c03e79fb..06b89c6b5a697e 100644
--- a/clang/test/CoverageMapping/conditional-operator.c
+++ b/clang/test/CoverageMapping/conditional-operator.c
@@ -11,14 +11,14 @@ int binary_conditional(int x) {
   return y;
 }
 
-// CHECK-LABEL:       tenary_conditional:
+// CHECK-LABEL:       ternary_conditional:
 // CHECK-NEXT:          File 0, [[@LINE+6]]:31 -> {{[0-9]+}}:2 = #0
 // CHECK-NEXT:          File 0, [[@LINE+6]]:7 -> [[@LINE+6]]:8 = #0
 // CHECK-NEXT:          Branch,File 0, [[@LINE+5]]:7 -> [[@LINE+5]]:8 = #1, (#0 - #1)
 // CHECK-NEXT:          Gap,File 0, [[@LINE+4]]:10 -> [[@LINE+4]]:11 = #1
 // CHECK-NEXT:          File 0, [[@LINE+3]]:11 -> [[@LINE+3]]:12 = #1
 // CHECK-NEXT:          File 0, [[@LINE+2]]:15 -> [[@LINE+2]]:16 = (#0 - #1)
-int tenary_conditional(int x) {
+int ternary_conditional(int x) {
   x = x ? x : 4;
   int y = x;
   return y;



More information about the cfe-commits mailing list