diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 50adf447c..9bced459a 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -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(";"); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index b9fafd201..f79573542 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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()