[cfe-commits] r77631 - in /cfe/trunk: lib/Sema/SemaStmt.cpp test/Sema/unused-expr.c

Anders Carlsson andersca at mac.com
Thu Jul 30 15:39:03 PDT 2009


Author: andersca
Date: Thu Jul 30 17:39:03 2009
New Revision: 77631

URL: http://llvm.org/viewvc/llvm-project?rev=77631&view=rev
Log:
Diagnose unused expression results for all statements, just not compound statements.

Modified:
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/Sema/unused-expr.c

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=77631&r1=77630&r2=77631&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Thu Jul 30 17:39:03 2009
@@ -52,7 +52,7 @@
 }
 
 void Sema::DiagnoseUnusedExprResult(const Stmt *S) {
-  const Expr *E = dyn_cast<Expr>(S);
+  const Expr *E = dyn_cast_or_null<Expr>(S);
   if (!E)
     return;
 
@@ -215,6 +215,7 @@
   }
 
   Stmt *thenStmt = ThenVal.takeAs<Stmt>();
+  DiagnoseUnusedExprResult(thenStmt);
 
   // Warn if the if block has a null body without an else value.
   // this helps prevent bugs due to typos, such as
@@ -225,9 +226,12 @@
       Diag(stmt->getSemiLoc(), diag::warn_empty_if_body);
   }
 
+  Stmt *elseStmt = ElseVal.takeAs<Stmt>();
+  DiagnoseUnusedExprResult(elseStmt);
+  
   CondResult.release();
   return Owned(new (Context) IfStmt(IfLoc, condExpr, thenStmt,
-                                    ElseLoc, ElseVal.takeAs<Stmt>()));
+                                    ElseLoc, elseStmt));
 }
 
 Action::OwningStmtResult
@@ -571,9 +575,11 @@
                        << condType << condExpr->getSourceRange());
   }
 
+  Stmt *bodyStmt = Body.takeAs<Stmt>();
+  DiagnoseUnusedExprResult(bodyStmt);
+  
   CondArg.release();
-  return Owned(new (Context) WhileStmt(condExpr, Body.takeAs<Stmt>(), 
-                                       WhileLoc));
+  return Owned(new (Context) WhileStmt(condExpr, bodyStmt, WhileLoc));
 }
 
 Action::OwningStmtResult
@@ -597,8 +603,11 @@
                        << condType << condExpr->getSourceRange());
   }
 
+  Stmt *bodyStmt = Body.takeAs<Stmt>();
+  DiagnoseUnusedExprResult(bodyStmt);
+
   Cond.release();
-  return Owned(new (Context) DoStmt(Body.takeAs<Stmt>(), condExpr, DoLoc,
+  return Owned(new (Context) DoStmt(bodyStmt, condExpr, DoLoc,
                                     WhileLoc, CondRParen));
 }
 
@@ -639,6 +648,9 @@
                             diag::err_typecheck_statement_requires_scalar)
         << SecondType << Second->getSourceRange());
   }
+  
+  DiagnoseUnusedExprResult(Body);
+
   first.release();
   second.release();
   third.release();

Modified: cfe/trunk/test/Sema/unused-expr.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/unused-expr.c?rev=77631&r1=77630&r2=77631&view=diff

==============================================================================
--- cfe/trunk/test/Sema/unused-expr.c (original)
+++ cfe/trunk/test/Sema/unused-expr.c Thu Jul 30 17:39:03 2009
@@ -50,4 +50,23 @@
   ((void)0), y = x;
 }
 
+void t4(int a) {
+  int b = 0;
+
+  if (a)
+    b == 1; // expected-warning{{expression result unused}}
+  else
+    b == 2; // expected-warning{{expression result unused}}
+    
+  while (1)
+    b == 3; // expected-warning{{expression result unused}}
+
+  do
+    b == 4; // expected-warning{{expression result unused}}
+  while (1);
+  
+  for (;;)
+    b == 5; // expected-warning{{expression result unused}}
+}
+
 





More information about the cfe-commits mailing list