simplify known variable: don't simplify this ';i++;'

This commit is contained in:
Daniel Marjamäki 2009-02-27 18:25:47 +00:00
parent c991aded12
commit 6b7b27a2e8
2 changed files with 35 additions and 5 deletions

View File

@ -1687,17 +1687,24 @@ bool Tokenizer::simplifyKnownVariables()
if (Token::Match(tok3->next(), "%varid% ++|--", varid)) if (Token::Match(tok3->next(), "%varid% ++|--", varid))
{ {
tok3 = tok3->next(); tok3 = tok3->next();
tok3->str(value.c_str()); const std::string op(tok3->strAt(1));
incdec(value, tok3->strAt(1)); if (!Token::Match(tok3->previous(), "; %any% %any% ;"))
tok3->deleteNext(); {
tok3->str(value.c_str());
tok3->deleteNext();
}
incdec(value, op);
} }
if (Token::Match(tok3->next(), "++|-- %varid%", varid)) if (Token::Match(tok3->next(), "++|-- %varid%", varid))
{ {
incdec(value, tok3->strAt(1)); incdec(value, tok3->strAt(1));
tok3->deleteNext(); if (!Token::Match(tok3, "; %any% %any% ;"))
{
tok3->deleteNext();
tok3->next()->str(value.c_str());
}
tok3 = tok3->next(); tok3 = tok3->next();
tok3->str(value.c_str());
} }
} }
} }

View File

@ -85,6 +85,7 @@ private:
TEST_CASE(simplifyKnownVariables5); TEST_CASE(simplifyKnownVariables5);
TEST_CASE(simplifyKnownVariables6); TEST_CASE(simplifyKnownVariables6);
TEST_CASE(simplifyKnownVariables7); TEST_CASE(simplifyKnownVariables7);
TEST_CASE(simplifyKnownVariables8);
TEST_CASE(multiCompare); TEST_CASE(multiCompare);
@ -557,6 +558,28 @@ private:
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 = 22 ; abc [ 22 ] = 1 ; abc [ 24 ] = 2 ; }"), ostr.str());
} }
void simplifyKnownVariables8()
{
const char code[] = "void foo()\n"
"{\n"
" int i = 22;\n"
" i++;\n"
" abc[i] = 0;\n"
"}\n";
// tokenize..
OurTokenizer 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(std::string(" void foo ( ) { int i = 22 ; i ++ ; abc [ 23 ] = 0 ; }"), ostr.str());
}
void multiCompare() void multiCompare()