From 20753f4697ed74fd475cddddfabe28dad9f46816 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sun, 18 May 2014 12:52:15 +0200 Subject: [PATCH] Refactorization: Use AST in CheckOther::checkRedundantCopy(), CheckOther::clarifyStatement() and CheckPostfixOperator::postfixOperator(). --- lib/checkother.cpp | 28 +++++++--------------------- lib/checkpostfixoperator.cpp | 33 +++++---------------------------- 2 files changed, 12 insertions(+), 49 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 48c39b584..7f1027e68 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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()); } diff --git a/lib/checkpostfixoperator.cpp b/lib/checkpostfixoperator.cpp index 1ffe14bbb..9289c60dd 100644 --- a/lib/checkpostfixoperator.cpp +++ b/lib/checkpostfixoperator.cpp @@ -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()) {