[PATCH] some StmtExprs do not have side-effects
scott douglass
sdouglass at arm.com
Fri Jun 5 08:53:42 PDT 2015
Thanks for the reviews! I have addressed the review comments. (But I still need a review on the prereq differential http://reviews.llvm.org/D10210)
http://reviews.llvm.org/D10211
Files:
include/clang/AST/EvaluatedExprVisitor.h
lib/AST/Expr.cpp
test/Sema/stmtexprs.c
Index: include/clang/AST/EvaluatedExprVisitor.h
===================================================================
--- include/clang/AST/EvaluatedExprVisitor.h
+++ 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:
Index: lib/AST/Expr.cpp
===================================================================
--- lib/AST/Expr.cpp
+++ lib/AST/Expr.cpp
@@ -2881,6 +2881,27 @@
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 (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
@@ -2967,14 +2988,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: test/Sema/stmtexprs.c
===================================================================
--- /dev/null
+++ 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; }) );
+}
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10211.27202.patch
Type: text/x-patch
Size: 2792 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150605/cf62af5c/attachment.bin>
More information about the cfe-commits
mailing list