Fixed ticket #519 (Tokenizer converts commas in for-loops)

http://sourceforge.net/apps/trac/cppcheck/ticket/519
This commit is contained in:
Slava Semushin 2009-07-28 23:40:15 +07:00
parent 600f3a834d
commit c8caefa94c
2 changed files with 29 additions and 1 deletions

View File

@ -3190,8 +3190,28 @@ void Tokenizer::syntaxError(const Token *tok, char c)
bool Tokenizer::simplifyComma()
{
bool ret = false;
bool insideLoop = false;
size_t indentlevel = 0;
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (tok->str() == "(")
{
++indentlevel;
}
else if (tok->str() == ")")
{
--indentlevel;
if (indentlevel == 0)
{
insideLoop = false;
}
}
else if (tok->str() == "for")
{
insideLoop = true;
}
if (tok->str() != ",")
continue;
@ -3218,7 +3238,7 @@ bool Tokenizer::simplifyComma()
{
for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous())
{
if (tok2->str() == "=")
if (tok2->str() == "=" && !insideLoop)
{
// Handle "a = 0, b = 0;"
tok->str(";");

View File

@ -1088,6 +1088,14 @@ private:
"}\n";
ASSERT_EQUALS(" void foo ( ) { delete a ; a = 0 ; }", sizeof_(code));
}
{
const char code[] = "void f()\n"
"{\n"
" for(int a,b; a < 10; a = a + 1, b = b + 1);\n"
"}\n";
ASSERT_EQUALS(" void f ( ) { for ( int a , b ; a < 10 ; a = a + 1 , b = b + 1 ) { ; } }", sizeof_(code));
}
}
void remove_comma()