[cfe-commits] r98335 - in /cfe/trunk: lib/AST/Expr.cpp lib/Sema/SemaStmt.cpp test/Sema/return.c test/Sema/warn-unused-value.c test/SemaCXX/decl-expr-ambiguity.cpp test/SemaCXX/warn-unused-value.cpp

John McCall rjmccall at apple.com
Thu Mar 11 23:11:26 PST 2010


Author: rjmccall
Date: Fri Mar 12 01:11:26 2010
New Revision: 98335

URL: http://llvm.org/viewvc/llvm-project?rev=98335&view=rev
Log:
Improve the unused-value check to look into comma expressions and filter out
voids in sub-expressions.  Patch by Mike M!

Fixes PR4806.


Added:
    cfe/trunk/test/Sema/warn-unused-value.c
    cfe/trunk/test/SemaCXX/warn-unused-value.cpp
Modified:
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/Sema/return.c
    cfe/trunk/test/SemaCXX/decl-expr-ambiguity.cpp

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=98335&r1=98334&r2=98335&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Fri Mar 12 01:11:26 2010
@@ -788,6 +788,8 @@
 
   switch (getStmtClass()) {
   default:
+    if (getType()->isVoidType())
+      return false;
     Loc = getExprLoc();
     R1 = getSourceRange();
     return true;
@@ -834,8 +836,8 @@
         if (IE->getValue() == 0)
           return false;
 
-      return (BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
-              BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
+      return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
+              BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
     }
 
     if (BO->isAssignmentOp())
@@ -936,6 +938,8 @@
       if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
         return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
 
+    if (getType()->isVoidType())
+      return false;
     Loc = cast<StmtExpr>(this)->getLParenLoc();
     R1 = getSourceRange();
     return true;
@@ -949,6 +953,8 @@
     R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
     return true;
   case CXXFunctionalCastExprClass: {
+    if (getType()->isVoidType())
+      return false;
     const CastExpr *CE = cast<CastExpr>(this);
     
     // If this is a cast to void or a constructor conversion, check the operand.

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=98335&r1=98334&r2=98335&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Fri Mar 12 01:11:26 2010
@@ -76,10 +76,6 @@
   if (!E)
     return;
 
-  // Ignore expressions that have void type.
-  if (E->getType()->isVoidType())
-    return;
-
   SourceLocation Loc;
   SourceRange R1, R2;
   if (!E->isUnusedResultAWarning(Loc, R1, R2, Context))
@@ -103,6 +99,9 @@
   }
       
   if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
+    if (E->getType()->isVoidType())
+      return;
+
     // If the callee has attribute pure, const, or warn_unused_result, warn with
     // a more specific message to make it clear what is happening.
     if (const Decl *FD = CE->getCalleeDecl()) {

Modified: cfe/trunk/test/Sema/return.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/return.c?rev=98335&r1=98334&r2=98335&view=diff
==============================================================================
--- cfe/trunk/test/Sema/return.c (original)
+++ cfe/trunk/test/Sema/return.c Fri Mar 12 01:11:26 2010
@@ -1,4 +1,4 @@
-// RUN: %clang %s -fsyntax-only -Xclang -verify -fblocks -Wno-unreachable-code
+// RUN: %clang %s -fsyntax-only -Xclang -verify -fblocks -Wno-unreachable-code -Wno-unused-value
 
 // clang emits the following warning by default.
 // With GCC, -pedantic, -Wreturn-type or -Wall are required to produce the 

Added: cfe/trunk/test/Sema/warn-unused-value.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unused-value.c?rev=98335&view=auto
==============================================================================
--- cfe/trunk/test/Sema/warn-unused-value.c (added)
+++ cfe/trunk/test/Sema/warn-unused-value.c Fri Mar 12 01:11:26 2010
@@ -0,0 +1,53 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value %s
+
+int i = 0;
+int j = 0;
+
+void foo();
+
+// PR4806
+void pr4806() {
+  1,foo();          // expected-warning {{expression result unused}}
+
+  // other
+  foo();
+  i;                // expected-warning {{expression result unused}}
+
+  i,foo();          // expected-warning {{expression result unused}}
+  foo(),i;          // expected-warning {{expression result unused}}
+
+  i,j,foo();        // expected-warning {{expression result unused}}
+  i,foo(),j;        // expected-warning {{expression result unused}}
+  foo(),i,j;        // expected-warning {{expression result unused}}
+
+  i++;
+
+  i++,foo();
+  foo(),i++;
+
+  i++,j,foo();      // expected-warning {{expression result unused}}
+  i++,foo(),j;      // expected-warning {{expression result unused}}
+  foo(),i++,j;      // expected-warning {{expression result unused}}
+
+  i,j++,foo();      // expected-warning {{expression result unused}}
+  i,foo(),j++;      // expected-warning {{expression result unused}}
+  foo(),i,j++;      // expected-warning {{expression result unused}}
+
+  i++,j++,foo();
+  i++,foo(),j++;
+  foo(),i++,j++;
+
+  {};
+  ({});
+  ({}),foo();
+  foo(),({});
+
+  (int)1U;          // expected-warning {{expression result unused}}
+  (void)1U;
+
+  // pointer to volatile has side effect (thus no warning)
+  int* pi = &i;
+  volatile int* pj = &j;
+  *pi;              // expected-warning {{expression result unused}}
+  *pj;
+}

Modified: cfe/trunk/test/SemaCXX/decl-expr-ambiguity.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/decl-expr-ambiguity.cpp?rev=98335&r1=98334&r2=98335&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/decl-expr-ambiguity.cpp (original)
+++ cfe/trunk/test/SemaCXX/decl-expr-ambiguity.cpp Fri Mar 12 01:11:26 2010
@@ -10,7 +10,7 @@
   int(a)++; // expected-error {{expression is not assignable}}
   __extension__ int(a)++; // expected-error {{expression is not assignable}}
   __typeof(int)(a,5)<<a; // expected-error {{function-style cast to a builtin type can only take one argument}}
-  void(a), ++a; // expected-warning {{expression result unused}}
+  void(a), ++a;
   if (int(a)+1) {}
   for (int(a)+1;;) {} // expected-warning {{expression result unused}}
   a = sizeof(int()+1);

Added: cfe/trunk/test/SemaCXX/warn-unused-value.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-value.cpp?rev=98335&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unused-value.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-unused-value.cpp Fri Mar 12 01:11:26 2010
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wunused-value %s
+
+// PR4806
+namespace test0 {
+  class Box {
+  public:
+    int i;
+    volatile int j;
+  };
+
+  void doit() {
+    // pointer to volatile has side effect (thus no warning)
+    Box* box = new Box;
+    box->i; // expected-warning {{expression result unused}}
+    box->j;
+  }
+}





More information about the cfe-commits mailing list