Fixed #1494 (Improve Tokenizer::simplifyKnownVariables to handle for loops better)

This commit is contained in:
Daniel Marjamäki 2010-03-14 09:57:34 +01:00
parent 7afdd8db31
commit 382e41d92a
2 changed files with 52 additions and 0 deletions

View File

@ -4807,6 +4807,48 @@ bool Tokenizer::simplifyKnownVariables()
ret = true;
}
// Using the variable in for-condition..
if (Token::simpleMatch(tok3, "for ("))
{
for (Token *tok4 = tok3->tokAt(2); tok4; tok4 = tok4->next())
{
if (tok4->str() == "(" || tok4->str() == ")")
break;
// Replace variable used in condition..
if (Token::Match(tok4, "; %var% <|<=|!= %var% ; ++| %var% ++| )"))
{
const Token *inctok = tok4->tokAt(5);
if (inctok->str() == "++")
inctok = inctok->next();
if (inctok->varId() == varid)
break;
if (tok4->next()->varId() == varid)
{
tok4->next()->str(value);
ret = true;
}
if (tok4->tokAt(3)->varId() == varid)
{
tok4->tokAt(3)->str(value);
ret = true;
}
}
}
if (tok3->next()->varId() == varid)
{
tok3->next()->str(value);
ret = true;
}
else if (tok3->tokAt(3)->varId() == varid)
{
tok3->tokAt(3)->str(value);
ret = true;
}
}
if (Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value))
{
const std::string op(tok3->strAt(2));

View File

@ -99,6 +99,7 @@ private:
TEST_CASE(simplifyKnownVariables18);
TEST_CASE(simplifyKnownVariables19);
TEST_CASE(simplifyKnownVariables20);
TEST_CASE(simplifyKnownVariables21);
TEST_CASE(match1);
@ -1063,6 +1064,15 @@ private:
simplifyKnownVariables(code));
}
void simplifyKnownVariables21()
{
const char code[] = "void foo() { int n = 10; for (int i = 0; i < n; ++i) { } }";
ASSERT_EQUALS(
"void foo ( ) { int n ; n = 10 ; for ( int i = 0 ; i < 10 ; ++ i ) { } }",
simplifyKnownVariables(code));
}
void match1()
{
// Match "%var% | %var%"