Fixed #2025 (iso646 c++ operators are not handled correctly)

This commit is contained in:
Daniel Marjamäki 2010-09-09 17:43:09 +02:00
parent 6d35396720
commit 5af0d65aef
2 changed files with 48 additions and 14 deletions

View File

@ -5546,33 +5546,53 @@ bool Tokenizer::simplifyLogicalOperators()
// "if (p or q)" => "if (p || q)"
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "if|while ( not %var%"))
if (Token::Match(tok, "if|while ( not|compl %var%"))
{
tok->tokAt(2)->str("!");
tok->tokAt(2)->str(tok->strAt(2) == "not" ? "!" : "~");
ret = true;
}
else if (Token::Match(tok, "&& not %var%"))
else if (Token::Match(tok, "&& not|compl %var%"))
{
tok->next()->str("!");
tok->next()->str(tok->strAt(1) == "not" ? "!" : "~");
ret = true;
}
else if (Token::Match(tok, "|| not %var%"))
else if (Token::Match(tok, "|| not|compl %var%"))
{
tok->next()->str("!");
tok->next()->str(tok->strAt(1) == "not" ? "!" : "~");
ret = true;
}
// "%var%|) and %var%|("
else if (Token::Match(tok->previous(), "%any% and|or %any%") &&
((tok->previous()->isName() || tok->previous()->str() == ")") ||
(tok->next()->isName() || tok->next()->str() == "(")))
else if (Token::Match(tok->previous(), "%any% %var% %any%"))
{
if (!Token::Match(tok, "and|or|bitand|bitor|xor|not_eq"))
continue;
const Token *tok2 = tok;
while (0 != (tok2 = tok2->previous()))
{
if (tok2->str() == ")")
tok2 = tok2->link();
else if (Token::Match(tok2, "(|;|{|}"))
break;
}
if (tok2 && Token::Match(tok2->previous(), "if|while ("))
{
if (tok->str() == "and")
tok->str("&&");
else
else if (tok->str() == "or")
tok->str("||");
else if (tok->str() == "bitand")
tok->str("&");
else if (tok->str() == "bitor")
tok->str("|");
else if (tok->str() == "xor")
tok->str("^");
else if (tok->str() == "not_eq")
tok->str("!=");
ret = true;
}
}
}
return ret;
}

View File

@ -259,6 +259,8 @@ private:
TEST_CASE(borland);
TEST_CASE(sql);
TEST_CASE(simplifyLogicalOperators);
}
@ -4541,6 +4543,18 @@ private:
ASSERT_EQUALS("; asm ( ) ;", tokenizeAndStringify(code1,false));
}
void simplifyLogicalOperators()
{
ASSERT_EQUALS("if ( a && b )", tokenizeAndStringify("if (a and b)"));
ASSERT_EQUALS("if ( a || b )", tokenizeAndStringify("if (a or b)"));
ASSERT_EQUALS("if ( a & b )", tokenizeAndStringify("if (a bitand b)"));
ASSERT_EQUALS("if ( a | b )", tokenizeAndStringify("if (a bitor b)"));
ASSERT_EQUALS("if ( a ^ b )", tokenizeAndStringify("if (a xor b)"));
ASSERT_EQUALS("if ( ~ b )", tokenizeAndStringify("if (compl b)"));
ASSERT_EQUALS("if ( ! b )", tokenizeAndStringify("if (not b)"));
ASSERT_EQUALS("if ( a != b )", tokenizeAndStringify("if (a not_eq b)"));
}
};
REGISTER_TEST(TestTokenizer)