diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4e7f73827..a90489e94 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8176,19 +8176,19 @@ void Tokenizer::simplifyErrNoInWhile() void Tokenizer::simplifyFuncInWhile() { + unsigned int count = 0; for (Token *tok = list.front(); tok; tok = tok->next()) { if (!Token::Match(tok, "while ( %var% ( %var% ) ) {")) continue; Token *func = tok->tokAt(2); Token *var = tok->tokAt(4); - Token *end = tok->linkAt(7); - if (!end) - break; + Token *end = tok->next()->link()->next()->link(); const unsigned int varid = ++_varId; // Create new variable + const std::string varname("cppcheck:r" + MathLib::longToString(++count)); tok->str("int"); - tok->next()->insertToken("cppcheck:r"); + tok->next()->insertToken(varname); tok->tokAt(2)->varId(varid); tok->insertToken("while"); tok->insertToken(";"); @@ -8198,10 +8198,10 @@ void Tokenizer::simplifyFuncInWhile() tok->insertToken("("); tok->insertToken(func->str()); tok->insertToken("="); - tok->insertToken("cppcheck:r"); + tok->insertToken(varname); tok->next()->varId(varid); Token::createMutualLinks(tok->tokAt(4), tok->tokAt(6)); - end->previous()->insertToken("cppcheck:r"); + end->previous()->insertToken(varname); end->previous()->varId(varid); end->previous()->insertToken("="); Token::move(func, func->tokAt(3), end->previous()); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 7fc5a5621..2a70c864e 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -7309,30 +7309,30 @@ private: } void simplifyFuncInWhile() { - ASSERT_EQUALS("int cppcheck:r = fclose ( f ) ; " - "while ( cppcheck:r ) " + ASSERT_EQUALS("int cppcheck:r1 = fclose ( f ) ; " + "while ( cppcheck:r1 ) " "{ " "foo ( ) ; " - "cppcheck:r = fclose ( f ) ; " + "cppcheck:r1 = fclose ( f ) ; " "}", tok("while(fclose(f))foo();")); - ASSERT_EQUALS("int cppcheck:r = fclose ( f ) ; " - "while ( cppcheck:r ) " + ASSERT_EQUALS("int cppcheck:r1 = fclose ( f ) ; " + "while ( cppcheck:r1 ) " "{ " - "; cppcheck:r = fclose ( f ) ; " + "; cppcheck:r1 = fclose ( f ) ; " "}", tok("while(fclose(f));")); - ASSERT_EQUALS("int cppcheck:r = fclose ( f ) ; " - "while ( cppcheck:r ) " + ASSERT_EQUALS("int cppcheck:r1 = fclose ( f ) ; " + "while ( cppcheck:r1 ) " "{ " - "; cppcheck:r = fclose ( f ) ; " + "; cppcheck:r1 = fclose ( f ) ; " "} " - "int cppcheck:r = fclose ( g ) ; " - "while ( cppcheck:r ) " + "int cppcheck:r2 = fclose ( g ) ; " + "while ( cppcheck:r2 ) " "{ " - "; cppcheck:r = fclose ( g ) ; " + "; cppcheck:r2 = fclose ( g ) ; " "}", tok("while(fclose(f)); while(fclose(g));")); }