aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRyan Flynn <pizza@parseerror.com>2009-08-07 16:20:20 +0000
committerRyan Flynn <pizza@parseerror.com>2009-08-07 16:20:20 +0000
commitd0439688fea4dedc28125d246bbdec1f5a208660 (patch)
tree15e5acd5c8fd89d9174bb3dd1f2b56437bc49af0 /lib
parent42602bb40aefcc2751d4078ba88aacf4d965c9bd (diff)
PR3333: warn when shifting by invalid amount
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78385 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r--lib/Sema/SemaExpr.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 7ef30d3647..51ebd079a4 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -4124,6 +4124,29 @@ QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc,
UsualUnaryConversions(rex);
+ // Sanity-check shift operands
+ llvm::APSInt Right;
+ // Check right/shifter operand
+ if (rex->isIntegerConstantExpr(Right, Context)) {
+ // Check left/shiftee operand
+ llvm::APSInt Left;
+ if (lex->isIntegerConstantExpr(Left, Context)) {
+ if (Left == 0 && Right != 0)
+ Diag(Loc, diag::warn_op_no_effect)
+ << lex->getSourceRange() << rex->getSourceRange();
+ }
+ if (isCompAssign && Right == 0)
+ Diag(Loc, diag::warn_op_no_effect) << rex->getSourceRange();
+ else if (Right.isNegative())
+ Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange();
+ else {
+ llvm::APInt LeftBits(Right.getBitWidth(),
+ Context.getTypeSize(lex->getType()));
+ if (Right.uge(LeftBits))
+ Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange();
+ }
+ }
+
// "The type of the result is that of the promoted left operand."
return LHSTy;
}