[cfe-commits] r71960 - in /cfe/trunk: lib/Lex/PPExpressions.cpp test/Preprocessor/overflow.c

Eli Friedman eli.friedman at gmail.com
Sat May 16 14:24:10 PDT 2009


Author: efriedma
Date: Sat May 16 16:24:10 2009
New Revision: 71960

URL: http://llvm.org/viewvc/llvm-project?rev=71960&view=rev
Log:
PR3942: Don't warn on unsigned overflow in preprocessor expressions.


Added:
    cfe/trunk/test/Preprocessor/overflow.c
Modified:
    cfe/trunk/lib/Lex/PPExpressions.cpp

Modified: cfe/trunk/lib/Lex/PPExpressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPExpressions.cpp?rev=71960&r1=71959&r2=71960&view=diff

==============================================================================
--- cfe/trunk/lib/Lex/PPExpressions.cpp (original)
+++ cfe/trunk/lib/Lex/PPExpressions.cpp Sat May 16 16:24:10 2009
@@ -511,7 +511,7 @@
         
     case tok::star:
       Res = LHS.Val * RHS.Val;
-      if (LHS.Val != 0 && RHS.Val != 0)
+      if (Res.isSigned() && LHS.Val != 0 && RHS.Val != 0)
         Overflow = Res/RHS.Val != LHS.Val || Res/LHS.Val != RHS.Val;
       break;
     case tok::lessless: {
@@ -520,7 +520,7 @@
       if (ShAmt >= LHS.Val.getBitWidth())
         Overflow = true, ShAmt = LHS.Val.getBitWidth()-1;
       else if (LHS.isUnsigned())
-        Overflow = ShAmt > LHS.Val.countLeadingZeros();
+        Overflow = false;
       else if (LHS.Val.isNonNegative()) // Don't allow sign change.
         Overflow = ShAmt >= LHS.Val.countLeadingZeros();
       else
@@ -540,7 +540,7 @@
     case tok::plus:
       Res = LHS.Val + RHS.Val;
       if (LHS.isUnsigned())
-        Overflow = Res.ult(LHS.Val);
+        Overflow = false;
       else if (LHS.Val.isNonNegative() == RHS.Val.isNonNegative() &&
                Res.isNonNegative() != LHS.Val.isNonNegative())
         Overflow = true;  // Overflow for signed addition.
@@ -548,7 +548,7 @@
     case tok::minus:
       Res = LHS.Val - RHS.Val;
       if (LHS.isUnsigned())
-        Overflow = Res.ugt(LHS.Val);
+        Overflow = false;
       else if (LHS.Val.isNonNegative() != RHS.Val.isNonNegative() &&
                Res.isNonNegative() != LHS.Val.isNonNegative())
         Overflow = true;  // Overflow for signed subtraction.

Added: cfe/trunk/test/Preprocessor/overflow.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/overflow.c?rev=71960&view=auto

==============================================================================
--- cfe/trunk/test/Preprocessor/overflow.c (added)
+++ cfe/trunk/test/Preprocessor/overflow.c Sat May 16 16:24:10 2009
@@ -0,0 +1,25 @@
+// RUN: clang-cc -Eonly %s -verify -triple i686-pc-linux-gnu
+
+// Multiply signed overflow
+#if 0x7FFFFFFFFFFFFFFF*2 // expected-warning {{overflow}}
+#endif
+
+// Multiply unsigned overflow
+#if 0xFFFFFFFFFFFFFFFF*2
+#endif
+
+// Add signed overflow
+#if 0x7FFFFFFFFFFFFFFF+1 // expected-warning {{overflow}}
+#endif
+
+// Add unsigned overflow
+#if 0xFFFFFFFFFFFFFFFF+1
+#endif
+
+// Subtract signed overflow
+#if 0x7FFFFFFFFFFFFFFF- -1 // expected-warning {{overflow}}
+#endif
+
+// Subtract unsigned overflow
+#if 0xFFFFFFFFFFFFFFFF- -1 // expected-warning {{converted from negative value}}
+#endif





More information about the cfe-commits mailing list