Fixed #2665 (Slightly sped-up how Token::Match() parses patterns)

This commit is contained in:
Zachary Blair 2011-03-22 00:15:12 -07:00
parent c043e6e1a2
commit f89f0c748f
1 changed files with 80 additions and 59 deletions

View File

@ -384,21 +384,44 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
// Compare the first character of the string for optimization reasons // Compare the first character of the string for optimization reasons
// before doing more detailed checks. // before doing more detailed checks.
bool patternUnderstood = false;
if (p[0] == '%') if (p[0] == '%')
{ {
// TODO: %var% should match only for switch (p[1])
// variables that have varId != 0, but that needs a lot of
// work, before that change can be made.
// Any symbolname..
if (firstWordEquals(p, "%var%") == 0)
{ {
if (!tok->isName()) case 'v':
return false; // TODO: %var% should match only for
p += 5; // variables that have varId != 0, but that needs a lot of
} // work, before that change can be made.
// Any symbolname..
if (p[4] == '%') // %var%
{
if (!tok->isName())
return false;
p += 5;
patternUnderstood = true;
}
else // %varid%
{
if (varid == 0)
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::error,
"Internal error. Token::Match called with varid 0.",
"cppcheckError");
Check::reportError(errmsg);
}
// Type.. if (tok->varId() != varid)
else if (firstWordEquals(p, "%type%") == 0) return false;
p += 7;
patternUnderstood = true;
}
break;
case 't':
// Type (%type%)
{ {
if (!tok->isName()) if (!tok->isName())
return false; return false;
@ -410,73 +433,71 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
return false; return false;
p += 6; p += 6;
patternUnderstood = true;
} }
break;
// Accept any token case 'a':
else if (firstWordEquals(p, "%any%") == 0) // Accept any token (%any%)
{ {
p += 5; p += 5;
patternUnderstood = true;
} }
break;
else if (firstWordEquals(p, "%varid%") == 0) case 'n':
{ // Number (%num)
if (varid == 0)
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
const ErrorLogger::ErrorMessage errmsg(locationList,
Severity::error,
"Internal error. Token::Match called with varid 0.",
"cppcheckError");
Check::reportError(errmsg);
}
if (tok->varId() != varid)
return false;
p += 7;
}
else if (firstWordEquals(p, "%num%") == 0)
{ {
if (!tok->isNumber()) if (!tok->isNumber())
return false; return false;
p += 5; p += 5;
patternUnderstood = true;
} }
break;
else if (firstWordEquals(p, "%bool%") == 0) case 's':
{ // String (%str%)
if (!tok->isBoolean())
return false;
p += 6;
}
else if (firstWordEquals(p, "%str%") == 0)
{ {
if (tok->_str[0] != '\"') if (tok->_str[0] != '\"')
return false; return false;
p += 5; p += 5;
patternUnderstood = true;
} }
break;
else if (firstWordEquals(p, "%or%") == 0) case 'b':
// Bool (%bool%)
{ {
if (tok->str() != "|") if (!tok->isBoolean())
return false;
p += 4;
}
else if (firstWordEquals(p, "%oror%") == 0)
{
if (tok->str() != "||")
return false; return false;
p += 6; p += 6;
patternUnderstood = true;
}
break;
case 'o':
// Or (%or%)
if (p[3] == '%')
{
if (tok->str() != "|")
return false;
p += 4;
patternUnderstood = true;
}
// Oror (%oror%)
else
{
if (tok->str() != "||")
return false;
p += 6;
patternUnderstood = true;
}
break;
default:
if (firstWordEquals(p, tok->_str.c_str()))
{
p += tok->_str.length();
patternUnderstood = true;
}
break;
} }
else if (firstWordEquals(p, tok->_str.c_str())) if (!patternUnderstood)
{
p += tok->_str.length();
}
else
{ {
return false; return false;
} }