diff options
author | Aaron Ballman <aaron@aaronballman.com> | 2014-12-17 21:57:17 +0000 |
---|---|---|
committer | Aaron Ballman <aaron@aaronballman.com> | 2014-12-17 21:57:17 +0000 |
commit | f1a2b5319ff6e4fb093c1dd3646e13c1086255a9 (patch) | |
tree | 3423aa4a85e5b0e647d7a7e7f6686a3c400fcd45 /lib/Sema/SemaExpr.cpp | |
parent | 9f988c44c935f42d9a131726302a7f997f2b3b37 (diff) |
Adding a -Wunused-value warning for expressions with side effects used in an unevaluated expression context, such as sizeof(), or decltype(). Also adds a similar warning when the expression passed to typeid() *is* evaluated, since it is equally likely that the user would expect the expression operand to be unevaluated in that case.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@224465 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaExpr.cpp')
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 22cc925c25..ef38fec2d9 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -1290,6 +1290,13 @@ Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, ControllingExpr = result.get(); } + // The controlling expression is an unevaluated operand, so side effects are + // likely unintended. + if (ActiveTemplateInstantiations.empty() && + ControllingExpr->HasSideEffects(Context, false)) + Diag(ControllingExpr->getExprLoc(), + diag::warn_side_effects_unevaluated_context); + bool TypeErrorFound = false, IsResultDependent = ControllingExpr->isTypeDependent(), ContainsUnexpandedParameterPack @@ -3525,6 +3532,12 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E, return true; } + // The operand for sizeof and alignof is in an unevaluated expression context, + // so side effects could result in unintended consequences. + if ((ExprKind == UETT_SizeOf || ExprKind == UETT_AlignOf) && + ActiveTemplateInstantiations.empty() && E->HasSideEffects(Context, false)) + Diag(E->getExprLoc(), diag::warn_side_effects_unevaluated_context); + if (CheckObjCTraitOperandConstraints(*this, ExprTy, E->getExprLoc(), E->getSourceRange(), ExprKind)) return true; |