Token::multiCompare : allow that %op% is used in multiCompare pattern

This commit is contained in:
Daniel Marjamäki 2011-04-09 18:09:13 +02:00
parent 575cb242c2
commit 6c4f5fc496
3 changed files with 51 additions and 5 deletions

View File

@ -200,8 +200,37 @@ std::string Token::strAt(int index) const
return tok ? tok->_str.c_str() : "";
}
static bool strisop(const char str[])
{
if (str[1] == 0)
{
if (strchr("+-*/%&|^~!<>", *str))
return true;
}
else if (str[2] == 0)
{
if (strcmp(str, "&&")==0 ||
strcmp(str, "||")==0 ||
strcmp(str, "==")==0 ||
strcmp(str, "!=")==0 ||
strcmp(str, ">=")==0 ||
strcmp(str, "<=")==0 ||
strcmp(str, ">>")==0 ||
strcmp(str, "<<")==0)
return true;
}
return false;
}
int Token::multiCompare(const char *haystack, const char *needle)
{
if (strncmp(haystack, "%op%|", 5) == 0)
{
haystack = haystack + 5;
if (strisop(needle))
return 1;
}
bool emptyStringFound = false;
const char *needlePointer = needle;
while (true)
@ -254,6 +283,19 @@ int Token::multiCompare(const char *haystack, const char *needle)
}
++haystack;
if (strncmp(haystack, "%op%", 4) == 0)
{
if (strisop(needle))
return 1;
haystack = haystack + 4;
if (*haystack == '|')
haystack++;
else if (*haystack == ' ' || *haystack == '\0')
return emptyStringFound ? 0 : -1;
else
return -1;
}
}
}

View File

@ -2075,7 +2075,7 @@ bool Tokenizer::tokenize(std::istream &code,
// Combine "- %num%" ..
for (Token *tok = _tokens; tok; tok = tok->next())
{
if (Token::Match(tok, "%any% - %num%") && (tok->isOp() || Token::Match(tok, "?|:|,|(|[|=|return|case")))
if (Token::Match(tok, "?|:|,|(|[|=|return|case|%op% - %num%"))
{
tok->next()->str("-" + tok->strAt(2));
tok->next()->deleteNext();
@ -5594,12 +5594,10 @@ bool Tokenizer::simplifyFunctionReturn()
else if (indentlevel == 0 && Token::Match(tok, "%var% ( ) { return %num% ; }") && tok->str() != ")")
{
const std::string pattern("%any% " + tok->str() + " ( ) %any%");
const std::string pattern("(|[|=|%op% " + tok->str() + " ( ) ;|]|)|%op%");
for (Token *tok2 = _tokens; tok2; tok2 = tok2->next())
{
if (Token::Match(tok2, pattern.c_str()) &&
(tok2->isOp() || Token::Match(tok2, "(|[|=")) &&
(tok2->tokAt(4)->isOp() || Token::Match(tok2->tokAt(4), ";|]|)")))
if (Token::Match(tok2, pattern.c_str()))
{
tok2 = tok2->next();
tok2->str(tok->strAt(5));

View File

@ -89,6 +89,12 @@ private:
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare("abc|def", "a")));
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare("abc|def", "abcd")));
ASSERT_EQUALS(static_cast<unsigned int>(-1), static_cast<unsigned int>(Token::multiCompare("abc|def", "default")));
// %op%
ASSERT_EQUALS(1, Token::multiCompare("one|%op%", "+"));
ASSERT_EQUALS(1, Token::multiCompare("%op%|two", "+"));
ASSERT_EQUALS(-1, Token::multiCompare("one|%op%", "x"));
ASSERT_EQUALS(-1, Token::multiCompare("%op%|two", "x"));
}
void getStrLength()