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();
if (Token::Match(tok2, "[{};]")) {
tok = tok->tokAt(2);
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;
}
tok = tok->astOperand1();
if (Token::Match(tok, "++|-- [;,]"))
clarifyStatementError(tok);
}
@ -3026,10 +3014,10 @@ void CheckOther::checkModuloAlwaysTrueFalse()
if (!tok->isComparisonOp())
continue;
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();
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();
modulo = tok->astOperand2();
} else {
@ -3218,15 +3206,13 @@ void CheckOther::checkRedundantCopy()
} else
continue;
const Token* tok = startTok->tokAt(2);
while (tok && Token::Match(tok, "%var% .|::"))
tok = tok->tokAt(2);
if (!Token::Match(tok, "%var% ("))
const Token* tok = startTok->next()->astOperand2();
if (!Token::Match(tok->previous(), "%var% ("))
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;
const Function* func = tok->function();
const Function* func = tok->previous()->function();
if (func && func->tokenDef->strAt(-1) == "&") {
redundantCopyError(startTok, startTok->str());
}

View File

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