Author: NAKAMURA Takumi
Date: 2026-01-13T22:44:30+09:00
New Revision: 9780d421ce5439ca2e9aa9cb3a29866070d20314

URL: 
https://github.com/llvm/llvm-project/commit/9780d421ce5439ca2e9aa9cb3a29866070d20314
DIFF: 
https://github.com/llvm/llvm-project/commit/9780d421ce5439ca2e9aa9cb3a29866070d20314.diff

LOG: [MC/DC] Handle __builtin_expect as if parenthses (#125405)

Fixes #124565

Added: 
    

Modified: 
    clang/include/clang/AST/IgnoreExpr.h
    clang/lib/CodeGen/CodeGenFunction.cpp
    clang/test/CoverageMapping/mcdc-nested-expr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/IgnoreExpr.h 
b/clang/include/clang/AST/IgnoreExpr.h
index ce88b9ac67ef0..3b50b8115c6b8 100644
--- a/clang/include/clang/AST/IgnoreExpr.h
+++ b/clang/include/clang/AST/IgnoreExpr.h
@@ -132,6 +132,15 @@ inline Expr *IgnoreUOpLNotSingleStep(Expr *E) {
   return E;
 }
 
+inline Expr *IgnoreBuiltinExpectSingleStep(Expr *E) {
+  if (auto *CE = dyn_cast<CallExpr>(E)) {
+    if (const FunctionDecl *FD = CE->getDirectCallee())
+      if (FD->getBuiltinID() == Builtin::BI__builtin_expect)
+        return CE->getArg(0);
+  }
+  return E;
+}
+
 inline Expr *IgnoreImplicitAsWrittenSingleStep(Expr *E) {
   if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
     return ICE->getSubExprAsWritten();

diff  --git a/clang/lib/CodeGen/CodeGenFunction.cpp 
b/clang/lib/CodeGen/CodeGenFunction.cpp
index 10f2ff0184c1c..bec359eb2b6b3 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -1791,8 +1791,9 @@ bool CodeGenFunction::ConstantFoldsToSimpleInteger(const 
Expr *Cond,
 /// Strip parentheses and simplistic logical-NOT operators.
 const Expr *CodeGenFunction::stripCond(const Expr *C) {
   while (true) {
-    const Expr *SC =
-        IgnoreExprNodes(C, IgnoreParensSingleStep, IgnoreUOpLNotSingleStep);
+    const Expr *SC = IgnoreExprNodes(
+        C, IgnoreParensSingleStep, IgnoreUOpLNotSingleStep,
+        IgnoreBuiltinExpectSingleStep, IgnoreImplicitCastsSingleStep);
     if (C == SC)
       return SC;
     C = SC;

diff  --git a/clang/test/CoverageMapping/mcdc-nested-expr.cpp 
b/clang/test/CoverageMapping/mcdc-nested-expr.cpp
index a49dad0b33612..f393919a91892 100644
--- a/clang/test/CoverageMapping/mcdc-nested-expr.cpp
+++ b/clang/test/CoverageMapping/mcdc-nested-expr.cpp
@@ -21,8 +21,11 @@ bool func_condop(bool a, bool b, bool c) {
 // Treated as parentheses.
 // CHECK: func_expect{{.*}}:
 bool func_expect(bool a, bool b, bool c) {
-  // WARN: :[[@LINE+1]]:10: warning: unsupported MC/DC boolean expression; 
contains an operation with a nested boolean expression.
   return a || __builtin_expect(b && c, true);
+  // CHECK:  Decision,File 0, [[@LINE-1]]:10 -> [[#L:@LINE-1]]:45 = M:4, C:3
+  // CHECK:  Branch,File 0, [[#L]]:10 -> [[#L]]:11 = (#0 - #1), #1 [1,0,2]
+  // CHECK:  Branch,File 0, [[#L]]:32 -> [[#L]]:33 = #2, (#1 - #2) [2,3,0]
+  // CHECK:  Branch,File 0, [[#L]]:37 -> [[#L]]:38 = #3, (#2 - #3) [3,0,0]
 }
 
 // LNot among BinOp(s)


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to