]> granicus.if.org Git - clang/commitdiff
PR3333: warn when shifting by invalid amount
authorRyan Flynn <pizza@parseerror.com>
Fri, 7 Aug 2009 16:20:20 +0000 (16:20 +0000)
committerRyan Flynn <pizza@parseerror.com>
Fri, 7 Aug 2009 16:20:20 +0000 (16:20 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78385 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExpr.cpp
test/Sema/shift.c

index aac64cee65b8d5be0c6557ee9b8072e816210061..591cf0a28ac374c0115cec1a028d0cde7f83ea59 100644 (file)
@@ -1237,6 +1237,14 @@ def warn_floatingpoint_eq : Warning<
   "comparing floating point with == or != is unsafe">,
   InGroup<DiagGroup<"float-equal">>, 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<DiagGroup<"all">>, DefaultIgnore;
+
 def err_sizeof_nonfragile_interface : Error<
   "invalid application of '%select{alignof|sizeof}1' to interface %0 in "
   "non-fragile ABI">;
index 7ef30d364758f419d3cd18fdc767a95e0174c139..51ebd079a496f6968562050956bf18187ab2d160 100644 (file)
@@ -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;
 }
index 5acbe12ac33eaf883a2234338e3541cca60984f5..4c2b88a7f01d1c3223af4be08e709672a54a0370 100644 (file)
@@ -1,6 +1,35 @@
-// RUN: clang-cc -fsyntax-only %s
+// RUN: clang-cc -Wall -fsyntax-only -verify %s
+
+#include <limits.h>
+
+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);
 }