diff options
| author | Chris Lattner <sabre@nondot.org> | 2007-08-28 18:40:30 +0000 |
|---|---|---|
| committer | Chris Lattner <sabre@nondot.org> | 2007-08-28 18:40:30 +0000 |
| commit | 24b51ff7525952057b078b0642e4dd50b37df71f (patch) | |
| tree | 3a691886257f0b33ffe826e207640bbc9df92d5a /clang/Sema/SemaStmt.cpp | |
| parent | c11005faa103f660ec32ec0d6348a562b8a61b62 (diff) | |
| download | bcm5719-llvm-24b51ff7525952057b078b0642e4dd50b37df71f.tar.gz bcm5719-llvm-24b51ff7525952057b078b0642e4dd50b37df71f.zip | |
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;
~~~~~~~~~~~~^ ~~~
llvm-svn: 41551
Diffstat (limited to 'clang/Sema/SemaStmt.cpp')
| -rw-r--r-- | clang/Sema/SemaStmt.cpp | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/clang/Sema/SemaStmt.cpp b/clang/Sema/SemaStmt.cpp index 6247215f0ed..300388cf33a 100644 --- a/clang/Sema/SemaStmt.cpp +++ b/clang/Sema/SemaStmt.cpp @@ -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 @@ Sema::StmtResult Sema::ParseExprStmt(ExprTy *expr) { // 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; } |

