Improve Tokenizer::simplifyFuncInWhile.
Differentiate between the new cppcheck:r variables by appending a number to the name, like 'cppcheck:r1', 'cppcheck:r2' and more.
This commit is contained in:
parent
9dae536ce3
commit
76639578ad
|
@ -8176,19 +8176,19 @@ void Tokenizer::simplifyErrNoInWhile()
|
||||||
|
|
||||||
void Tokenizer::simplifyFuncInWhile()
|
void Tokenizer::simplifyFuncInWhile()
|
||||||
{
|
{
|
||||||
|
unsigned int count = 0;
|
||||||
for (Token *tok = list.front(); tok; tok = tok->next()) {
|
for (Token *tok = list.front(); tok; tok = tok->next()) {
|
||||||
if (!Token::Match(tok, "while ( %var% ( %var% ) ) {"))
|
if (!Token::Match(tok, "while ( %var% ( %var% ) ) {"))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Token *func = tok->tokAt(2);
|
Token *func = tok->tokAt(2);
|
||||||
Token *var = tok->tokAt(4);
|
Token *var = tok->tokAt(4);
|
||||||
Token *end = tok->linkAt(7);
|
Token *end = tok->next()->link()->next()->link();
|
||||||
if (!end)
|
|
||||||
break;
|
|
||||||
|
|
||||||
const unsigned int varid = ++_varId; // Create new variable
|
const unsigned int varid = ++_varId; // Create new variable
|
||||||
|
const std::string varname("cppcheck:r" + MathLib::longToString(++count));
|
||||||
tok->str("int");
|
tok->str("int");
|
||||||
tok->next()->insertToken("cppcheck:r");
|
tok->next()->insertToken(varname);
|
||||||
tok->tokAt(2)->varId(varid);
|
tok->tokAt(2)->varId(varid);
|
||||||
tok->insertToken("while");
|
tok->insertToken("while");
|
||||||
tok->insertToken(";");
|
tok->insertToken(";");
|
||||||
|
@ -8198,10 +8198,10 @@ void Tokenizer::simplifyFuncInWhile()
|
||||||
tok->insertToken("(");
|
tok->insertToken("(");
|
||||||
tok->insertToken(func->str());
|
tok->insertToken(func->str());
|
||||||
tok->insertToken("=");
|
tok->insertToken("=");
|
||||||
tok->insertToken("cppcheck:r");
|
tok->insertToken(varname);
|
||||||
tok->next()->varId(varid);
|
tok->next()->varId(varid);
|
||||||
Token::createMutualLinks(tok->tokAt(4), tok->tokAt(6));
|
Token::createMutualLinks(tok->tokAt(4), tok->tokAt(6));
|
||||||
end->previous()->insertToken("cppcheck:r");
|
end->previous()->insertToken(varname);
|
||||||
end->previous()->varId(varid);
|
end->previous()->varId(varid);
|
||||||
end->previous()->insertToken("=");
|
end->previous()->insertToken("=");
|
||||||
Token::move(func, func->tokAt(3), end->previous());
|
Token::move(func, func->tokAt(3), end->previous());
|
||||||
|
|
|
@ -7309,30 +7309,30 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
void simplifyFuncInWhile() {
|
void simplifyFuncInWhile() {
|
||||||
ASSERT_EQUALS("int cppcheck:r = fclose ( f ) ; "
|
ASSERT_EQUALS("int cppcheck:r1 = fclose ( f ) ; "
|
||||||
"while ( cppcheck:r ) "
|
"while ( cppcheck:r1 ) "
|
||||||
"{ "
|
"{ "
|
||||||
"foo ( ) ; "
|
"foo ( ) ; "
|
||||||
"cppcheck:r = fclose ( f ) ; "
|
"cppcheck:r1 = fclose ( f ) ; "
|
||||||
"}",
|
"}",
|
||||||
tok("while(fclose(f))foo();"));
|
tok("while(fclose(f))foo();"));
|
||||||
|
|
||||||
ASSERT_EQUALS("int cppcheck:r = fclose ( f ) ; "
|
ASSERT_EQUALS("int cppcheck:r1 = fclose ( f ) ; "
|
||||||
"while ( cppcheck:r ) "
|
"while ( cppcheck:r1 ) "
|
||||||
"{ "
|
"{ "
|
||||||
"; cppcheck:r = fclose ( f ) ; "
|
"; cppcheck:r1 = fclose ( f ) ; "
|
||||||
"}",
|
"}",
|
||||||
tok("while(fclose(f));"));
|
tok("while(fclose(f));"));
|
||||||
|
|
||||||
ASSERT_EQUALS("int cppcheck:r = fclose ( f ) ; "
|
ASSERT_EQUALS("int cppcheck:r1 = fclose ( f ) ; "
|
||||||
"while ( cppcheck:r ) "
|
"while ( cppcheck:r1 ) "
|
||||||
"{ "
|
"{ "
|
||||||
"; cppcheck:r = fclose ( f ) ; "
|
"; cppcheck:r1 = fclose ( f ) ; "
|
||||||
"} "
|
"} "
|
||||||
"int cppcheck:r = fclose ( g ) ; "
|
"int cppcheck:r2 = fclose ( g ) ; "
|
||||||
"while ( cppcheck:r ) "
|
"while ( cppcheck:r2 ) "
|
||||||
"{ "
|
"{ "
|
||||||
"; cppcheck:r = fclose ( g ) ; "
|
"; cppcheck:r2 = fclose ( g ) ; "
|
||||||
"}",
|
"}",
|
||||||
tok("while(fclose(f)); while(fclose(g));"));
|
tok("while(fclose(f)); while(fclose(g));"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue