From aeb4531a58da736fd35511be0820a44ae05dffee Mon Sep 17 00:00:00 2001 From: Edoardo Prezioso Date: Sat, 1 Dec 2012 00:47:07 +0100 Subject: [PATCH] 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. --- lib/checkinternal.cpp | 25 ++++++++++++++----------- lib/token.cpp | 24 +++++++++++++++++------- test/testtoken.cpp | 12 ++++++++++++ 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/lib/checkinternal.cpp b/lib/checkinternal.cpp index 21c192163..c65fa03cf 100644 --- a/lib/checkinternal.cpp +++ b/lib/checkinternal.cpp @@ -122,15 +122,17 @@ void CheckInternal::checkMissingPercentCharacter() static std::set 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 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()) { diff --git a/lib/token.cpp b/lib/token.cpp index 8a1af1e0b..3cf31b9de 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -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(); diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 80735bd07..030a876a3 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -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%"));