[cfe-commits] r78385 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/Sema/shift.c

Ryan Flynn pizza at parseerror.com
Fri Aug 7 09:20:20 PDT 2009


Author: pizza
Date: Fri Aug  7 11:20:20 2009
New Revision: 78385

URL: http://llvm.org/viewvc/llvm-project?rev=78385&view=rev
Log:
PR3333: warn when shifting by invalid amount

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/shift.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=78385&r1=78384&r2=78385&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Aug  7 11:20:20 2009
@@ -1237,6 +1237,14 @@
   "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">;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=78385&r1=78384&r2=78385&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Aug  7 11:20:20 2009
@@ -4124,6 +4124,29 @@
 
   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;
 }

Modified: cfe/trunk/test/Sema/shift.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/shift.c?rev=78385&r1=78384&r2=78385&view=diff

==============================================================================
--- cfe/trunk/test/Sema/shift.c (original)
+++ cfe/trunk/test/Sema/shift.c Fri Aug  7 11:20:20 2009
@@ -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);
 }





More information about the cfe-commits mailing list