[cfe-commits] r41551 - /cfe/trunk/Sema/SemaStmt.cpp

Chris Lattner sabre at nondot.org
Tue Aug 28 11:40:31 PDT 2007


Author: lattner
Date: Tue Aug 28 13:40:30 2007
New Revision: 41551

URL: http://llvm.org/viewvc/llvm-project?rev=41551&view=rev
Log:
Emit a slightly better warning for unused values for unary and binary operators:

t2.c:3:18: warning: expression result unused
     x = ++x, ++y, y+2;
     ~~~~~~~~~~~~^ ~~~


Modified:
    cfe/trunk/Sema/SemaStmt.cpp

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

==============================================================================
--- cfe/trunk/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/Sema/SemaStmt.cpp Tue Aug 28 13:40:30 2007
@@ -21,6 +21,20 @@
 #include "clang/Lex/IdentifierTable.h"
 using namespace clang;
 
+/// DiagnoseDeadExpr - The specified expression is side-effect free and
+/// evaluated in a context where the result is unused.  Emit a diagnostic to
+/// warn about this if appropriate.
+static void DiagnoseDeadExpr(Expr *E, Sema &S) {
+  if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
+    S.Diag(BO->getOperatorLoc(), diag::warn_unused_expr,
+           BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange());
+  else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
+    S.Diag(UO->getOperatorLoc(), diag::warn_unused_expr,
+           UO->getSubExpr()->getSourceRange());
+  else 
+    S.Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
+}
+
 Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) {
   Expr *E = static_cast<Expr*>(expr);
   assert(E && "ParseExprStmt(): missing expression");
@@ -28,7 +42,7 @@
   // Exprs are statements, so there is no need to do a conversion here. However,
   // diagnose some potentially bad code.
   if (!E->hasLocalSideEffect() && !E->getType()->isVoidType())
-    Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange());
+    DiagnoseDeadExpr(E, *this);
   
   return E;
 }





More information about the cfe-commits mailing list