[cfe-commits] r100676 - in /cfe/trunk: lib/AST/Expr.cpp test/Sema/warn-unused-value.c

Ted Kremenek kremenek at apple.com
Wed Apr 7 11:49:21 PDT 2010


Author: kremenek
Date: Wed Apr  7 13:49:21 2010
New Revision: 100676

URL: http://llvm.org/viewvc/llvm-project?rev=100676&view=rev
Log:
Don't emit an 'unused expression' warning for '||' and '&&' expressions that contain assignments
or similar side-effects.

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=100676&r1=100675&r2=100676&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Wed Apr  7 13:49:21 2010
@@ -837,19 +837,22 @@
   }
   case BinaryOperatorClass: {
     const BinaryOperator *BO = cast<BinaryOperator>(this);
-    // Consider comma to have side effects if the LHS or RHS does.
-    if (BO->getOpcode() == BinaryOperator::Comma) {
-      // ((foo = <blah>), 0) is an idiom for hiding the result (and
-      // lvalue-ness) of an assignment written in a macro.
-      if (IntegerLiteral *IE =
-            dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
-        if (IE->getValue() == 0)
-          return false;
-
-      return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
-              BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
+    switch (BO->getOpcode()) {
+      default:
+        break;
+      // Consider ',', '||', '&&' to have side effects if the LHS or RHS does.
+      case BinaryOperator::Comma:
+        // ((foo = <blah>), 0) is an idiom for hiding the result (and
+        // lvalue-ness) of an assignment written in a macro.
+        if (IntegerLiteral *IE =
+              dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
+          if (IE->getValue() == 0)
+            return false;
+      case BinaryOperator::LAnd:
+      case BinaryOperator::LOr:
+        return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
+                BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
     }
-
     if (BO->isAssignmentOp())
       return false;
     Loc = BO->getOperatorLoc();

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=100676&r1=100675&r2=100676&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unused-value.c (original)
+++ cfe/trunk/test/Sema/warn-unused-value.c Wed Apr  7 13:49:21 2010
@@ -51,3 +51,16 @@
   *pi;              // expected-warning {{expression result unused}}
   *pj;
 }
+
+// Don't warn about unused '||', '&&' expressions that contain assignments.
+int test_logical_foo1();
+int test_logical_foo2();
+int test_logical_foo3();
+int test_logical_bar() {
+  int x = 0;
+  (x = test_logical_foo1()) ||  // no-warning
+  (x = test_logical_foo2()) ||  // no-warning
+  (x = test_logical_foo3());    // no-warning
+  return x;
+}
+





More information about the cfe-commits mailing list