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

Method constFoldBitwise

externals/simplecpp/simplecpp.cpp:1315–1346  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1313
1314static const std::string XOR("xor");
1315void simplecpp::TokenList::constFoldBitwise(Token *tok)
1316{
1317 Token * const tok1 = tok;
1318 for (const char *op = "&^|"; *op; op++) {
1319 const std::string* alternativeOp;
1320 if (*op == '&')
1321 alternativeOp = &BITAND;
1322 else if (*op == '|')
1323 alternativeOp = &BITOR;
1324 else
1325 alternativeOp = &XOR;
1326 for (tok = tok1; tok && tok->op != ')'; tok = tok->next) {
1327 if (tok->op != *op && !isAlternativeBinaryOp(tok, *alternativeOp))
1328 continue;
1329 if (!tok->previous || !tok->previous->number)
1330 continue;
1331 if (!tok->next || !tok->next->number)
1332 continue;
1333 long long result;
1334 if (*op == '&')
1335 result = (stringToLL(tok->previous->str()) & stringToLL(tok->next->str()));
1336 else if (*op == '^')
1337 result = (stringToLL(tok->previous->str()) ^ stringToLL(tok->next->str()));
1338 else /*if (*op == '|')*/
1339 result = (stringToLL(tok->previous->str()) | stringToLL(tok->next->str()));
1340 tok = tok->previous;
1341 tok->setstr(toString(result));
1342 deleteToken(tok->next);
1343 deleteToken(tok->next);
1344 }
1345 }
1346}
1347
1348static const std::string OR("or");
1349void simplecpp::TokenList::constFoldLogicalOp(Token *tok)

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected