diff options
author | Ryan Flynn <pizza@parseerror.com> | 2009-08-07 16:20:20 +0000 |
---|---|---|
committer | Ryan Flynn <pizza@parseerror.com> | 2009-08-07 16:20:20 +0000 |
commit | f53fab87d80bb26cb76a4938dc48b69fc5cfc91b (patch) | |
tree | 43d85dec3e5918695e63f6d2be9dbacdede566e3 /clang/lib | |
parent | 787591a5940ee82276db5e86d04015644c0d0f29 (diff) | |
download | bcm5719-llvm-f53fab87d80bb26cb76a4938dc48b69fc5cfc91b.tar.gz bcm5719-llvm-f53fab87d80bb26cb76a4938dc48b69fc5cfc91b.zip |
PR3333: warn when shifting by invalid amount
llvm-svn: 78385
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Sema/SemaExpr.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 7ef30d36475..51ebd079a49 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/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; } |