Browse files

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

  • Loading branch information...
1 parent e75a816 commit 3d9550b0bf02948f76b7c36a9dc06a6881c0223f @rgeissert committed Jan 2, 2011
Showing with 17 additions and 0 deletions.
  1. +13 −0 lib/tokenize.cpp
  2. +4 −0 test/testsimplifytokens.cpp
View
13 lib/tokenize.cpp
@@ -4116,6 +4116,7 @@ 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 ) ;"))
@@ -4143,6 +4144,18 @@ bool Tokenizer::simplifyTokenList()
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
View
4 test/testsimplifytokens.cpp
@@ -6004,6 +6004,10 @@ class TestSimplifyTokens : public TestFixture
{
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()

0 comments on commit 3d9550b

Please sign in to comment.