Optimizations in CheckInternal:

- Run checks only on executable scopes
- Simplified patterns
This commit is contained in:
PKEuS 2016-07-24 11:45:28 +02:00
parent 161bb50e0c
commit f44d9d5c9b
1 changed files with 185 additions and 160 deletions

View File

@ -33,7 +33,10 @@ namespace {
void CheckInternal::checkTokenMatchPatterns()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
continue;
@ -72,16 +75,20 @@ void CheckInternal::checkTokenMatchPatterns()
complex = true;
break;
}
index = pattern.find('%', index+1);
index = pattern.find('%', index + 1);
}
if (!complex)
simplePatternError(tok, pattern, funcname);
}
}
}
void CheckInternal::checkTokenSimpleMatchPatterns()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (!Token::simpleMatch(tok, "Token :: simpleMatch (") && !Token::simpleMatch(tok, "Token :: findsimplematch ("))
continue;
@ -134,14 +141,15 @@ void CheckInternal::checkTokenSimpleMatchPatterns()
// Check for real errors
if (pattern.length() > 1) {
for (size_t i = 0; i < pattern.length() - 1; i++) {
if (pattern[i] == '%' && pattern[i + 1] != ' ')
for (size_t j = 0; j < pattern.length() - 1; j++) {
if (pattern[j] == '%' && pattern[j + 1] != ' ')
complexPatternError(tok, pattern, funcname);
else if (pattern[i] == '!' && pattern[i + 1] == '!')
else if (pattern[j] == '!' && pattern[j + 1] == '!')
complexPatternError(tok, pattern, funcname);
}
}
}
}
}
namespace {
@ -164,7 +172,10 @@ namespace {
void CheckInternal::checkMissingPercentCharacter()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
continue;
@ -179,7 +190,7 @@ void CheckInternal::checkMissingPercentCharacter()
std::set<std::string>::const_iterator magic, magics_end = magics.end();
for (magic = magics.begin(); magic != magics_end; ++magic) {
const std::string broken_magic = (*magic).substr(0, (*magic).size()-1);
const std::string broken_magic = (*magic).substr(0, (*magic).size() - 1);
std::string::size_type pos = 0;
while ((pos = pattern.find(broken_magic, pos)) != std::string::npos) {
@ -199,6 +210,7 @@ void CheckInternal::checkMissingPercentCharacter()
}
}
}
}
}
namespace {
@ -222,7 +234,10 @@ namespace {
void CheckInternal::checkUnknownPattern()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
continue;
@ -234,54 +249,63 @@ void CheckInternal::checkUnknownPattern()
const std::string pattern = pattern_tok->strValue();
bool inBrackets = false;
for (std::string::size_type i = 0; i < pattern.length()-1; i++) {
if (pattern[i] == '[' && (i == 0 || pattern[i-1] == ' '))
for (std::string::size_type j = 0; j < pattern.length() - 1; j++) {
if (pattern[j] == '[' && (j == 0 || pattern[j - 1] == ' '))
inBrackets = true;
else if (pattern[i] == ']')
else if (pattern[j] == ']')
inBrackets = false;
else if (pattern[i] == '%' && pattern[i+1] != ' ' && pattern[i+1] != '|' && !inBrackets) {
const std::string::size_type end = pattern.find('%', i+1);
else if (pattern[j] == '%' && pattern[j + 1] != ' ' && pattern[j + 1] != '|' && !inBrackets) {
const std::string::size_type end = pattern.find('%', j + 1);
if (end != std::string::npos) {
const std::string s = pattern.substr(i, end-i+1);
const std::string s = pattern.substr(j, end - j + 1);
if (knownPatterns.find(s) == knownPatterns.end())
unknownPatternError(tok, s);
}
}
}
}
}
}
void CheckInternal::checkRedundantNextPrevious()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, ". previous ( ) . next|tokAt|strAt|linkAt (") || Token::Match(tok, ". next ( ) . previous|tokAt|strAt|linkAt (") ||
(Token::simpleMatch(tok, ". tokAt (") && Token::Match(tok->linkAt(2), ") . previous|next|tokAt|strAt|linkAt|str|link ("))) {
const std::string& func1 = tok->strAt(1);
const std::string& func2 = tok->linkAt(2)->strAt(2);
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (tok->str() != ".")
continue;
tok = tok->next();
if ((func2 == "previous" || func2 == "next" || func2 == "str" || func2 == "link") && tok->linkAt(2)->strAt(4) != ")")
if (Token::Match(tok, "previous ( ) . next|tokAt|strAt|linkAt (") || Token::Match(tok, "next ( ) . previous|tokAt|strAt|linkAt (") ||
(Token::simpleMatch(tok, "tokAt (") && Token::Match(tok->linkAt(1), ") . previous|next|tokAt|strAt|linkAt|str|link ("))) {
const std::string& func1 = tok->str();
const std::string& func2 = tok->linkAt(1)->strAt(2);
if ((func2 == "previous" || func2 == "next" || func2 == "str" || func2 == "link") && tok->linkAt(1)->strAt(4) != ")")
continue;
redundantNextPreviousError(tok, func1, func2);
} else if (Token::Match(tok, ". next|previous ( ) . next|previous ( ) . next|previous|linkAt|strAt|link|str (")) {
const std::string& func1 = tok->strAt(1);
const std::string& func2 = tok->strAt(9);
} else if (Token::Match(tok, "next|previous ( ) . next|previous ( ) . next|previous|linkAt|strAt|link|str (")) {
const std::string& func1 = tok->str();
const std::string& func2 = tok->strAt(8);
if ((func2 == "previous" || func2 == "next" || func2 == "str" || func2 == "link") && tok->strAt(11) != ")")
if ((func2 == "previous" || func2 == "next" || func2 == "str" || func2 == "link") && tok->strAt(10) != ")")
continue;
redundantNextPreviousError(tok, func1, func2);
}
}
}
}
void CheckInternal::checkExtraWhitespace()
{
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (!Token::simpleMatch(tok, "Token :: simpleMatch (") &&
!Token::simpleMatch(tok, "Token :: findsimplematch (") &&
!Token::simpleMatch(tok, "Token :: Match (") &&
!Token::simpleMatch(tok, "Token :: findmatch ("))
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (!Token::Match(tok, "Token :: simpleMatch|findsimplematch|Match|findmatch ("))
continue;
const std::string& funcname = tok->strAt(2);
@ -299,6 +323,7 @@ void CheckInternal::checkExtraWhitespace()
if (pattern.find(" ") != std::string::npos)
extraWhitespaceError(tok, pattern, funcname);
}
}
}
void CheckInternal::multiComparePatternError(const Token* tok, const std::string& pattern, const std::string &funcname)