diff options
author | Chris Lattner <sabre@nondot.org> | 2007-08-31 21:49:55 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2007-08-31 21:49:55 +0000 |
commit | 98414c1b7d1944a57156d52e29bd41c005de09ac (patch) | |
tree | 47fbed1c7e597bc7a962919e8e1fa44bff614ba3 /Sema/SemaStmt.cpp | |
parent | 805e9a8300af9489ec13cd804c070267b7c4cfec (diff) |
Fix a bug/missing-feature Ted noticed: the 'unused' warning should not
warn about the last stmt in a stmtexpr, f.e. there should be no warning for:
int maxval_stmt_expr(int x, int y) {
return ({int _a = x, _b = y; _a > _b ? _a : _b; });
}
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41655 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'Sema/SemaStmt.cpp')
-rw-r--r-- | Sema/SemaStmt.cpp | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/Sema/SemaStmt.cpp b/Sema/SemaStmt.cpp index 300388cf33..bec7f892df 100644 --- a/Sema/SemaStmt.cpp +++ b/Sema/SemaStmt.cpp @@ -21,29 +21,9 @@ #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"); - - // 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()) - DiagnoseDeadExpr(E, *this); - return E; } @@ -61,7 +41,7 @@ Sema::StmtResult Sema::ParseDeclStmt(DeclTy *decl) { Action::StmtResult Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R, - StmtTy **elts, unsigned NumElts) { + StmtTy **elts, unsigned NumElts, bool isStmtExpr) { Stmt **Elts = reinterpret_cast<Stmt**>(elts); // If we're in C89 mode, check that we don't have any decls after stmts. If // so, emit an extension diagnostic. @@ -82,6 +62,32 @@ Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R, } } + // Warn about unused expressions in statements. + for (unsigned i = 0; i != NumElts; ++i) { + Expr *E = dyn_cast<Expr>(Elts[i]); + if (!E) continue; + + // Warn about expressions with unused results. + if (E->hasLocalSideEffect() || E->getType()->isVoidType()) + continue; + + // The last expr in a stmt expr really is used. + if (isStmtExpr && i == NumElts-1) + continue; + + /// DiagnoseDeadExpr - This expression is side-effect free and evaluated in + /// a context where the result is unused. Emit a diagnostic to warn about + /// this. + if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) + Diag(BO->getOperatorLoc(), diag::warn_unused_expr, + BO->getLHS()->getSourceRange(), BO->getRHS()->getSourceRange()); + else if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) + Diag(UO->getOperatorLoc(), diag::warn_unused_expr, + UO->getSubExpr()->getSourceRange()); + else + Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange()); + } + return new CompoundStmt(Elts, NumElts); } |