Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

dtellenbach
Copy link
Member

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|}

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|}
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Feb 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2024

@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: David Tellenbach (dtellenbach)

Changes

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;       &lt;-- 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;       &lt;-- Covered
    5|      1|}

Full diff: https://github.com/llvm/llvm-project/pull/82141.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CoverageMappingGen.cpp (+2)
  • (added) clang/test/CoverageMapping/conditional-operator.c (+25)
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;
+}

Copy link
Contributor

@hanickadot hanickadot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typo in test. Otherwise LGTM

// 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: s/tenary/ternary/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks for catching that!

@dtellenbach dtellenbach merged commit dc94eb5 into llvm:main Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants