@@ -891,48 +891,6 @@ static bool isLowerEqualThanMulDiv(const Token* lower)
891891 return isLowerThanMulDiv (lower) || Token::Match (lower, " [*/%]" );
892892}
893893
894- static std::string ShiftInt (const char cop, const Token* left, const Token* right)
895- {
896- if (cop == ' &' || cop == ' |' || cop == ' ^' )
897- return MathLib::calculate (left->str (), right->str (), cop);
898-
899- const MathLib::bigint leftInt = MathLib::toLongNumber (left->str ());
900- const MathLib::bigint rightInt = MathLib::toLongNumber (right->str ());
901- const bool rightIntIsPositive = rightInt >= 0 ;
902-
903- if (cop == ' <' ) {
904- const bool leftOperationIsNotLeftShift = left->previous ()->str () != " <<" ;
905- const bool operandIsLeftShift = right->previous ()->str () == " <<" ;
906-
907- // Ensure that its not a shift operator as used for streams
908- if (leftOperationIsNotLeftShift && operandIsLeftShift && rightIntIsPositive) {
909- const bool leftIntIsPositive = leftInt >= 0 ;
910- if (!leftIntIsPositive) { // In case the left integer is negative, e.g. -1000 << 16. Do not simplify.
911- return left->str () + " << " + right->str ();
912- }
913- return MathLib::toString (leftInt << rightInt);
914- }
915- } else if (rightIntIsPositive) {
916- return MathLib::toString (leftInt >> rightInt);
917- }
918- return " " ;
919- }
920-
921- static std::string ShiftUInt (const char cop, const Token* left, const Token* right)
922- {
923- if (cop == ' &' || cop == ' |' || cop == ' ^' )
924- return MathLib::calculate (left->str (), right->str (), cop);
925-
926- const MathLib::biguint leftInt=MathLib::toULongNumber (left->str ());
927- const MathLib::biguint rightInt=MathLib::toULongNumber (right->str ());
928- if (cop == ' <' ) {
929- if (left->previous ()->str () != " <<" ) // Ensure that its not a shift operator as used for streams
930- return MathLib::toString (leftInt << rightInt);
931- } else {
932- return MathLib::toString (leftInt >> rightInt);
933- }
934- return " " ;
935- }
936894
937895bool TemplateSimplifier::simplifyNumericCalculations (Token *tok)
938896{
@@ -968,15 +926,34 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
968926 if (MathLib::isNegative (tok->str ()) || MathLib::isNegative (tok->strAt (2 )))
969927 continue ;
970928
971- const char cop = op->str ()[0 ];
972- std::string result;
973- if (tok->str ().find_first_of (" uU" ) != std::string::npos)
974- result = ShiftUInt (cop, tok, tok->tokAt (2 ));
975- else
976- result = ShiftInt (cop, tok, tok->tokAt (2 ));
977- if (result.empty ())
929+ const MathLib::value v1 (tok->str ());
930+ const MathLib::value v2 (tok->strAt (2 ));
931+
932+ if (!v1.isInt () || !v2.isInt ())
933+ continue ;
934+
935+ switch (op->str ()[0 ]) {
936+ case ' <' :
937+ tok->str ((v1 << v2).str ());
938+ ret = true ;
939+ break ;
940+ case ' >' :
941+ tok->str ((v1 >> v2).str ());
942+ ret = true ;
943+ break ;
944+ case ' &' :
945+ tok->str ((v1 & v2).str ());
946+ ret = true ;
947+ break ;
948+ case ' |' :
949+ tok->str ((v1 | v2).str ());
950+ ret = true ;
951+ break ;
952+ case ' ^' :
953+ tok->str ((v1 ^ v2).str ());
954+ ret = true ;
978955 break ;
979- tok-> str (result) ;
956+ } ;
980957 }
981958
982959 // Logical operations
0 commit comments