diff options
| author | Scott Douglass <sdouglass@arm.com> | 2015-06-10 15:18:23 +0000 |
|---|---|---|
| committer | Scott Douglass <sdouglass@arm.com> | 2015-06-10 15:18:23 +0000 |
| commit | cc01359cfe057ef49cdb45c28af94ad312141495 (patch) | |
| tree | 3b0b45930165c2dddd58f60ec3bda7ccbdb28cf8 | |
| parent | feacdd39d59e25e9bb1a850a2507266a37863b60 (diff) | |
| download | bcm5719-llvm-cc01359cfe057ef49cdb45c28af94ad312141495.tar.gz bcm5719-llvm-cc01359cfe057ef49cdb45c28af94ad312141495.zip | |
some StmtExprs do not have side-effects
Differential Revision: http://reviews.llvm.org/D10211
llvm-svn: 239476
| -rw-r--r-- | clang/include/clang/AST/EvaluatedExprVisitor.h | 1 | ||||
| -rw-r--r-- | clang/lib/AST/Expr.cpp | 30 | ||||
| -rw-r--r-- | clang/test/Sema/stmtexprs.c | 9 |
3 files changed, 39 insertions, 1 deletions
diff --git a/clang/include/clang/AST/EvaluatedExprVisitor.h b/clang/include/clang/AST/EvaluatedExprVisitor.h index 89d86419cfc..5cae5d9eca3 100644 --- a/clang/include/clang/AST/EvaluatedExprVisitor.h +++ b/clang/include/clang/AST/EvaluatedExprVisitor.h @@ -28,6 +28,7 @@ class ASTContext; /// of its potentially-evaluated subexpressions, recursively. template<template <typename> class Ptr, typename ImplClass> class EvaluatedExprVisitorBase : public StmtVisitorBase<Ptr, ImplClass, void> { +protected: const ASTContext &Context; public: diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 99c72436b44..36f4139f835 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -2887,6 +2887,28 @@ bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef, return false; } +namespace { + /// \brief Look for any side effects within a Stmt. + class SideEffectFinder : public ConstEvaluatedExprVisitor<SideEffectFinder> { + typedef ConstEvaluatedExprVisitor<SideEffectFinder> Inherited; + const bool IncludePossibleEffects; + bool HasSideEffects; + + public: + explicit SideEffectFinder(const ASTContext &Context, bool IncludePossible) + : Inherited(Context), + IncludePossibleEffects(IncludePossible), HasSideEffects(false) { } + + bool hasSideEffects() const { return HasSideEffects; } + + void VisitExpr(const Expr *E) { + if (!HasSideEffects && + E->HasSideEffects(Context, IncludePossibleEffects)) + HasSideEffects = true; + } + }; +} + bool Expr::HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects) const { // In circumstances where we care about definite side effects instead of @@ -2974,7 +2996,6 @@ bool Expr::HasSideEffects(const ASTContext &Ctx, case CompoundAssignOperatorClass: case VAArgExprClass: case AtomicExprClass: - case StmtExprClass: case CXXThrowExprClass: case CXXNewExprClass: case CXXDeleteExprClass: @@ -2982,6 +3003,13 @@ bool Expr::HasSideEffects(const ASTContext &Ctx, // These always have a side-effect. return true; + case StmtExprClass: { + // StmtExprs have a side-effect if any substatement does. + SideEffectFinder Finder(Ctx, IncludePossibleEffects); + Finder.Visit(cast<StmtExpr>(this)->getSubStmt()); + return Finder.hasSideEffects(); + } + case ParenExprClass: case ArraySubscriptExprClass: case MemberExprClass: diff --git a/clang/test/Sema/stmtexprs.c b/clang/test/Sema/stmtexprs.c new file mode 100644 index 00000000000..8594aae1868 --- /dev/null +++ b/clang/test/Sema/stmtexprs.c @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -Wno-gnu-statement-expression + +int stmtexpr_fn(); +void stmtexprs(int i) { + __builtin_assume( ({ 1; }) ); // no warning about "side effects" + __builtin_assume( ({ if (i) { (void)0; }; 42; }) ); // no warning about "side effects" + // expected-warning@+1 {{the argument to '__builtin_assume' has side effects that will be discarded}} + __builtin_assume( ({ if (i) ({ stmtexpr_fn(); }); 1; }) ); +} |

