From: Eli Friedman Date: Sat, 16 May 2009 21:24:10 +0000 (+0000) Subject: PR3942: Don't warn on unsigned overflow in preprocessor expressions. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3265a42e5a87269fce5d7a7cdd7bafe62c7becfe;p=clang 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 0c3aa02361..47c3f8d1c7 100644 --- a/lib/Lex/PPExpressions.cpp +++ b/lib/Lex/PPExpressions.cpp @@ -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 index 0000000000..297a35e658 --- /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