MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / constFoldMulDivRem

Method constFoldMulDivRem

externals/simplecpp/simplecpp.cpp:1197–1229  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1195}
1196
1197void simplecpp::TokenList::constFoldMulDivRem(Token *tok)
1198{
1199 for (; tok && tok->op != ')'; tok = tok->next) {
1200 if (!tok->previous || !tok->previous->number)
1201 continue;
1202 if (!tok->next || !tok->next->number)
1203 continue;
1204
1205 long long result;
1206 if (tok->op == '*') {
1207 result = (stringToLL(tok->previous->str()) * stringToLL(tok->next->str()));
1208 }
1209 else if (tok->op == '/' || tok->op == '%') {
1210 const long long rhs = stringToLL(tok->next->str());
1211 if (rhs == 0)
1212 throw std::overflow_error("division/modulo by zero");
1213 const long long lhs = stringToLL(tok->previous->str());
1214 if (rhs == -1 && lhs == std::numeric_limits<long long>::min())
1215 throw std::overflow_error("division overflow");
1216 if (tok->op == '/')
1217 result = (lhs / rhs);
1218 else
1219 result = (lhs % rhs);
1220 } else {
1221 continue;
1222 }
1223
1224 tok = tok->previous;
1225 tok->setstr(toString(result));
1226 deleteToken(tok->next);
1227 deleteToken(tok->next);
1228 }
1229}
1230
1231void simplecpp::TokenList::constFoldAddSub(Token *tok)
1232{

Callers

nothing calls this directly

Calls 4

stringToLLFunction · 0.85
deleteTokenFunction · 0.85
toStringFunction · 0.70
strMethod · 0.45

Tested by

no test coverage detected