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,112 +33,120 @@ namespace {
void CheckInternal::checkTokenMatchPatterns() void CheckInternal::checkTokenMatchPatterns()
{ {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch (")) for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
continue; 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;
const std::string& funcname = tok->strAt(2); const std::string& funcname = tok->strAt(2);
// Get pattern string // Get pattern string
const Token *pattern_tok = tok->tokAt(4)->nextArgument(); const Token *pattern_tok = tok->tokAt(4)->nextArgument();
if (!pattern_tok || pattern_tok->tokType() != Token::eString) if (!pattern_tok || pattern_tok->tokType() != Token::eString)
continue; continue;
const std::string pattern = pattern_tok->strValue(); const std::string pattern = pattern_tok->strValue();
if (pattern.empty()) { if (pattern.empty()) {
simplePatternError(tok, pattern, funcname); simplePatternError(tok, pattern, funcname);
continue; continue;
}
if (pattern.find("||") != std::string::npos || pattern.find(" | ") != std::string::npos || pattern[0] == '|' || (pattern[pattern.length() - 1] == '|' && pattern[pattern.length() - 2] == ' '))
orInComplexPattern(tok, pattern, funcname);
// Check for signs of complex patterns
if (pattern.find_first_of("[|") != std::string::npos)
continue;
else if (pattern.find("!!") != std::string::npos)
continue;
bool complex = false;
size_t index = pattern.find('%');
while (index != std::string::npos) {
if (pattern.length() <= index + 2) {
complex = true;
break;
} }
if (pattern[index + 1] == 'o' && pattern[index + 2] == 'r') // %or% or %oror%
if (pattern.find("||") != std::string::npos || pattern.find(" | ") != std::string::npos || pattern[0] == '|' || (pattern[pattern.length() - 1] == '|' && pattern[pattern.length() - 2] == ' '))
orInComplexPattern(tok, pattern, funcname);
// Check for signs of complex patterns
if (pattern.find_first_of("[|") != std::string::npos)
continue;
else if (pattern.find("!!") != std::string::npos)
continue;
bool complex = false;
size_t index = pattern.find('%');
while (index != std::string::npos) {
if (pattern.length() <= index + 2) {
complex = true;
break;
}
if (pattern[index + 1] == 'o' && pattern[index + 2] == 'r') // %or% or %oror%
index = pattern.find('%', index + 1);
else {
complex = true;
break;
}
index = pattern.find('%', index + 1); index = pattern.find('%', index + 1);
else {
complex = true;
break;
} }
index = pattern.find('%', index+1); if (!complex)
simplePatternError(tok, pattern, funcname);
} }
if (!complex)
simplePatternError(tok, pattern, funcname);
} }
} }
void CheckInternal::checkTokenSimpleMatchPatterns() void CheckInternal::checkTokenSimpleMatchPatterns()
{ {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
if (!Token::simpleMatch(tok, "Token :: simpleMatch (") && !Token::simpleMatch(tok, "Token :: findsimplematch (")) for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
continue; 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;
const std::string& funcname = tok->strAt(2); const std::string& funcname = tok->strAt(2);
// Get pattern string // Get pattern string
const Token *pattern_tok = tok->tokAt(4)->nextArgument(); const Token *pattern_tok = tok->tokAt(4)->nextArgument();
if (!pattern_tok || pattern_tok->tokType() != Token::eString) if (!pattern_tok || pattern_tok->tokType() != Token::eString)
continue; continue;
const std::string pattern = pattern_tok->strValue(); const std::string pattern = pattern_tok->strValue();
if (pattern.empty()) { if (pattern.empty()) {
complexPatternError(tok, pattern, funcname); complexPatternError(tok, pattern, funcname);
continue; continue;
}
// Check for [xyz] usage - but exclude standalone square brackets
unsigned int char_count = 0;
for (std::string::size_type pos = 0; pos < pattern.size(); ++pos) {
char c = pattern[pos];
if (c == ' ') {
char_count = 0;
} else if (c == ']') {
if (char_count > 0) {
complexPatternError(tok, pattern, funcname);
continue;
}
} else {
++char_count;
} }
}
// Check | usage: Count characters before the symbol // Check for [xyz] usage - but exclude standalone square brackets
char_count = 0; unsigned int char_count = 0;
for (std::string::size_type pos = 0; pos < pattern.size(); ++pos) { for (std::string::size_type pos = 0; pos < pattern.size(); ++pos) {
const char c = pattern[pos]; char c = pattern[pos];
if (c == ' ') { if (c == ' ') {
char_count = 0; char_count = 0;
} else if (c == '|') { } else if (c == ']') {
if (char_count > 0) { if (char_count > 0) {
complexPatternError(tok, pattern, funcname); complexPatternError(tok, pattern, funcname);
continue; continue;
}
} else {
++char_count;
} }
} else {
++char_count;
} }
}
// Check for real errors // Check | usage: Count characters before the symbol
if (pattern.length() > 1) { char_count = 0;
for (size_t i = 0; i < pattern.length() - 1; i++) { for (std::string::size_type pos = 0; pos < pattern.size(); ++pos) {
if (pattern[i] == '%' && pattern[i + 1] != ' ') const char c = pattern[pos];
complexPatternError(tok, pattern, funcname);
else if (pattern[i] == '!' && pattern[i + 1] == '!') if (c == ' ') {
complexPatternError(tok, pattern, funcname); char_count = 0;
} else if (c == '|') {
if (char_count > 0) {
complexPatternError(tok, pattern, funcname);
continue;
}
} else {
++char_count;
}
}
// Check for real errors
if (pattern.length() > 1) {
for (size_t j = 0; j < pattern.length() - 1; j++) {
if (pattern[j] == '%' && pattern[j + 1] != ' ')
complexPatternError(tok, pattern, funcname);
else if (pattern[j] == '!' && pattern[j + 1] == '!')
complexPatternError(tok, pattern, funcname);
}
} }
} }
} }
@ -164,38 +172,42 @@ namespace {
void CheckInternal::checkMissingPercentCharacter() void CheckInternal::checkMissingPercentCharacter()
{ {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch (")) for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
continue; 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;
const std::string& funcname = tok->strAt(2); const std::string& funcname = tok->strAt(2);
// Get pattern string // Get pattern string
const Token *pattern_tok = tok->tokAt(4)->nextArgument(); const Token *pattern_tok = tok->tokAt(4)->nextArgument();
if (!pattern_tok || pattern_tok->tokType() != Token::eString) if (!pattern_tok || pattern_tok->tokType() != Token::eString)
continue; continue;
const std::string pattern = pattern_tok->strValue(); const std::string pattern = pattern_tok->strValue();
std::set<std::string>::const_iterator magic, magics_end = magics.end(); std::set<std::string>::const_iterator magic, magics_end = magics.end();
for (magic = magics.begin(); magic != magics_end; ++magic) { 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; std::string::size_type pos = 0;
while ((pos = pattern.find(broken_magic, pos)) != std::string::npos) { while ((pos = pattern.find(broken_magic, pos)) != std::string::npos) {
// Check if it's the full pattern // Check if it's the full pattern
if (pattern.find(*magic, pos) != pos) { if (pattern.find(*magic, pos) != pos) {
// Known whitelist of substrings // Known whitelist of substrings
if ((broken_magic == "%var" && pattern.find("%varid%", pos) == pos) || if ((broken_magic == "%var" && pattern.find("%varid%", pos) == pos) ||
(broken_magic == "%or" && pattern.find("%oror%", pos) == pos)) { (broken_magic == "%or" && pattern.find("%oror%", pos) == pos)) {
++pos; ++pos;
continue; continue;
}
missingPercentCharacterError(tok, pattern, funcname);
} }
missingPercentCharacterError(tok, pattern, funcname); ++pos;
} }
++pos;
} }
} }
} }
@ -222,29 +234,33 @@ namespace {
void CheckInternal::checkUnknownPattern() void CheckInternal::checkUnknownPattern()
{ {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch (")) for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
continue; 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;
// Get pattern string // Get pattern string
const Token *pattern_tok = tok->tokAt(4)->nextArgument(); const Token *pattern_tok = tok->tokAt(4)->nextArgument();
if (!pattern_tok || pattern_tok->tokType() != Token::eString) if (!pattern_tok || pattern_tok->tokType() != Token::eString)
continue; continue;
const std::string pattern = pattern_tok->strValue(); const std::string pattern = pattern_tok->strValue();
bool inBrackets = false; bool inBrackets = false;
for (std::string::size_type i = 0; i < pattern.length()-1; i++) { for (std::string::size_type j = 0; j < pattern.length() - 1; j++) {
if (pattern[i] == '[' && (i == 0 || pattern[i-1] == ' ')) if (pattern[j] == '[' && (j == 0 || pattern[j - 1] == ' '))
inBrackets = true; inBrackets = true;
else if (pattern[i] == ']') else if (pattern[j] == ']')
inBrackets = false; inBrackets = false;
else if (pattern[i] == '%' && pattern[i+1] != ' ' && pattern[i+1] != '|' && !inBrackets) { else if (pattern[j] == '%' && pattern[j + 1] != ' ' && pattern[j + 1] != '|' && !inBrackets) {
const std::string::size_type end = pattern.find('%', i+1); const std::string::size_type end = pattern.find('%', j + 1);
if (end != std::string::npos) { 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()) if (knownPatterns.find(s) == knownPatterns.end())
unknownPatternError(tok, s); unknownPatternError(tok, s);
}
} }
} }
} }
@ -253,51 +269,60 @@ void CheckInternal::checkUnknownPattern()
void CheckInternal::checkRedundantNextPrevious() void CheckInternal::checkRedundantNextPrevious()
{ {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
if (Token::Match(tok, ". previous ( ) . next|tokAt|strAt|linkAt (") || Token::Match(tok, ". next ( ) . previous|tokAt|strAt|linkAt (") || for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
(Token::simpleMatch(tok, ". tokAt (") && Token::Match(tok->linkAt(2), ") . previous|next|tokAt|strAt|linkAt|str|link ("))) { const Scope * scope = symbolDatabase->functionScopes[i];
const std::string& func1 = tok->strAt(1); for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
const std::string& func2 = tok->linkAt(2)->strAt(2); if (tok->str() != ".")
if ((func2 == "previous" || func2 == "next" || func2 == "str" || func2 == "link") && tok->linkAt(2)->strAt(4) != ")")
continue; continue;
tok = tok->next();
redundantNextPreviousError(tok, func1, func2); if (Token::Match(tok, "previous ( ) . next|tokAt|strAt|linkAt (") || Token::Match(tok, "next ( ) . previous|tokAt|strAt|linkAt (") ||
} else if (Token::Match(tok, ". next|previous ( ) . next|previous ( ) . next|previous|linkAt|strAt|link|str (")) { (Token::simpleMatch(tok, "tokAt (") && Token::Match(tok->linkAt(1), ") . previous|next|tokAt|strAt|linkAt|str|link ("))) {
const std::string& func1 = tok->strAt(1); const std::string& func1 = tok->str();
const std::string& func2 = tok->strAt(9); const std::string& func2 = tok->linkAt(1)->strAt(2);
if ((func2 == "previous" || func2 == "next" || func2 == "str" || func2 == "link") && tok->strAt(11) != ")") if ((func2 == "previous" || func2 == "next" || func2 == "str" || func2 == "link") && tok->linkAt(1)->strAt(4) != ")")
continue; continue;
redundantNextPreviousError(tok, func1, func2); redundantNextPreviousError(tok, func1, func2);
} 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(10) != ")")
continue;
redundantNextPreviousError(tok, func1, func2);
}
} }
} }
} }
void CheckInternal::checkExtraWhitespace() void CheckInternal::checkExtraWhitespace()
{ {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
if (!Token::simpleMatch(tok, "Token :: simpleMatch (") && for (std::size_t i = 0; i < symbolDatabase->functionScopes.size(); ++i) {
!Token::simpleMatch(tok, "Token :: findsimplematch (") && const Scope * scope = symbolDatabase->functionScopes[i];
!Token::simpleMatch(tok, "Token :: Match (") && for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
!Token::simpleMatch(tok, "Token :: findmatch (")) if (!Token::Match(tok, "Token :: simpleMatch|findsimplematch|Match|findmatch ("))
continue; continue;
const std::string& funcname = tok->strAt(2); const std::string& funcname = tok->strAt(2);
// Get pattern string // Get pattern string
const Token *pattern_tok = tok->tokAt(4)->nextArgument(); const Token *pattern_tok = tok->tokAt(4)->nextArgument();
if (!pattern_tok || pattern_tok->tokType() != Token::eString) if (!pattern_tok || pattern_tok->tokType() != Token::eString)
continue; continue;
const std::string pattern = pattern_tok->strValue(); const std::string pattern = pattern_tok->strValue();
if (!pattern.empty() && (pattern[0] == ' ' || *pattern.rbegin() == ' ')) if (!pattern.empty() && (pattern[0] == ' ' || *pattern.rbegin() == ' '))
extraWhitespaceError(tok, pattern, funcname); extraWhitespaceError(tok, pattern, funcname);
// two whitespaces or more // two whitespaces or more
if (pattern.find(" ") != std::string::npos) if (pattern.find(" ") != std::string::npos)
extraWhitespaceError(tok, pattern, funcname); extraWhitespaceError(tok, pattern, funcname);
}
} }
} }