[PATCH] some StmtExprs do not have side-effects
scott douglass
sdouglass at arm.com
Wed Jun 10 08:22:36 PDT 2015
REPOSITORY
rL LLVM
http://reviews.llvm.org/D10211
Files:
cfe/trunk/include/clang/AST/EvaluatedExprVisitor.h
cfe/trunk/lib/AST/Expr.cpp
cfe/trunk/test/Sema/stmtexprs.c
Index: cfe/trunk/lib/AST/Expr.cpp
===================================================================
--- cfe/trunk/lib/AST/Expr.cpp
+++ cfe/trunk/lib/AST/Expr.cpp
@@ -2887,6 +2887,28 @@
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,14 +2996,20 @@
case CompoundAssignOperatorClass:
case VAArgExprClass:
case AtomicExprClass:
- case StmtExprClass:
case CXXThrowExprClass:
case CXXNewExprClass:
case CXXDeleteExprClass:
case ExprWithCleanupsClass:
// 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:
Index: cfe/trunk/test/Sema/stmtexprs.c
===================================================================
--- cfe/trunk/test/Sema/stmtexprs.c
+++ cfe/trunk/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 at +1 {{the argument to '__builtin_assume' has side effects that will be discarded}}
+ __builtin_assume( ({ if (i) ({ stmtexpr_fn(); }); 1; }) );
+}
Index: cfe/trunk/include/clang/AST/EvaluatedExprVisitor.h
===================================================================
--- cfe/trunk/include/clang/AST/EvaluatedExprVisitor.h
+++ cfe/trunk/include/clang/AST/EvaluatedExprVisitor.h
@@ -28,6 +28,7 @@
/// 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:
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10211.27442.patch
Type: text/x-patch
Size: 2924 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150610/be7f3f1c/attachment.bin>
More information about the cfe-commits
mailing list