match: skip initial !! patterns if on first token.

This commit is contained in:
Leandro Penz 2009-01-10 21:13:10 +00:00
parent 12a7d1df4b
commit 764e44790f
2 changed files with 19 additions and 0 deletions

View File

@ -167,6 +167,7 @@ bool Token::simpleMatch(const Token *tok, const char pattern[])
bool Token::Match(const Token *tok, const char pattern[], unsigned int varid) bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
{ {
const char *p = pattern; const char *p = pattern;
bool firstpattern = true;
while (*p) while (*p)
{ {
// Skip spaces in pattern.. // Skip spaces in pattern..
@ -198,6 +199,12 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
return false; return false;
} }
// If we are in the first token, we skip all initial !! patterns
if (firstpattern && !tok->previous() && tok->next() && str[1] == '!' && str[0] == '!' && str[2] != '\0')
continue;
firstpattern = false;
// 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 patternIdentified = false; bool patternIdentified = false;

View File

@ -504,6 +504,18 @@ private:
ASSERT_EQUALS(false, Token::Match(tokenizer.tokens(), "!!else something")); ASSERT_EQUALS(false, Token::Match(tokenizer.tokens(), "!!else something"));
} }
{
const std::string code("if ;");
// tokenize..
Tokenizer tokenizer;
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
// Match..
ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "!!return if"));
}
{ {
const std::string code("if ;"); const std::string code("if ;");