r359844 - Fix -Wunsequenced false-positives in code controlled by a branch on

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Thu May 2 16:21:29 PDT 2019


Author: rsmith
Date: Thu May  2 16:21:28 2019
New Revision: 359844

URL: http://llvm.org/viewvc/llvm-project?rev=359844&view=rev
Log:
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.

Modified:
    cfe/trunk/lib/AST/ExprConstant.cpp
    cfe/trunk/test/Sema/warn-unsequenced.c

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=359844&r1=359843&r2=359844&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Thu May  2 16:21:28 2019
@@ -8269,11 +8269,14 @@ bool IntExprEvaluator::VisitBuiltinCallE
 
   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;
     }

Modified: cfe/trunk/test/Sema/warn-unsequenced.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unsequenced.c?rev=359844&r1=359843&r2=359844&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unsequenced.c (original)
+++ cfe/trunk/test/Sema/warn-unsequenced.c Thu May  2 16:21:28 2019
@@ -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;
 }




More information about the cfe-commits mailing list