Fixed #1642 (Tokenizer::simplifyKnownVariables: simplify after loop)

This commit is contained in:
Daniel Marjamäki 2010-05-02 14:41:21 +02:00
parent 0415560912
commit 804b2a0952
2 changed files with 50 additions and 8 deletions

View File

@ -4949,16 +4949,35 @@ bool Tokenizer::simplifyKnownVariables()
const bool pointeralias(tok2->tokAt(2)->isName() || tok2->tokAt(2)->str() == "&"); const bool pointeralias(tok2->tokAt(2)->isName() || tok2->tokAt(2)->str() == "&");
std::string value(tok2->strAt(2)); std::string value;
Token *tok3 = NULL;
if (Token::Match(tok2->tokAt(-2), "for ( %varid% = %num% ; %varid% <|<= %num% ; ++| %varid% ++| ) {", varid))
{
const std::string compareop = tok2->strAt(5);
if (compareop == "<")
value = tok2->strAt(6);
else
value = MathLib::toString(MathLib::toLongNumber(tok2->strAt(6)) + 1);
// Skip for-body..
tok3 = tok2->previous()->link()->next()->link()->next();
}
else
{
value = tok2->strAt(2);
if (value == "]") if (value == "]")
value = tok2->strAt(4); value = tok2->strAt(4);
else if (value == "&") else if (value == "&")
value = tok2->strAt(3); value = tok2->strAt(3);
Token* bailOutFromLoop = 0;
if (Token::simpleMatch(tok2->next(), "= &")) if (Token::simpleMatch(tok2->next(), "= &"))
tok2 = tok2->tokAt(3); tok2 = tok2->tokAt(3);
tok3 = tok2->next();
}
Token* bailOutFromLoop = 0;
int indentlevel3 = indentlevel; // indentlevel for tok3 int indentlevel3 = indentlevel; // indentlevel for tok3
for (Token *tok3 = tok2->next(); tok3; tok3 = tok3->next()) for (; tok3; tok3 = tok3->next())
{ {
if (tok3->str() == "{") if (tok3->str() == "{")
{ {

View File

@ -101,6 +101,7 @@ private:
TEST_CASE(simplifyKnownVariables21); TEST_CASE(simplifyKnownVariables21);
TEST_CASE(simplifyKnownVariables22); TEST_CASE(simplifyKnownVariables22);
TEST_CASE(simplifyKnownVariables23); TEST_CASE(simplifyKnownVariables23);
TEST_CASE(simplifyKnownVariables24);
TEST_CASE(match1); TEST_CASE(match1);
@ -1145,6 +1146,28 @@ private:
simplifyKnownVariables(code)); simplifyKnownVariables(code));
} }
void simplifyKnownVariables24()
{
// This testcase is related to ticket #1596
const char code[] = "void foo()\n"
"{\n"
" int c;\n"
" for (c=0;c<10;++c) { }\n"
" a[c] = 0;\n"
"}\n";
// Current result
ASSERT_EQUALS(
"void foo ( ) "
"{"
" int c ;"
" for ( c = 0 ; c < 10 ; ++ c ) { }"
" a [ 10 ] = 0 ; "
"}",
simplifyKnownVariables(code));
}
void match1() void match1()
{ {
// Match "%var% | %var%" // Match "%var% | %var%"