From: Richard Smith Date: Thu, 2 May 2019 23:21:28 +0000 (+0000) Subject: Fix -Wunsequenced false-positives in code controlled by a branch on X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b4f79aa4368802682268e2f39a29dd8f9b107b59;p=clang Fix -Wunsequenced false-positives in code controlled by a branch on __builtin_constant_p. If the operand of __builtin_constant_p is not constant and has side-effects, then code controlled by a branch on it is unreachable and we should not emit runtime behavior warnings in such code. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@359844 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index 24b28971af..8386ce8f2d 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -8269,11 +8269,14 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, case Builtin::BI__builtin_constant_p: { const Expr *Arg = E->getArg(0); - if (EvaluateBuiltinConstantP(Info, Arg)) + if (EvaluateBuiltinConstantP(Info, Arg)) { return Success(true, E); - else if (Info.InConstantContext) + } else if (Info.InConstantContext || Arg->HasSideEffects(Info.Ctx)) { + // Outside a constant context, eagerly evaluate to false in the presence + // of side-effects in order to avoid -Wunsequenced false-positives in + // a branch on __builtin_constant_p(expr). return Success(false, E); - else { + } else { Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); return false; } diff --git a/test/Sema/warn-unsequenced.c b/test/Sema/warn-unsequenced.c index 70163dc0de..247a121941 100644 --- a/test/Sema/warn-unsequenced.c +++ b/test/Sema/warn-unsequenced.c @@ -93,4 +93,6 @@ void test() { _Generic(++a, default: 0) + ++a; // ok sizeof(++a) + ++a; // ok _Alignof(++a) + ++a; // expected-warning {{extension}} + + __builtin_constant_p(f(++a, 0)) ? f(f(++a, 0), f(++a, 0)) : 0; }