Little tweaks to Token::Match code.

This commit is contained in:
Edoardo Prezioso 2012-11-25 15:13:41 +01:00
parent 0f4cdc9e87
commit 9dae536ce3
1 changed files with 35 additions and 48 deletions

View File

@ -565,7 +565,7 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
if (!tok) { if (!tok) {
// If we have no tokens, pattern "!!else" should return true // If we have no tokens, pattern "!!else" should return true
if (p[1] == '!' && p[0] == '!' && p[2] != '\0') { if (p[0] == '!' && p[1] == '!' && p[2] != '\0') {
while (*p && *p != ' ') while (*p && *p != ' ')
++p; ++p;
continue; continue;
@ -584,18 +584,25 @@ 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.
if (p[0] == '%' && p[1]!='|') { if (p[0] == '%') {
bool patternUnderstood = false; ++p;
switch (p[1]) { switch (p[0]) {
case '|':
case '\0':
case ' ':
//simple '%' character
{
multicompare(p, tok->str() == "%", ismulticomp);
}
break;
case 'v': case 'v':
// TODO: %var% should match only for // TODO: %var% should match only for
// variables that have varId != 0, but that needs a lot of // variables that have varId != 0, but that needs a lot of
// work, before that change can be made. // work, before that change can be made.
// Any symbolname.. // Any symbolname..
if (p[4] == '%') { // %var% if (p[3] == '%') { // %var%
p += 5; p += 4;
multicompare(p,tok->isName(),ismulticomp); multicompare(p,tok->isName(),ismulticomp);
patternUnderstood = true;
} else { // %varid% } else { // %varid%
if (varid == 0) { if (varid == 0) {
throw InternalError(tok, "Internal error. Token::Match called with varid 0. Please report this to Cppcheck developers"); throw InternalError(tok, "Internal error. Token::Match called with varid 0. Please report this to Cppcheck developers");
@ -604,99 +611,79 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
if (tok->varId() != varid) if (tok->varId() != varid)
return false; return false;
p += 7; p += 6;
patternUnderstood = true;
} }
break; break;
case 't': case 't':
// Type (%type%) // Type (%type%)
{ {
p += 6; p += 5;
multicompare(p,tok->isName() && tok->varId() == 0 && tok->str() != "delete",ismulticomp); multicompare(p,tok->isName() && tok->varId() == 0 && tok->str() != "delete",ismulticomp);
patternUnderstood = true;
} }
break; break;
case 'a': case 'a':
// Accept any token (%any%) // Accept any token (%any%)
{ {
p += 5; p += 4;
if (p[0] == '|') if (p[0] == '|') {
while (*p && *p != ' ') while (*p && *p != ' ')
++p; ++p;
}
ismulticomp = false; ismulticomp = false;
patternUnderstood = true;
} }
break; break;
case 'n': case 'n':
// Number (%num%) // Number (%num%)
{ {
p += 5; p += 4;
multicompare(p,tok->isNumber(),ismulticomp); multicompare(p,tok->isNumber(),ismulticomp);
patternUnderstood = true;
} }
break; break;
case 'c': case 'c':
// Character (%char%) // Character (%char%)
{ {
p += 6; p += 5;
multicompare(p,tok->type() == eChar,ismulticomp); multicompare(p,tok->type() == eChar,ismulticomp);
patternUnderstood = true;
} }
break; break;
case 's': case 's':
// String (%str%) // String (%str%)
{ {
p += 5; p += 4;
multicompare(p,tok->type() == eString,ismulticomp); multicompare(p,tok->type() == eString,ismulticomp);
patternUnderstood = true;
} }
break; break;
case 'b': case 'b':
// Bool (%bool%) // Bool (%bool%)
{ {
p += 6; p += 5;
multicompare(p,tok->isBoolean(),ismulticomp); multicompare(p,tok->isBoolean(),ismulticomp);
patternUnderstood = true;
} }
break; break;
case 'o': case 'o':
if (p[3] == '%') { ++p;
p += 2; if (p[1] == '%') {
// Or (%or%)
if (p[0] == 'r') {
p += 2;
multicompare(p,tok->str() == "|",ismulticomp)
patternUnderstood = true;
}
// Op (%op%) // Op (%op%)
else if (p[0] == 'p') { if (p[0] == 'p') {
p += 2; p += 2;
multicompare(p,tok->isOp(),ismulticomp); multicompare(p,tok->isOp(),ismulticomp);
patternUnderstood = true; }
// Or (%or%)
else {
p += 2;
multicompare(p,tok->str() == "|",ismulticomp)
} }
} }
// Oror (%oror%) // Oror (%oror%)
else if (p[5] == '%') { else {
p += 6; p += 4;
multicompare(p,tok->str() == "||",ismulticomp); multicompare(p,tok->str() == "||",ismulticomp);
patternUnderstood = true;
} }
break; break;
default: default:
if (firstWordEquals(p, tok->str().c_str())) { //unknown %cmd%, abort
p += tok->str().length(); abort();
if (p[0] == '|') {
while (*p && *p != ' ')
++p;
}
patternUnderstood = true;
}
break;
}
if (!patternUnderstood) {
return false;
} }
} }