known variables: better handling of ++ and --

This commit is contained in:
Daniel Marjamäki 2009-03-04 06:03:51 +00:00
parent 832275bc37
commit 547f120ee9
2 changed files with 18 additions and 6 deletions

View File

@ -1713,20 +1713,32 @@ bool Tokenizer::simplifyKnownVariables()
if (Token::Match(tok3->next(), "%varid% ++|--", varid)) if (Token::Match(tok3->next(), "%varid% ++|--", varid))
{ {
tok3 = tok3->next(); const std::string op(tok3->strAt(2));
const std::string op(tok3->strAt(1)); if (Token::Match(tok3, "; %any% %any% ;"))
if (!Token::Match(tok3->previous(), "; %any% %any% ;"))
{ {
tok3->deleteNext();
tok3->deleteNext();
}
else
{
tok3 = tok3->next();
tok3->str(value.c_str()); tok3->str(value.c_str());
tok3->deleteNext(); tok3->deleteNext();
} }
incdec(value, op); incdec(value, op);
tok2->tokAt(2)->str(value.c_str());
} }
if (Token::Match(tok3->next(), "++|-- %varid%", varid)) if (Token::Match(tok3->next(), "++|-- %varid%", varid))
{ {
incdec(value, tok3->strAt(1)); incdec(value, tok3->strAt(1));
if (!Token::Match(tok3, "; %any% %any% ;")) tok2->tokAt(2)->str(value.c_str());
if (Token::Match(tok3, "; %any% %any% ;"))
{
tok3->deleteNext();
tok3->deleteNext();
}
else
{ {
tok3->deleteNext(); tok3->deleteNext();
tok3->next()->str(value.c_str()); tok3->next()->str(value.c_str());

View File

@ -552,7 +552,7 @@ private:
std::ostringstream ostr; std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str(); ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" void foo ( ) { int i = 22 ; abc [ 22 ] = 1 ; abc [ 24 ] = 2 ; }"), ostr.str()); ASSERT_EQUALS(std::string(" void foo ( ) { int i = 24 ; abc [ 22 ] = 1 ; abc [ 24 ] = 2 ; }"), ostr.str());
} }
void simplifyKnownVariables8() void simplifyKnownVariables8()
@ -574,7 +574,7 @@ private:
std::ostringstream ostr; std::ostringstream ostr;
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
ostr << " " << tok->str(); ostr << " " << tok->str();
ASSERT_EQUALS(std::string(" void foo ( ) { int i = 22 ; i ++ ; abc [ 23 ] = 0 ; }"), ostr.str()); ASSERT_EQUALS(std::string(" void foo ( ) { int i = 23 ; ; abc [ 23 ] = 0 ; }"), ostr.str());
} }