Refactoring: %var1% complitely removed. Execution time increased from 4,7 to 6,2 seconds.

This commit is contained in:
Reijo Tomperi 2009-01-05 23:06:51 +00:00
parent 49b864b1fd
commit e721383d01
3 changed files with 19 additions and 57 deletions

View File

@ -354,6 +354,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
const char *varnames[2]; const char *varnames[2];
varnames[0] = varname; varnames[0] = varname;
varnames[1] = 0; varnames[1] = 0;
std::string varnameStr = varname;
Token *rethead = 0, *rettail = 0; Token *rethead = 0, *rettail = 0;
#define addtoken(_str) \ #define addtoken(_str) \
@ -405,7 +406,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
if (parlevel == 0 && tok->str() == ";") if (parlevel == 0 && tok->str() == ";")
addtoken(";"); addtoken(";");
if (Token::Match(tok, "[(;{}] %var1% =", 0, varnames)) if (Token::Match(tok, std::string("[(;{}] " + varnameStr + " =").c_str()))
{ {
AllocType alloc = GetAllocationType(tok->tokAt(3)); AllocType alloc = GetAllocationType(tok->tokAt(3));
bool realloc = false; bool realloc = false;
@ -480,9 +481,9 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
} }
// if else switch // if else switch
if (Token::Match(tok, "if ( %var1% )", 0, varnames) || if (Token::simpleMatch(tok, std::string("if ( " + varnameStr + " )").c_str()) ||
Token::Match(tok, "if ( %var1% != 0 )", 0, varnames) || Token::simpleMatch(tok, std::string("if ( " + varnameStr + " != 0 )").c_str()) ||
Token::Match(tok, "if ( 0 != %var1% )", 0, varnames)) Token::simpleMatch(tok, std::string("if ( 0 != " + varnameStr + " )").c_str()))
{ {
addtoken("if(var)"); addtoken("if(var)");
@ -509,7 +510,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
if (parlevel <= 0) if (parlevel <= 0)
break; break;
} }
if (Token::Match(tok2, "fclose ( %var1% )", 0, varnames)) if (Token::simpleMatch(tok2, std::string("fclose ( " + varnameStr + " )").c_str()))
{ {
addtoken("dealloc"); addtoken("dealloc");
addtoken(";"); addtoken(";");
@ -517,8 +518,8 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
break; break;
} }
if ((tok2->str() != ".") && if ((tok2->str() != ".") &&
Token::Match(tok2->next(), "%var1%", 0, varnames) && Token::simpleMatch(tok2->next(), varnameStr.c_str()) &&
!Token::Match(tok2->next(), "%var1% .", 0, varnames)) !Token::simpleMatch(tok2->next(), std::string(varnameStr + " .").c_str()))
{ {
dep = true; dep = true;
break; break;
@ -572,8 +573,8 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
if (tok->str() == "return") if (tok->str() == "return")
{ {
addtoken("return"); addtoken("return");
if (Token::Match(tok, "return %var1%", 0, varnames) || if (Token::simpleMatch(tok, std::string("return " + varnameStr).c_str()) ||
Token::Match(tok, "return & %var1%", 0, varnames)) Token::simpleMatch(tok, std::string("return & " + varnameStr).c_str()))
addtoken("use"); addtoken("use");
if (Token::simpleMatch(tok->next(), "(")) if (Token::simpleMatch(tok->next(), "("))
{ {
@ -596,13 +597,13 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
addtoken(tok->strAt(0)); addtoken(tok->strAt(0));
// Assignment.. // Assignment..
if (Token::Match(tok, "[)=] %var1% [+;)]", 0, varnames) || if (Token::Match(tok, std::string("[)=] " + varnameStr + " [+;)]").c_str()) ||
Token::Match(tok, "%var1% +=|-=", 0, varnames) || Token::Match(tok, std::string(varnameStr + " +=|-=").c_str()) ||
Token::Match(tok, "+=|<< %var1% ;", 0, varnames)) Token::Match(tok, std::string("+=|<< " + varnameStr + " ;").c_str()))
{ {
addtoken("use"); addtoken("use");
} }
else if (Token::Match(tok, "[;{}=(,+-*/] %var1% [", 0, varnames)) else if (Token::Match(tok, std::string("[;{}=(,+-*/] " + varnameStr + " [").c_str()))
{ {
addtoken("use_"); addtoken("use_");
} }
@ -642,11 +643,11 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
} }
// Linux lists.. // Linux lists..
if (Token::Match(tok, "[=(,] & %var1% [.[]", 0, varnames)) if (Token::Match(tok, std::string("[=(,] & " + varnameStr + " [.[]").c_str()))
{ {
addtoken("&use"); addtoken("&use");
} }
else if (Token::Match(tok, "[=(,] & %var1% [,)]", 0, varnames)) else if (Token::Match(tok, std::string("[=(,] & " + varnameStr + " [,)]").c_str()))
{ {
addtoken("&use2"); addtoken("&use2");
} }

View File

@ -164,7 +164,7 @@ bool Token::simpleMatch(const Token *tok, const char pattern[])
return true; return true;
} }
bool Token::Match(const Token *tok, const char pattern[], unsigned int varid, const char *varname1[]) bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
{ {
const char *p = pattern; const char *p = pattern;
while (*p) while (*p)
@ -218,32 +218,6 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid, co
patternIdentified = true; patternIdentified = true;
} }
// Variable name..
else if (strcmp(str, "%var1%") == 0)
{
if (! varname1)
return false;
if (tok->_str != varname1[0])
return false;
for (int i = 1; varname1[i]; i++)
{
if (!(tok->tokAt(2)))
return false;
if (strcmp(tok->strAt(1), "."))
return false;
if (strcmp(tok->strAt(2), varname1[i]))
return false;
tok = tok->tokAt(2);
}
patternIdentified = true;
}
else if (strcmp(str, "%varid%") == 0) else if (strcmp(str, "%varid%") == 0)
{ {
if (varid == 0) if (varid == 0)
@ -354,16 +328,6 @@ bool Token::isStandardType() const
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
const Token *Token::findmatch(const Token *tok, const char pattern[], const char *varname1[])
{
for (; tok; tok = tok->next())
{
if (Token::Match(tok, pattern, 0, varname1))
return tok;
}
return 0;
}
const Token *Token::findmatch(const Token *tok, const char pattern[], unsigned int varId) const Token *Token::findmatch(const Token *tok, const char pattern[], unsigned int varId)
{ {
for (; tok; tok = tok->next()) for (; tok; tok = tok->next())

View File

@ -92,7 +92,6 @@ public:
* "%num%" Any numeric token, e.g. "23" * "%num%" Any numeric token, e.g. "23"
* "%bool%" true or false * "%bool%" true or false
* "%str%" Any token starting with "-character (C-string). * "%str%" Any token starting with "-character (C-string).
* "%var1%" Match with parameter varname1
* "%varid%" Match with parameter varid * "%varid%" Match with parameter varid
* "[abc]" Any of the characters 'a' or 'b' or 'c' * "[abc]" Any of the characters 'a' or 'b' or 'c'
* "int|void|char" Any of the strings, int, void or char * "int|void|char" Any of the strings, int, void or char
@ -109,18 +108,16 @@ public:
* @param tok List of tokens to be compared to the pattern * @param tok List of tokens to be compared to the pattern
* @param pattern The pattern against which the tokens are compared, * @param pattern The pattern against which the tokens are compared,
* e.g. "const" or ") const|volatile| {". * e.g. "const" or ") const|volatile| {".
* @param varname1 Used with pattern "%var1%"
* @return true if given token matches with given pattern * @return true if given token matches with given pattern
* false if given token does not match with given pattern * false if given token does not match with given pattern
*/ */
static bool Match(const Token *tok, const char pattern[], unsigned int varid = 0, const char *varname1[] = 0); static bool Match(const Token *tok, const char pattern[], unsigned int varid = 0);
bool isName() const; bool isName() const;
bool isNumber() const; bool isNumber() const;
bool isBoolean() const; bool isBoolean() const;
bool isStandardType() const; bool isStandardType() const;
static const Token *findmatch(const Token *tok, const char pattern[], const char *varname1[] = 0); static const Token *findmatch(const Token *tok, const char pattern[], unsigned int varId = 0);
static const Token *findmatch(const Token *tok, const char pattern[], unsigned int varId);
/** /**
* Needle is build from multiple alternatives. If one of * Needle is build from multiple alternatives. If one of