Refactorization: Use AST in CheckOther::checkRedundantCopy(), CheckOther::clarifyStatement() and CheckPostfixOperator::postfixOperator().

This commit is contained in:
PKEuS 2014-05-18 12:52:15 +02:00
parent 683d0b7e82
commit 20753f4697
2 changed files with 12 additions and 49 deletions

View File

@ -430,19 +430,7 @@ void CheckOther::clarifyStatement()
tok2=tok2->previous(); tok2=tok2->previous();
if (Token::Match(tok2, "[{};]")) { if (Token::Match(tok2, "[{};]")) {
tok = tok->tokAt(2); tok = tok->astOperand1();
for (;;) {
if (tok->str() == "[")
tok = tok->link()->next();
if (Token::Match(tok, ".|:: %var%")) {
if (tok->strAt(2) == "(")
tok = tok->linkAt(2)->next();
else
tok = tok->tokAt(2);
} else
break;
}
if (Token::Match(tok, "++|-- [;,]")) if (Token::Match(tok, "++|-- [;,]"))
clarifyStatementError(tok); clarifyStatementError(tok);
} }
@ -3026,10 +3014,10 @@ void CheckOther::checkModuloAlwaysTrueFalse()
if (!tok->isComparisonOp()) if (!tok->isComparisonOp())
continue; continue;
const Token *num,*modulo; const Token *num,*modulo;
if (Token::simpleMatch(tok->astOperand1(),"%") && Token::Match(tok->astOperand2(),"%num%")) { if (Token::simpleMatch(tok->astOperand1(), "%") && Token::Match(tok->astOperand2(), "%num%")) {
modulo = tok->astOperand1(); modulo = tok->astOperand1();
num = tok->astOperand2(); num = tok->astOperand2();
} else if (Token::Match(tok->astOperand1(),"%num%") && Token::simpleMatch(tok->astOperand2(),"%")) { } else if (Token::Match(tok->astOperand1(), "%num%") && Token::simpleMatch(tok->astOperand2(), "%")) {
num = tok->astOperand1(); num = tok->astOperand1();
modulo = tok->astOperand2(); modulo = tok->astOperand2();
} else { } else {
@ -3218,15 +3206,13 @@ void CheckOther::checkRedundantCopy()
} else } else
continue; continue;
const Token* tok = startTok->tokAt(2); const Token* tok = startTok->next()->astOperand2();
while (tok && Token::Match(tok, "%var% .|::")) if (!Token::Match(tok->previous(), "%var% ("))
tok = tok->tokAt(2);
if (!Token::Match(tok, "%var% ("))
continue; continue;
if (!Token::Match(tok->linkAt(1), ") )| ;")) // bailout for usage like "const A a = getA()+3" if (!Token::Match(tok->link(), ") )| ;")) // bailout for usage like "const A a = getA()+3"
continue; continue;
const Function* func = tok->function(); const Function* func = tok->previous()->function();
if (func && func->tokenDef->strAt(-1) == "&") { if (func && func->tokenDef->strAt(-1) == "&") {
redundantCopyError(startTok, startTok->str()); redundantCopyError(startTok, startTok->str());
} }

View File

@ -43,39 +43,16 @@ void CheckPostfixOperator::postfixOperator()
for (std::size_t i = 0; i < functions; ++i) { for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i]; const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
bool result = false;
const Variable *var = tok->variable(); const Variable *var = tok->variable();
if (var && Token::Match(tok, "%var% ++|--")) { if (!var || !Token::Match(tok, "%var% ++|--"))
if (Token::Match(tok->previous(), ";|{|}") && Token::Match(tok->tokAt(2), ";|,|)")) { continue;
result = true;
} else if (tok->strAt(-1) == ",") {
for (const Token* tok2 = tok->tokAt(2); tok2 != 0 && tok2->str() != ")"; tok2 = tok2->next()) {
if (tok2->str() == ";") {
result = true;
break;
} else if (tok2->str() == "(")
tok2 = tok2->link();
}
} else if (tok->strAt(-1) == ".") {
for (const Token* tok2 = tok->tokAt(-2); tok2 != 0; tok2 = tok2->previous()) {
if (Token::Match(tok2, ";|{|}")) {
result = true;
break;
} else if (Token::Match(tok2, ")|]|>") && tok2->link())
tok2 = tok2->link();
else if (tok2->isAssignmentOp() || Token::Match(tok2, "(|["))
break;
}
}
}
if (result) { const Token* parent = tok->next()->astParent();
if (!parent || parent->str() == ";" || (parent->str() == "," && (!parent->astParent() || parent->astParent()->str() != "("))) {
if (var->isPointer() || var->isArray()) if (var->isPointer() || var->isArray())
continue; continue;
const Token *decltok = var->nameToken(); if (Token::Match(var->nameToken()->previous(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator")) {
if (Token::Match(decltok->previous(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator")) {
// the variable is an iterator // the variable is an iterator
postfixOperatorError(tok); postfixOperatorError(tok);
} else if (var->type()) { } else if (var->type()) {