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

Fixes #124565
This commit is contained in:
NAKAMURA Takumi 2026-01-13 22:44:30 +09:00 committed by GitHub
parent bf5975e2ba
commit 9780d421ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 3 deletions

View File

@ -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();

View File

@ -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;

View File

@ -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)