Tokenizer::simplifyCalculations: basic handling of bitwise operators

This commit is contained in:
Daniel Marjamäki 2011-02-14 20:43:26 +01:00
parent 3bb2850c5c
commit 0ed0d07714
2 changed files with 30 additions and 4 deletions

View File

@ -7071,10 +7071,11 @@ bool Tokenizer::simplifyCalculations()
{
// (1-2)
while (Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% [],);=<>+-*/]") ||
Token::Match(tok, "<< %num% [+-*/] %num% [],);=<>+-*/]") ||
Token::Match(tok, "[[,(=<>+-*] %num% [+-*/] %num% <<|>>") ||
Token::Match(tok, "<< %num% [+-*/] %num% <<"))
while (Token::Match(tok, "[[,(=<>+-*|&^] %num% [+-*/] %num% [],);=<>+-*/|&^]") ||
Token::Match(tok, "<< %num% [+-*/] %num% [],);=<>+-*/|&^]") ||
Token::Match(tok, "[[,(=<>+-*|&^] %num% [+-*/] %num% <<|>>") ||
Token::Match(tok, "<< %num% [+-*/] %num% <<") ||
Token::Match(tok, "[(,[] %num% [|&^] %num% [];,);]"))
{
tok = tok->next();
@ -7082,6 +7083,29 @@ bool Tokenizer::simplifyCalculations()
if (Token::simpleMatch(tok->next(), "/ 0"))
continue;
// & | ^
if (Token::Match(tok->next(), "[&|^]"))
{
std::string result;
const std::string first(tok->str());
const std::string second(tok->strAt(2));
const char op = tok->next()->str()[0];
if (op == '&')
result = MathLib::toString<MathLib::bigint>(MathLib::toLongNumber(first) & MathLib::toLongNumber(second));
else if (op == '|')
result = MathLib::toString<MathLib::bigint>(MathLib::toLongNumber(first) | MathLib::toLongNumber(second));
else if (op == '^')
result = MathLib::toString<MathLib::bigint>(MathLib::toLongNumber(first) ^ MathLib::toLongNumber(second));
if (!result.empty())
{
ret = true;
tok->str(result);
Token::eraseTokens(tok, tok->tokAt(3));
continue;
}
}
// + and - are calculated after * and /
if (Token::Match(tok->next(), "[+-/]"))
{

View File

@ -2591,6 +2591,8 @@ private:
ASSERT_EQUALS("a [ 0 ]", tok(code));
}
ASSERT_EQUALS("a [ 4 ] ;", tok("a[1+3|4];"));
ASSERT_EQUALS("x = 1 + 2 * y ;", tok("x=1+2*y;"));
ASSERT_EQUALS("x = 7 ;", tok("x=1+2*3;"));
ASSERT_EQUALS("x = 47185 ;", tok("x=(65536*72/100);"));