File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -964,6 +964,12 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
964964
965965 // Integer operations
966966 if (Token::Match (op, " >>|<<|&|^|%or%" )) {
967+ // Don't simplify if operand is negative, shifting with negative
968+ // operand is UB. Bitmasking with negative operand is implementation
969+ // defined behaviour.
970+ if (MathLib::isNegative (tok->str ()) || MathLib::isNegative (tok->strAt (2 )))
971+ continue ;
972+
967973 const char cop = op->str ()[0 ];
968974 std::string result;
969975 if (tok->str ().find_first_of (" uU" ) != std::string::npos)
Original file line number Diff line number Diff line change @@ -2096,6 +2096,7 @@ class TestSimplifyTokens : public TestFixture {
20962096 ASSERT_EQUALS (" x ( 1 )" , tok (" x(2 && 4<<4<<5 && 4)" )); // #4933
20972097 ASSERT_EQUALS (" x ( 1 )" , tok (" x(9&&8%5%4/3)" )); // #4931
20982098 ASSERT_EQUALS (" x ( 1 )" , tok (" x(2 && 2|5<<2%4)" )); // #4931
2099+ ASSERT_EQUALS (" x ( -2 << 6 | 1 )" , tok (" x(1-3<<6|5/3)" )); // #4931
20992100
21002101 // don't remove these spaces..
21012102 ASSERT_EQUALS (" new ( auto ) ( 4 ) ;" , tok (" new (auto)(4);" ));
You can’t perform that action at this time.
0 commit comments