Tokenizer: enhance simplifyNot() and rename to simplifyLogicalOperators().
Don't replace "and" everything becuse it may be used as variable name.
Better fix for #620
Corrections for commit eb05cf904d
This commit is contained in:
parent
0582572867
commit
085187b445
|
@ -1504,13 +1504,7 @@ void Tokenizer::simplifyTokenList()
|
|||
}
|
||||
}
|
||||
|
||||
// Replace "and" with "&&"
|
||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
||||
{
|
||||
if (tok->str() == "and")
|
||||
tok->str("&&");
|
||||
}
|
||||
|
||||
simplifyLogicalOperators();
|
||||
simplifyCasts();
|
||||
|
||||
// Simplify simple calculations..
|
||||
|
@ -1571,7 +1565,6 @@ void Tokenizer::simplifyTokenList()
|
|||
elseif();
|
||||
simplifyIfNot();
|
||||
simplifyIfNotNull();
|
||||
simplifyNot();
|
||||
simplifyIfAssign();
|
||||
|
||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
||||
|
@ -2826,9 +2819,10 @@ bool Tokenizer::simplifyIfNotNull()
|
|||
}
|
||||
|
||||
|
||||
bool Tokenizer::simplifyNot()
|
||||
bool Tokenizer::simplifyLogicalOperators()
|
||||
{
|
||||
// "if (not p)" => "if (!p)"
|
||||
// "if (p and q)" => "if (p and q)"
|
||||
bool ret = false;
|
||||
for (Token *tok = _tokens; tok; tok = tok->next())
|
||||
{
|
||||
|
@ -2844,6 +2838,13 @@ bool Tokenizer::simplifyNot()
|
|||
{
|
||||
tok->next()->str("!");
|
||||
}
|
||||
// "%var%|) and %var%|("
|
||||
else if (tok->str() == "and" &&
|
||||
((Token::Match(tok->previous(), "%var%") || tok->previous()->str() == ")") ||
|
||||
(Token::Match(tok->next(), "%var%") || tok->next()->str() == "(")))
|
||||
{
|
||||
tok->str("&&");
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -162,12 +162,15 @@ private:
|
|||
bool simplifyIfNotNull();
|
||||
|
||||
/**
|
||||
* simplify the "not" keyword to "!"
|
||||
* Example: "if (not p)" => "if (!p)"
|
||||
* Simplify the "not" and "and" keywords to "!" and "&&"
|
||||
* accordingly.
|
||||
* Examples:
|
||||
* "if (not p)" => "if (!p)"
|
||||
* "if (p and q)" => "if (p && q)"
|
||||
* @return true if something is modified
|
||||
* false if nothing is done.
|
||||
*/
|
||||
bool simplifyNot();
|
||||
bool simplifyLogicalOperators();
|
||||
|
||||
/**
|
||||
* Simplify comma into a semicolon when possible
|
||||
|
|
|
@ -1011,14 +1011,14 @@ private:
|
|||
|
||||
|
||||
|
||||
std::string simplifyNot(const char code[])
|
||||
std::string simplifyLogicalOperators(const char code[])
|
||||
{
|
||||
// tokenize..
|
||||
Tokenizer tokenizer;
|
||||
std::istringstream istr(code);
|
||||
tokenizer.tokenize(istr, "test.cpp");
|
||||
|
||||
tokenizer.simplifyNot();
|
||||
tokenizer.simplifyLogicalOperators();
|
||||
|
||||
std::ostringstream ostr;
|
||||
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
|
||||
|
@ -1029,18 +1029,24 @@ private:
|
|||
|
||||
void not1()
|
||||
{
|
||||
ASSERT_EQUALS("if ( ! p )", simplifyNot("if (not p)"));
|
||||
ASSERT_EQUALS("if ( p && ! q )", simplifyNot("if (p && not q)"));
|
||||
ASSERT_EQUALS("void foo ( not i )", simplifyNot("void foo ( not i )"));
|
||||
ASSERT_EQUALS("if ( ! p )", simplifyLogicalOperators("if (not p)"));
|
||||
ASSERT_EQUALS("if ( p && ! q )", simplifyLogicalOperators("if (p && not q)"));
|
||||
ASSERT_EQUALS("void foo ( not i )", simplifyLogicalOperators("void foo ( not i )"));
|
||||
}
|
||||
|
||||
void and1()
|
||||
{
|
||||
ASSERT_EQUALS(" if ( p && q )",
|
||||
sizeof_(" if (p and q)"));
|
||||
ASSERT_EQUALS("if ( p && q ) ;",
|
||||
simplifyLogicalOperators("if (p and q) ;"));
|
||||
|
||||
ASSERT_EQUALS(" return operator == ( a ) && radius == 4 ;",
|
||||
sizeof_(" return operator==(a) and radius == 4;"));
|
||||
ASSERT_EQUALS("if ( foo ( ) && q ) ;",
|
||||
simplifyLogicalOperators("if (foo() and q) ;"));
|
||||
|
||||
ASSERT_EQUALS("if ( foo ( ) && bar ( ) ) ;",
|
||||
simplifyLogicalOperators("if (foo() and bar()) ;"));
|
||||
|
||||
ASSERT_EQUALS("if ( p && bar ( ) ) ;",
|
||||
simplifyLogicalOperators("if (p and bar()) ;"));
|
||||
}
|
||||
|
||||
void comma_keyword()
|
||||
|
|
Loading…
Reference in New Issue