From: Ryan Flynn Date: Fri, 7 Aug 2009 16:20:20 +0000 (+0000) Subject: PR3333: warn when shifting by invalid amount X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d0439688fea4dedc28125d246bbdec1f5a208660;p=clang PR3333: warn when shifting by invalid amount git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78385 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index aac64cee65..591cf0a28a 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1237,6 +1237,14 @@ def warn_floatingpoint_eq : Warning< "comparing floating point with == or != is unsafe">, InGroup>, DefaultIgnore; +def warn_shift_negative : Warning< + "shift count is negative">; +def warn_shift_gt_typewidth : Warning< + "shift count >= width of type">; +def warn_op_no_effect : Warning< + "operation has no effect">, + InGroup>, DefaultIgnore; + def err_sizeof_nonfragile_interface : Error< "invalid application of '%select{alignof|sizeof}1' to interface %0 in " "non-fragile ABI">; 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; } diff --git a/test/Sema/shift.c b/test/Sema/shift.c index 5acbe12ac3..4c2b88a7f0 100644 --- a/test/Sema/shift.c +++ b/test/Sema/shift.c @@ -1,6 +1,35 @@ -// RUN: clang-cc -fsyntax-only %s +// RUN: clang-cc -Wall -fsyntax-only -verify %s + +#include + +enum { + X = 1 << 0, + Y = 1 << 1, + Z = 1 << 2 +}; void test() { char c; - c <<= 14; + + c = 0 << 0; + c = 0 << 1; // expected-warning {{no effect}} + c = 1 << 0; + c = 1 << -0; + c = 1 >> -0; + c = 1 << -1; // expected-warning {{shift count is negative}} + c = 1 >> -1; // expected-warning {{shift count is negative}} + c = 1 << c; + c <<= 0; // expected-warning {{no effect}} + c >>= 0; // expected-warning {{no effect}} + c <<= 1; + c >>= 1; + c <<= -1; // expected-warning {{shift count is negative}} + c >>= -1; // expected-warning {{shift count is negative}} + c <<= 999999; // expected-warning {{shift count >= width of type}} + c >>= 999999; // expected-warning {{shift count >= width of type}} + c <<= CHAR_BIT; // expected-warning {{shift count >= width of type}} + c >>= CHAR_BIT; // expected-warning {{shift count >= width of type}} + c <<= CHAR_BIT+1; // expected-warning {{shift count >= width of type}} + c >>= CHAR_BIT+1; // expected-warning {{shift count >= width of type}} + (void)((long)c << CHAR_BIT); }