Skip to content

Commit aeb4531

Browse files
committed
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.
1 parent f378293 commit aeb4531

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

lib/checkinternal.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,17 @@ void CheckInternal::checkMissingPercentCharacter()
122122
static std::set<std::string> magics;
123123
if (magics.empty()) {
124124
magics.insert("%any%");
125-
magics.insert("%var%");
126-
magics.insert("%type%");
127-
magics.insert("%num%");
128125
magics.insert("%bool%");
129126
magics.insert("%char%");
130-
magics.insert("%str%");
131-
magics.insert("%varid%");
127+
magics.insert("%comp%");
128+
magics.insert("%num%");
129+
magics.insert("%op%");
132130
magics.insert("%or%");
133131
magics.insert("%oror%");
132+
magics.insert("%str%");
133+
magics.insert("%type%");
134+
magics.insert("%var%");
135+
magics.insert("%varid%");
134136
}
135137

136138
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
@@ -175,16 +177,17 @@ void CheckInternal::checkUnknownPattern()
175177
static std::set<std::string> knownPatterns;
176178
if (knownPatterns.empty()) {
177179
knownPatterns.insert("%any%");
178-
knownPatterns.insert("%var%");
179-
knownPatterns.insert("%type%");
180-
knownPatterns.insert("%num%");
181180
knownPatterns.insert("%bool%");
182181
knownPatterns.insert("%char%");
183-
knownPatterns.insert("%str%");
184-
knownPatterns.insert("%varid%");
182+
knownPatterns.insert("%comp%");
183+
knownPatterns.insert("%num%");
184+
knownPatterns.insert("%op%");
185185
knownPatterns.insert("%or%");
186186
knownPatterns.insert("%oror%");
187-
knownPatterns.insert("%op%");
187+
knownPatterns.insert("%str%");
188+
knownPatterns.insert("%type%");
189+
knownPatterns.insert("%var%");
190+
knownPatterns.insert("%varid%");
188191
}
189192

190193
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {

lib/token.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
560560
++p;
561561

562562
// No token => Success!
563-
if (*p == 0)
564-
return true;
563+
if (*p == '\0')
564+
break;
565565

566566
if (!tok) {
567567
// 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)
587587
if (p[0] == '%') {
588588
++p;
589589
switch (p[0]) {
590-
case '|':
591590
case '\0':
592591
case ' ':
592+
case '|':
593593
//simple '%' character
594594
{
595595
multicompare(p, tok->str() == "%", ismulticomp);
@@ -640,10 +640,18 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
640640
}
641641
break;
642642
case 'c':
643-
// Character (%char%)
644643
{
645-
p += 5;
646-
multicompare(p,tok->type() == eChar,ismulticomp);
644+
p += 1;
645+
// Character (%char%)
646+
if (p[0] == 'h') {
647+
p += 4;
648+
multicompare(p,tok->type() == eChar,ismulticomp);
649+
}
650+
// Comparison operator (%comp%)
651+
else {
652+
p += 4;
653+
multicompare(p,tok->isComparisonOp(),ismulticomp);
654+
}
647655
}
648656
break;
649657
case 's':
@@ -661,6 +669,7 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
661669
}
662670
break;
663671
case 'o':
672+
{
664673
++p;
665674
if (p[1] == '%') {
666675
// Op (%op%)
@@ -680,7 +689,8 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
680689
p += 4;
681690
multicompare(p,tok->str() == "||",ismulticomp);
682691
}
683-
break;
692+
}
693+
break;
684694
default:
685695
//unknown %cmd%, abort
686696
abort();

test/testtoken.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class TestToken : public TestFixture {
5757
TEST_CASE(matchNothingOrAnyNotElse);
5858
TEST_CASE(matchType);
5959
TEST_CASE(matchChar);
60+
TEST_CASE(matchCompOp);
6061
TEST_CASE(matchStr);
6162
TEST_CASE(matchVarid);
6263
TEST_CASE(matchNumeric);
@@ -314,6 +315,17 @@ class TestToken : public TestFixture {
314315
ASSERT_EQUALS(false, Token::Match(noChr.tokens(), "%char%"));
315316
}
316317

318+
void matchCompOp() {
319+
givenACodeSampleToTokenize comp1("<=", true);
320+
ASSERT_EQUALS(true, Token::Match(comp1.tokens(), "%comp%"));
321+
322+
givenACodeSampleToTokenize comp2(">", true);
323+
ASSERT_EQUALS(true, Token::Match(comp2.tokens(), "%comp%"));
324+
325+
givenACodeSampleToTokenize noComp("=", true);
326+
ASSERT_EQUALS(false, Token::Match(noComp.tokens(), "%comp%"));
327+
}
328+
317329
void matchStr() {
318330
givenACodeSampleToTokenize noStr1("abc", true);
319331
ASSERT_EQUALS(false, Token::Match(noStr1.tokens(), "%str%"));

0 commit comments

Comments
 (0)