PR3942: Don't warn on unsigned overflow in preprocessor expressions.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71960 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Lex/PPExpressions.cpp b/lib/Lex/PPExpressions.cpp
index 0c3aa02..47c3f8d 100644
--- a/lib/Lex/PPExpressions.cpp
+++ b/lib/Lex/PPExpressions.cpp
@@ -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.
diff --git a/test/Preprocessor/overflow.c b/test/Preprocessor/overflow.c
new file mode 100644
index 0000000..297a35e
--- /dev/null
+++ b/test/Preprocessor/overflow.c
@@ -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