diff options
author | David Blaikie <dblaikie@gmail.com> | 2012-10-05 00:41:03 +0000 |
---|---|---|
committer | David Blaikie <dblaikie@gmail.com> | 2012-10-05 00:41:03 +0000 |
commit | b3f55c5728aaf0d28579e67db3dd34c2934e7805 (patch) | |
tree | 682d28a991b03af974adb6cf00ce5bee45e875c6 /lib/Sema/SemaExpr.cpp | |
parent | 134d1e8a0b463d929ffeac5eefeae761707bf5d3 (diff) |
Implement -Wshift-op-parentheses for: a << b + c
This appears to be consistent with GCC's implementation of the same warning
under -Wparentheses. Suppressing a << b + c for cases where 'a' is a user
defined type for compatibility with C++ stream IO. Otherwise suggest
parentheses around the addition or subtraction subexpression.
(this came up when MSVC was complaining (incorrectly, so far as I can tell)
about a perceived violation of this within the LLVM codebase, PR14001)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@165283 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaExpr.cpp')
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index bbae55b598..d40818b83b 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -8570,6 +8570,20 @@ static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, } } +static void DiagnoseAdditionInShift(Sema &S, SourceLocation OpLoc, + Expr *SubExpr, StringRef shift) { + if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(SubExpr)) { + if (Bop->getOpcode() == BO_Add || Bop->getOpcode() == BO_Sub) { + StringRef op = Bop->getOpcode() == BO_Add ? "+" : "-"; + S.Diag(Bop->getOperatorLoc(), diag::warn_addition_in_bitshift) + << Bop->getSourceRange() << OpLoc << op << shift; + SuggestParentheses(S, Bop->getOperatorLoc(), + S.PDiag(diag::note_addition_in_bitshift_silence) << op, + Bop->getSourceRange()); + } + } +} + /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky /// precedence. static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, @@ -8591,6 +8605,13 @@ static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, LHSExpr, RHSExpr); DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, LHSExpr, RHSExpr); } + + if ((Opc == BO_Shl && LHSExpr->getType()->isIntegralType(Self.getASTContext())) + || Opc == BO_Shr) { + StringRef shift = Opc == BO_Shl ? "<<" : ">>"; + DiagnoseAdditionInShift(Self, OpLoc, LHSExpr, shift); + DiagnoseAdditionInShift(Self, OpLoc, RHSExpr, shift); + } } // Binary Operators. 'Tok' is the token for the operator. |