[cfe-commits] r77628 - in /cfe/trunk/lib/Sema: Sema.h SemaStmt.cpp
Anders Carlsson
andersca at mac.com
Thu Jul 30 15:17:18 PDT 2009
Author: andersca
Date: Thu Jul 30 17:17:18 2009
New Revision: 77628
URL: http://llvm.org/viewvc/llvm-project?rev=77628&view=rev
Log:
Factor code out into a DiagnoseUnusedExprResult function.
Modified:
cfe/trunk/lib/Sema/Sema.h
cfe/trunk/lib/Sema/SemaStmt.cpp
Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=77628&r1=77627&r2=77628&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Thu Jul 30 17:17:18 2009
@@ -1398,6 +1398,10 @@
MultiStmtArg Handlers);
void DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock);
+ /// DiagnoseUnusedExprResult - If the statement passed in is an expression
+ /// whose result is unused, warn.
+ void DiagnoseUnusedExprResult(const Stmt *S);
+
//===--------------------------------------------------------------------===//
// Expression Parsing Callbacks: SemaExpr.cpp.
Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=77628&r1=77627&r2=77628&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Thu Jul 30 17:17:18 2009
@@ -51,6 +51,23 @@
return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
}
+void Sema::DiagnoseUnusedExprResult(const Stmt *S) {
+ const Expr *E = dyn_cast<Expr>(S);
+ 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))
+ return;
+
+ Diag(Loc, diag::warn_unused_expr) << R1 << R2;
+}
+
Action::OwningStmtResult
Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R,
MultiStmtArg elts, bool isStmtExpr) {
@@ -76,20 +93,11 @@
}
// 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 they are non-void and if
- // this not the last stmt in a stmt expr.
- if (E->getType()->isVoidType() || (isStmtExpr && i == NumElts-1))
+ // Ignore statements that are last in a statement expression.
+ if (isStmtExpr && i == NumElts - 1)
continue;
- SourceLocation Loc;
- SourceRange R1, R2;
- if (!E->isUnusedResultAWarning(Loc, R1, R2))
- continue;
-
- Diag(Loc, diag::warn_unused_expr) << R1 << R2;
+ DiagnoseUnusedExprResult(Elts[i]);
}
return Owned(new (Context) CompoundStmt(Context, Elts, NumElts, L, R));
More information about the cfe-commits
mailing list