Fixed #608 (Tokenizer: simplifyKnownVariables doesn't handle 'while (--i)' correctly)

This commit is contained in:
Daniel Marjamäki 2009-08-22 10:23:55 +02:00
parent 2664bcf650
commit 94c49bc34e
2 changed files with 42 additions and 0 deletions

View File

@ -2827,6 +2827,24 @@ bool Tokenizer::simplifyKnownVariables()
break;
}
// Stop if something like 'while (--var)' is found
if (tok3->str() == "while")
{
const Token *endpar = tok3->next()->link();
bool bailout = false;
for (const Token *tok4 = tok3; tok4 != endpar; tok4 = tok4->next())
{
if (Token::Match(tok4, "++|-- %varid%", varid) ||
Token::Match(tok4, "%varid% ++|--", varid))
{
bailout = true;
break;
}
}
if (bailout)
break;
}
if (bailOutFromLoop)
{
// This could be a loop, skip it, but only if it doesn't contain

View File

@ -78,6 +78,7 @@ private:
TEST_CASE(simplifyKnownVariables10);
TEST_CASE(simplifyKnownVariables11);
TEST_CASE(simplifyKnownVariables12);
TEST_CASE(simplifyKnownVariables13);
TEST_CASE(match1);
@ -873,6 +874,29 @@ private:
ASSERT_EQUALS(" ENTER_NAMESPACE ( project_namespace ) const double pi = 3.14 ; int main ( ) { }", ostr.str());
}
void simplifyKnownVariables13()
{
const char code[] = "void f()\n"
"{\n"
" int i = 10;\n"
" while(--i) {}\n"
"}\n";
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
tokenizer.setVarId();
tokenizer.simplifyKnownVariables();
std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str();
ASSERT_EQUALS(" void f ( ) { int i ; i = 10 ; while ( -- i ) { } }", ostr.str());
}
void match1()
{
// Match "%var% | %var%"