[cfe-commits] r126779 - in /cfe/trunk: lib/AST/Expr.cpp test/Sema/warn-unused-value.c
Ted Kremenek
kremenek at apple.com
Tue Mar 1 12:34:48 PST 2011
Author: kremenek
Date: Tue Mar 1 14:34:48 2011
New Revision: 126779
URL: http://llvm.org/viewvc/llvm-project?rev=126779&view=rev
Log:
Don't warn about unused values in ternary ?: expressions unless both the LHS and RHS are "unused" (side-effect free).
Patch by Justin Bogner! Fixes PR 8282.
Modified:
cfe/trunk/lib/AST/Expr.cpp
cfe/trunk/test/Sema/warn-unused-value.c
Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=126779&r1=126778&r2=126779&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Tue Mar 1 14:34:48 2011
@@ -1404,13 +1404,15 @@
return false;
case ConditionalOperatorClass: {
- // The condition must be evaluated, but if either the LHS or RHS is a
- // warning, warn about them.
+ // If only one of the LHS or RHS is a warning, the operator might
+ // be being used for control flow. Only warn if both the LHS and
+ // RHS are warnings.
const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
- if (Exp->getLHS() &&
- Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
+ if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
+ return false;
+ if (!Exp->getLHS())
return true;
- return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
+ return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
}
case MemberExprClass:
Modified: cfe/trunk/test/Sema/warn-unused-value.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unused-value.c?rev=126779&r1=126778&r2=126779&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unused-value.c (original)
+++ cfe/trunk/test/Sema/warn-unused-value.c Tue Mar 1 14:34:48 2011
@@ -72,6 +72,15 @@
return x;
}
+// PR8282
+void conditional_for_control_flow(int cond, int x, int y)
+{
+ cond? y++ : x; // no-warning
+ cond? y : ++x; // no-warning
+ cond? (x |= y) : ++x; // no-warning
+ cond? y : x; // expected-warning {{expression result unused}}
+}
+
struct s0 { int f0; };
void f0(int a);
More information about the cfe-commits
mailing list