speed up checks by caching commonly looked up stuff in the symbol database (checkIncorrectLogicOperator). Ticket: #4266.

This commit is contained in:
Robert Reif 2012-11-13 18:30:33 +01:00 committed by Daniel Marjamäki
parent f82eff6589
commit 6578b78077
2 changed files with 196 additions and 184 deletions

View File

@ -1170,28 +1170,36 @@ void CheckOther::checkIncorrectLogicOperator()
if (!_settings->isEnabled("style"))
return;
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size();
for (std::size_t ii = 0; ii < functions; ++ii) {
const Scope * scope = symbolDatabase->functionScopes[ii];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
// Find a pair of comparison expressions with or without parenthesis
// with a shared variable and constants and with a logical operator between them.
// e.g. if (x != 3 || x != 4)
const Token *term1Tok = NULL, *term2Tok = NULL;
const Token *op1Tok = NULL, *op2Tok = NULL, *op3Tok = NULL, *nextTok = NULL;
if (Token::Match(tok, "( %any% !=|==|<|>|>=|<= %any% ) &&|%oror%")) {
if (Token::Match(tok, "( %any% %op% %any% ) &&|%oror%") &&
tok->tokAt(2)->isComparisonOp()) {
term1Tok = tok->next();
op1Tok = tok->tokAt(2);
op2Tok = tok->tokAt(5);
} else if (Token::Match(tok, "%any% !=|==|<|>|>=|<= %any% &&|%oror%")) {
} else if (Token::Match(tok, "%any% %op% %any% &&|%oror%") &&
tok->tokAt(1)->isComparisonOp()) {
term1Tok = tok;
op1Tok = tok->next();
op2Tok = tok->tokAt(3);
}
if (op2Tok) {
if (Token::Match(op2Tok->next(), "( %any% !=|==|<|>|>=|<= %any% ) %any%")) {
if (Token::Match(op2Tok->next(), "( %any% %op% %any% ) %any%") &&
op2Tok->tokAt(3)->isComparisonOp()) {
term2Tok = op2Tok->tokAt(2);
op3Tok = op2Tok->tokAt(3);
nextTok = op2Tok->tokAt(6);
} else if (Token::Match(op2Tok->next(), "%any% !=|==|<|>|>=|<= %any% %any%")) {
} else if (Token::Match(op2Tok->next(), "%any% %op% %any% %any%") &&
op2Tok->tokAt(2)->isComparisonOp()) {
term2Tok = op2Tok->next();
op3Tok = op2Tok->tokAt(2);
nextTok = op2Tok->tokAt(4);
@ -1365,6 +1373,7 @@ void CheckOther::checkIncorrectLogicOperator()
}
}
}
}
void CheckOther::incorrectLogicOperatorError(const Token *tok, const std::string &condition, bool always)
{

View File

@ -190,6 +190,9 @@ public:
return isOp() ||
_type == eExtendedOp;
}
bool isComparisonOp() const {
return _type == eComparisonOp;
}
bool isAssignmentOp() const {
return _type == eAssignmentOp;
}