Simplify "x = realloc (0, n);" to "x = malloc(n);"

This commit is contained in:
Raphael Geissert 2011-01-02 17:46:10 -06:00
parent e75a81669e
commit 3d9550b0bf
2 changed files with 17 additions and 0 deletions

View File

@ -4116,6 +4116,7 @@ bool Tokenizer::simplifyTokenList()
} }
// simplify "x=realloc(y,0);" => "free(y); x=0;".. // 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()) for (Token *tok = _tokens; tok; tok = tok->next())
{ {
if (Token::Match(tok, "; %var% = realloc ( %var% , 0 ) ;")) if (Token::Match(tok, "; %var% = realloc ( %var% , 0 ) ;"))
@ -4143,6 +4144,18 @@ bool Tokenizer::simplifyTokenList()
tok->insertToken(varname); tok->insertToken(varname);
tok->next()->varId(varid); 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 // Change initialisation of variable to assignment

View File

@ -6004,6 +6004,10 @@ private:
{ {
ASSERT_EQUALS("; free ( p ) ; p = 0 ;", ASSERT_EQUALS("; free ( p ) ; p = 0 ;",
tok("; p = realloc(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() void simplifyErrNoInWhile()