Token::Match: add a new pattern: '%comp%'.

Token::Match returns true if the token is a comparison operator. It's equivalent to matching '<|<=|==|!=|>=|>' and the tests show that the new pattern speeds up the program execution. Added the new pattern in CheckInternal (and also reordered the %cmd% lists) and in TestToken.
This commit is contained in:
Edoardo Prezioso 2012-12-01 00:47:07 +01:00
parent f3782935c2
commit aeb4531a58
3 changed files with 43 additions and 18 deletions

View File

@ -122,15 +122,17 @@ void CheckInternal::checkMissingPercentCharacter()
static std::set<std::string> magics;
if (magics.empty()) {
magics.insert("%any%");
magics.insert("%var%");
magics.insert("%type%");
magics.insert("%num%");
magics.insert("%bool%");
magics.insert("%char%");
magics.insert("%str%");
magics.insert("%varid%");
magics.insert("%comp%");
magics.insert("%num%");
magics.insert("%op%");
magics.insert("%or%");
magics.insert("%oror%");
magics.insert("%str%");
magics.insert("%type%");
magics.insert("%var%");
magics.insert("%varid%");
}
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
@ -175,16 +177,17 @@ void CheckInternal::checkUnknownPattern()
static std::set<std::string> knownPatterns;
if (knownPatterns.empty()) {
knownPatterns.insert("%any%");
knownPatterns.insert("%var%");
knownPatterns.insert("%type%");
knownPatterns.insert("%num%");
knownPatterns.insert("%bool%");
knownPatterns.insert("%char%");
knownPatterns.insert("%str%");
knownPatterns.insert("%varid%");
knownPatterns.insert("%comp%");
knownPatterns.insert("%num%");
knownPatterns.insert("%op%");
knownPatterns.insert("%or%");
knownPatterns.insert("%oror%");
knownPatterns.insert("%op%");
knownPatterns.insert("%str%");
knownPatterns.insert("%type%");
knownPatterns.insert("%var%");
knownPatterns.insert("%varid%");
}
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {

View File

@ -560,8 +560,8 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
++p;
// No token => Success!
if (*p == 0)
return true;
if (*p == '\0')
break;
if (!tok) {
// If we have no tokens, pattern "!!else" should return true
@ -587,9 +587,9 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
if (p[0] == '%') {
++p;
switch (p[0]) {
case '|':
case '\0':
case ' ':
case '|':
//simple '%' character
{
multicompare(p, tok->str() == "%", ismulticomp);
@ -640,10 +640,18 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
}
break;
case 'c':
// Character (%char%)
{
p += 5;
multicompare(p,tok->type() == eChar,ismulticomp);
p += 1;
// Character (%char%)
if (p[0] == 'h') {
p += 4;
multicompare(p,tok->type() == eChar,ismulticomp);
}
// Comparison operator (%comp%)
else {
p += 4;
multicompare(p,tok->isComparisonOp(),ismulticomp);
}
}
break;
case 's':
@ -661,6 +669,7 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
}
break;
case 'o':
{
++p;
if (p[1] == '%') {
// Op (%op%)
@ -680,7 +689,8 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
p += 4;
multicompare(p,tok->str() == "||",ismulticomp);
}
break;
}
break;
default:
//unknown %cmd%, abort
abort();

View File

@ -57,6 +57,7 @@ private:
TEST_CASE(matchNothingOrAnyNotElse);
TEST_CASE(matchType);
TEST_CASE(matchChar);
TEST_CASE(matchCompOp);
TEST_CASE(matchStr);
TEST_CASE(matchVarid);
TEST_CASE(matchNumeric);
@ -314,6 +315,17 @@ private:
ASSERT_EQUALS(false, Token::Match(noChr.tokens(), "%char%"));
}
void matchCompOp() {
givenACodeSampleToTokenize comp1("<=", true);
ASSERT_EQUALS(true, Token::Match(comp1.tokens(), "%comp%"));
givenACodeSampleToTokenize comp2(">", true);
ASSERT_EQUALS(true, Token::Match(comp2.tokens(), "%comp%"));
givenACodeSampleToTokenize noComp("=", true);
ASSERT_EQUALS(false, Token::Match(noComp.tokens(), "%comp%"));
}
void matchStr() {
givenACodeSampleToTokenize noStr1("abc", true);
ASSERT_EQUALS(false, Token::Match(noStr1.tokens(), "%str%"));