diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2019-05-02 23:21:28 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2019-05-02 23:21:28 +0000 |
commit | b4f79aa4368802682268e2f39a29dd8f9b107b59 (patch) | |
tree | 9babb15d5a976bb4b89dbd95c276663c44770a97 | |
parent | 2067b5bc0787859213f603c8e37d0d2182df48ff (diff) |
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
-rw-r--r-- | lib/AST/ExprConstant.cpp | 9 | ||||
-rw-r--r-- | test/Sema/warn-unsequenced.c | 2 |
2 files changed, 8 insertions, 3 deletions
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; } |