Merge branch 'master' of https://github.com/rgeissert/cppcheck into rgeissert-realloc

This commit is contained in:
Daniel Marjamäki 2011-01-04 18:21:18 +01:00
commit 2c6421d162
2 changed files with 47 additions and 30 deletions

View File

@ -4117,36 +4117,6 @@ bool Tokenizer::simplifyTokenList()
simplifyCasts();
// simplify "x=realloc(y,0);" => "free(y); x=0;"..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "; %var% = realloc ( %var% , 0 ) ;"))
{
const std::string varname(tok->next()->str());
const unsigned int varid(tok->next()->varId());
// Delete the "%var% ="
tok->deleteNext();
tok->deleteNext();
// Change function name "realloc" to "free"
tok->next()->str("free");
// delete the ", 0"
Token::eraseTokens(tok->tokAt(3), tok->tokAt(6));
// goto the ";"
tok = tok->tokAt(5);
// insert "var=0;"
tok->insertToken(";");
tok->insertToken("0");
tok->insertToken("=");
tok->insertToken(varname);
tok->next()->varId(varid);
}
}
// Simplify simple calculations..
simplifyCalculations();
@ -4186,6 +4156,49 @@ bool Tokenizer::simplifyTokenList()
}
}
// simplify "x=realloc(y,0);" => "free(y); x=0;"..
// and "x = realloc (0, n);" => "x = malloc(n);"
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "; %var% = realloc ( %var% , 0 ) ;"))
{
const std::string varname(tok->next()->str());
const unsigned int varid(tok->next()->varId());
// Delete the "%var% ="
tok->deleteNext();
tok->deleteNext();
// Change function name "realloc" to "free"
tok->next()->str("free");
// delete the ", 0"
Token::eraseTokens(tok->tokAt(3), tok->tokAt(6));
// goto the ";"
tok = tok->tokAt(5);
// insert "var=0;"
tok->insertToken(";");
tok->insertToken("0");
tok->insertToken("=");
tok->insertToken(varname);
tok->next()->varId(varid);
}
else if (Token::Match(tok, "; %var% = realloc ( 0 , %num% ) ;"))
{
const std::string varname(tok->next()->str());
tok = tok->tokAt(3);
// Change function name "realloc" to "malloc"
tok->str("malloc");
// delete "0 ,"
tok->next()->deleteNext();
tok->next()->deleteNext();
}
}
// Change initialisation of variable to assignment
simplifyInitVar();

View File

@ -6004,6 +6004,10 @@ private:
{
ASSERT_EQUALS("; free ( p ) ; p = 0 ;",
tok("; p = realloc(p,0);"));
ASSERT_EQUALS("; p = malloc ( 100 ) ;",
tok("; p = realloc(0, 100);"));
ASSERT_EQUALS("; p = malloc ( 0 ) ;",
tok("; p = realloc(0, sizeof(char)*0);"));
}
void simplifyErrNoInWhile()