[cfe-commits] r41655 - in /cfe/trunk: Parse/ParseExpr.cpp Parse/ParseStmt.cpp Sema/Sema.h Sema/SemaStmt.cpp clang.xcodeproj/project.pbxproj include/clang/Parse/Action.h include/clang/Parse/Parser.h test/Sema/unused-expr.c
Chris Lattner
sabre at nondot.org
Fri Aug 31 14:49:55 PDT 2007
Author: lattner
Date: Fri Aug 31 16:49:55 2007
New Revision: 41655
URL: http://llvm.org/viewvc/llvm-project?rev=41655&view=rev
Log:
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; });
}
Modified:
cfe/trunk/Parse/ParseExpr.cpp
cfe/trunk/Parse/ParseStmt.cpp
cfe/trunk/Sema/Sema.h
cfe/trunk/Sema/SemaStmt.cpp
cfe/trunk/clang.xcodeproj/project.pbxproj
cfe/trunk/include/clang/Parse/Action.h
cfe/trunk/include/clang/Parse/Parser.h
cfe/trunk/test/Sema/unused-expr.c
Modified: cfe/trunk/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Parse/ParseExpr.cpp?rev=41655&r1=41654&r2=41655&view=diff
==============================================================================
--- cfe/trunk/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/Parse/ParseExpr.cpp Fri Aug 31 16:49:55 2007
@@ -928,10 +928,9 @@
ExprResult Result(true);
CastTy = 0;
- if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
- !getLang().NoExtensions) {
+ if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace) {
Diag(Tok, diag::ext_gnu_statement_expr);
- Parser::StmtResult Stmt = ParseCompoundStatement();
+ Parser::StmtResult Stmt = ParseCompoundStatement(true);
ExprType = CompoundStmt;
// If the substmt parsed correctly, build the AST node.
Modified: cfe/trunk/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Parse/ParseStmt.cpp?rev=41655&r1=41654&r2=41655&view=diff
==============================================================================
--- cfe/trunk/Parse/ParseStmt.cpp (original)
+++ cfe/trunk/Parse/ParseStmt.cpp Fri Aug 31 16:49:55 2007
@@ -366,7 +366,7 @@
/// [OMP] barrier-directive
/// [OMP] flush-directive
///
-Parser::StmtResult Parser::ParseCompoundStatement() {
+Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
// Enter a scope to hold everything within the compound stmt. Compound
@@ -374,7 +374,7 @@
EnterScope(Scope::DeclScope);
// Parse the statements in the body.
- StmtResult Body = ParseCompoundStatementBody();
+ StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
ExitScope();
return Body;
@@ -385,7 +385,7 @@
/// ParseCompoundStmt action. This expects the '{' to be the current token, and
/// consume the '}' at the end of the block. It does not manipulate the scope
/// stack.
-Parser::StmtResult Parser::ParseCompoundStatementBody() {
+Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
// TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
@@ -442,7 +442,7 @@
SourceLocation RBraceLoc = ConsumeBrace();
return Actions.ParseCompoundStmt(LBraceLoc, RBraceLoc,
- &Stmts[0], Stmts.size());
+ &Stmts[0], Stmts.size(), isStmtExpr);
}
/// ParseIfStatement
Modified: cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/Sema.h?rev=41655&r1=41654&r2=41655&view=diff
==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Fri Aug 31 16:49:55 2007
@@ -202,7 +202,8 @@
virtual StmtResult ParseNullStmt(SourceLocation SemiLoc);
virtual StmtResult ParseCompoundStmt(SourceLocation L, SourceLocation R,
- StmtTy **Elts, unsigned NumElts);
+ StmtTy **Elts, unsigned NumElts,
+ bool isStmtExpr);
virtual StmtResult ParseDeclStmt(DeclTy *Decl);
virtual StmtResult ParseCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal,
SourceLocation DotDotDotLoc, ExprTy *RHSVal,
Modified: cfe/trunk/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaStmt.cpp?rev=41655&r1=41654&r2=41655&view=diff
==============================================================================
--- cfe/trunk/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/Sema/SemaStmt.cpp Fri Aug 31 16:49:55 2007
@@ -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 @@
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 @@
}
}
+ // 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);
}
Modified: cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=41655&r1=41654&r2=41655&view=diff
==============================================================================
--- cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/trunk/clang.xcodeproj/project.pbxproj Fri Aug 31 16:49:55 2007
@@ -637,7 +637,6 @@
08FB7793FE84155DC02AAC07 /* Project object */ = {
isa = PBXProject;
buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
- compatibilityVersion = "Xcode 2.4";
hasScannedForEncodings = 1;
mainGroup = 08FB7794FE84155DC02AAC07 /* clang */;
projectDirPath = "";
Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=41655&r1=41654&r2=41655&view=diff
==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Fri Aug 31 16:49:55 2007
@@ -191,7 +191,8 @@
}
virtual StmtResult ParseCompoundStmt(SourceLocation L, SourceLocation R,
- StmtTy **Elts, unsigned NumElts) {
+ StmtTy **Elts, unsigned NumElts,
+ bool isStmtExpr) {
return 0;
}
virtual StmtResult ParseDeclStmt(DeclTy *Decl) {
Modified: cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=41655&r1=41654&r2=41655&view=diff
==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Fri Aug 31 16:49:55 2007
@@ -361,8 +361,8 @@
StmtResult ParseIdentifierStatement(bool OnlyStatement);
StmtResult ParseCaseStatement();
StmtResult ParseDefaultStatement();
- StmtResult ParseCompoundStatement();
- StmtResult ParseCompoundStatementBody();
+ StmtResult ParseCompoundStatement(bool isStmtExpr = false);
+ StmtResult ParseCompoundStatementBody(bool isStmtExpr = false);
StmtResult ParseIfStatement();
StmtResult ParseSwitchStatement();
StmtResult ParseWhileStatement();
Modified: cfe/trunk/test/Sema/unused-expr.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/unused-expr.c?rev=41655&r1=41654&r2=41655&view=diff
==============================================================================
--- cfe/trunk/test/Sema/unused-expr.c (original)
+++ cfe/trunk/test/Sema/unused-expr.c Fri Aug 31 16:49:55 2007
@@ -30,3 +30,8 @@
c ? t1() : t2();
}
+// This shouldn't warn: the expr at the end of the stmtexpr really is used.
+int stmt_expr(int x, int y) {
+ return ({int _a = x, _b = y; _a > _b ? _a : _b; });
+}
+
More information about the cfe-commits
mailing list