simplifyNumericCalculations: Don't fold negative constants in shift/bitmask calculation. Behaviour is not well defined.

This commit is contained in:
Daniel Marjamäki 2015-11-27 14:16:49 +01:00
parent 3b4160600d
commit 1977a18a1e
2 changed files with 7 additions and 0 deletions

View File

@ -964,6 +964,12 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
// Integer operations
if (Token::Match(op, ">>|<<|&|^|%or%")) {
// Don't simplify if operand is negative, shifting with negative
// operand is UB. Bitmasking with negative operand is implementation
// defined behaviour.
if (MathLib::isNegative(tok->str()) || MathLib::isNegative(tok->strAt(2)))
continue;
const char cop = op->str()[0];
std::string result;
if (tok->str().find_first_of("uU") != std::string::npos)

View File

@ -2096,6 +2096,7 @@ private:
ASSERT_EQUALS("x ( 1 )", tok("x(2 && 4<<4<<5 && 4)")); // #4933
ASSERT_EQUALS("x ( 1 )", tok("x(9&&8%5%4/3)")); // #4931
ASSERT_EQUALS("x ( 1 )", tok("x(2 && 2|5<<2%4)")); // #4931
ASSERT_EQUALS("x ( -2 << 6 | 1 )", tok("x(1-3<<6|5/3)")); // #4931
// don't remove these spaces..
ASSERT_EQUALS("new ( auto ) ( 4 ) ;", tok("new (auto)(4);"));