Refactoring: %var1% complitely removed. Execution time increased from 4,7 to 6,2 seconds.
This commit is contained in:
parent
49b864b1fd
commit
e721383d01
|
@ -354,6 +354,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
|
|||
const char *varnames[2];
|
||||
varnames[0] = varname;
|
||||
varnames[1] = 0;
|
||||
std::string varnameStr = varname;
|
||||
|
||||
Token *rethead = 0, *rettail = 0;
|
||||
#define addtoken(_str) \
|
||||
|
@ -405,7 +406,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
|
|||
if (parlevel == 0 && tok->str() == ";")
|
||||
addtoken(";");
|
||||
|
||||
if (Token::Match(tok, "[(;{}] %var1% =", 0, varnames))
|
||||
if (Token::Match(tok, std::string("[(;{}] " + varnameStr + " =").c_str()))
|
||||
{
|
||||
AllocType alloc = GetAllocationType(tok->tokAt(3));
|
||||
bool realloc = false;
|
||||
|
@ -480,9 +481,9 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
|
|||
}
|
||||
|
||||
// if else switch
|
||||
if (Token::Match(tok, "if ( %var1% )", 0, varnames) ||
|
||||
Token::Match(tok, "if ( %var1% != 0 )", 0, varnames) ||
|
||||
Token::Match(tok, "if ( 0 != %var1% )", 0, varnames))
|
||||
if (Token::simpleMatch(tok, std::string("if ( " + varnameStr + " )").c_str()) ||
|
||||
Token::simpleMatch(tok, std::string("if ( " + varnameStr + " != 0 )").c_str()) ||
|
||||
Token::simpleMatch(tok, std::string("if ( 0 != " + varnameStr + " )").c_str()))
|
||||
{
|
||||
addtoken("if(var)");
|
||||
|
||||
|
@ -509,7 +510,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
|
|||
if (parlevel <= 0)
|
||||
break;
|
||||
}
|
||||
if (Token::Match(tok2, "fclose ( %var1% )", 0, varnames))
|
||||
if (Token::simpleMatch(tok2, std::string("fclose ( " + varnameStr + " )").c_str()))
|
||||
{
|
||||
addtoken("dealloc");
|
||||
addtoken(";");
|
||||
|
@ -517,8 +518,8 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
|
|||
break;
|
||||
}
|
||||
if ((tok2->str() != ".") &&
|
||||
Token::Match(tok2->next(), "%var1%", 0, varnames) &&
|
||||
!Token::Match(tok2->next(), "%var1% .", 0, varnames))
|
||||
Token::simpleMatch(tok2->next(), varnameStr.c_str()) &&
|
||||
!Token::simpleMatch(tok2->next(), std::string(varnameStr + " .").c_str()))
|
||||
{
|
||||
dep = true;
|
||||
break;
|
||||
|
@ -572,8 +573,8 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
|
|||
if (tok->str() == "return")
|
||||
{
|
||||
addtoken("return");
|
||||
if (Token::Match(tok, "return %var1%", 0, varnames) ||
|
||||
Token::Match(tok, "return & %var1%", 0, varnames))
|
||||
if (Token::simpleMatch(tok, std::string("return " + varnameStr).c_str()) ||
|
||||
Token::simpleMatch(tok, std::string("return & " + varnameStr).c_str()))
|
||||
addtoken("use");
|
||||
if (Token::simpleMatch(tok->next(), "("))
|
||||
{
|
||||
|
@ -596,13 +597,13 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
|
|||
addtoken(tok->strAt(0));
|
||||
|
||||
// Assignment..
|
||||
if (Token::Match(tok, "[)=] %var1% [+;)]", 0, varnames) ||
|
||||
Token::Match(tok, "%var1% +=|-=", 0, varnames) ||
|
||||
Token::Match(tok, "+=|<< %var1% ;", 0, varnames))
|
||||
if (Token::Match(tok, std::string("[)=] " + varnameStr + " [+;)]").c_str()) ||
|
||||
Token::Match(tok, std::string(varnameStr + " +=|-=").c_str()) ||
|
||||
Token::Match(tok, std::string("+=|<< " + varnameStr + " ;").c_str()))
|
||||
{
|
||||
addtoken("use");
|
||||
}
|
||||
else if (Token::Match(tok, "[;{}=(,+-*/] %var1% [", 0, varnames))
|
||||
else if (Token::Match(tok, std::string("[;{}=(,+-*/] " + varnameStr + " [").c_str()))
|
||||
{
|
||||
addtoken("use_");
|
||||
}
|
||||
|
@ -642,11 +643,11 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
|
|||
}
|
||||
|
||||
// Linux lists..
|
||||
if (Token::Match(tok, "[=(,] & %var1% [.[]", 0, varnames))
|
||||
if (Token::Match(tok, std::string("[=(,] & " + varnameStr + " [.[]").c_str()))
|
||||
{
|
||||
addtoken("&use");
|
||||
}
|
||||
else if (Token::Match(tok, "[=(,] & %var1% [,)]", 0, varnames))
|
||||
else if (Token::Match(tok, std::string("[=(,] & " + varnameStr + " [,)]").c_str()))
|
||||
{
|
||||
addtoken("&use2");
|
||||
}
|
||||
|
|
38
token.cpp
38
token.cpp
|
@ -164,7 +164,7 @@ bool Token::simpleMatch(const Token *tok, const char pattern[])
|
|||
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;
|
||||
while (*p)
|
||||
|
@ -218,32 +218,6 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid, co
|
|||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
for (; tok; tok = tok->next())
|
||||
|
|
7
token.h
7
token.h
|
@ -92,7 +92,6 @@ public:
|
|||
* "%num%" Any numeric token, e.g. "23"
|
||||
* "%bool%" true or false
|
||||
* "%str%" Any token starting with "-character (C-string).
|
||||
* "%var1%" Match with parameter varname1
|
||||
* "%varid%" Match with parameter varid
|
||||
* "[abc]" Any of the characters 'a' or 'b' or 'c'
|
||||
* "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 pattern The pattern against which the tokens are compared,
|
||||
* e.g. "const" or ") const|volatile| {".
|
||||
* @param varname1 Used with pattern "%var1%"
|
||||
* @return true if given token matches 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 isNumber() const;
|
||||
bool isBoolean() 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);
|
||||
static const Token *findmatch(const Token *tok, const char pattern[], unsigned int varId = 0);
|
||||
|
||||
/**
|
||||
* Needle is build from multiple alternatives. If one of
|
||||
|
|
Loading…
Reference in New Issue