Refactorized inefficient usage of Token::findmatch in two checks in checkother.cpp

This commit is contained in:
Philipp Kloke 2014-04-11 11:56:25 +02:00
parent 20c3a5baf0
commit cf30fab413
1 changed files with 17 additions and 19 deletions

View File

@ -1059,22 +1059,20 @@ void CheckOther::checkSuspiciousCaseInSwitch()
for (const Token* tok = i->classStart->next(); tok != i->classEnd; tok = tok->next()) { for (const Token* tok = i->classStart->next(); tok != i->classEnd; tok = tok->next()) {
if (tok->str() == "case") { if (tok->str() == "case") {
const Token* end = 0; const Token* finding = nullptr;
for (const Token* tok2 = tok->next(); tok2; tok2 = tok2->next()) { for (const Token* tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->str() == ":") { if (tok2->str() == ":")
end = tok2;
break; break;
} if (Token::Match(tok2, "[;}{]"))
if (Token::Match(tok2, "[?;}{]")) {
break; break;
} if (tok2->str() == "?")
} finding = nullptr;
if (end) { if (Token::Match(tok2, "&&|%oror%"))
const Token* finding = Token::findmatch(tok->next(), "&&|%oror%", end); finding = tok2;
if (finding)
suspiciousCaseInSwitchError(tok, finding->str());
} }
if (finding)
suspiciousCaseInSwitchError(finding, finding->str());
} }
} }
} }
@ -1108,20 +1106,20 @@ void CheckOther::checkSuspiciousEqualityComparison()
// for (i == 2; i < 10; i++) // for (i == 2; i < 10; i++)
// or // or
// for (i = 0; i < 10; i == a) // for (i = 0; i < 10; i == a)
const Token* tok2 = Token::findmatch(openParen, "[;(] %var% == %any% [;)]", closeParen); if (Token::Match(openParen->next(), "%var% =="))
if (tok2 && (tok2 == openParen || tok2->tokAt(4) == closeParen)) { suspiciousEqualityComparisonError(openParen->tokAt(2));
suspiciousEqualityComparisonError(tok2->tokAt(2)); if (Token::Match(closeParen->tokAt(-2), "== %any%"))
} suspiciousEqualityComparisonError(closeParen->tokAt(-2));
// Equality comparisons with 0 are simplified to negation. For instance, // Equality comparisons with 0 are simplified to negation. For instance,
// (x == 0) is simplified to (!x), so also check for suspicious negation // (x == 0) is simplified to (!x), so also check for suspicious negation
// in the initialization or increment-decrement parts of the for() loop. // in the initialization or increment-decrement parts of the for() loop.
// For example: // For example:
// for (!i; i < 10; i++) // for (!i; i < 10; i++)
const Token* tok3 = Token::findmatch(openParen, "[;(] ! %var% [;)]", closeParen); if (Token::Match(openParen->next(), "! %var%"))
if (tok3 && (tok3 == openParen || tok3->tokAt(3) == closeParen)) { suspiciousEqualityComparisonError(openParen->next());
suspiciousEqualityComparisonError(tok3->tokAt(2)); if (Token::Match(closeParen->tokAt(-2), "! %var%"))
} suspiciousEqualityComparisonError(closeParen->tokAt(-2));
// Skip over for() loop conditions because "for (;running==1;)" // Skip over for() loop conditions because "for (;running==1;)"
// is a bit strange, but not necessarily incorrect. // is a bit strange, but not necessarily incorrect.