From e75a81669e44b05f9e4a3ed4083b5b8cdfeffd84 Mon Sep 17 00:00:00 2001 From: Raphael Geissert Date: Sun, 2 Jan 2011 16:41:34 -0600 Subject: [PATCH 1/2] Move simplification of realloc after simplification of math ops --- lib/tokenize.cpp | 60 ++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 3bf79ca03..9e553975e 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4076,36 +4076,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(); @@ -4145,6 +4115,36 @@ bool Tokenizer::simplifyTokenList() } } + // 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); + } + } + // Change initialisation of variable to assignment simplifyInitVar(); From 3d9550b0bf02948f76b7c36a9dc06a6881c0223f Mon Sep 17 00:00:00 2001 From: Raphael Geissert Date: Sun, 2 Jan 2011 17:46:10 -0600 Subject: [PATCH 2/2] Simplify "x = realloc (0, n);" to "x = malloc(n);" --- lib/tokenize.cpp | 13 +++++++++++++ test/testsimplifytokens.cpp | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 9e553975e..420767eb2 100644 --- a/lib/tokenize.cpp +++ b/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 diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 3c06af1c6..5e79adab9 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -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()