]> granicus.if.org Git - clang/commitdiff
PR3942: Don't warn on unsigned overflow in preprocessor expressions.
authorEli Friedman <eli.friedman@gmail.com>
Sat, 16 May 2009 21:24:10 +0000 (21:24 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Sat, 16 May 2009 21:24:10 +0000 (21:24 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71960 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Lex/PPExpressions.cpp
test/Preprocessor/overflow.c [new file with mode: 0644]

index 0c3aa0236104a1ebe5378890091a719082906357..47c3f8d1c709ad6a2b22856bd95f2972658686e9 100644 (file)
@@ -511,7 +511,7 @@ static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
         
     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 @@ static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
       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 @@ static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
     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 @@ static bool EvaluateDirectiveSubExpr(PPValue &LHS, unsigned MinPrec,
     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.
diff --git a/test/Preprocessor/overflow.c b/test/Preprocessor/overflow.c
new file mode 100644 (file)
index 0000000..297a35e
--- /dev/null
@@ -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