From 3d0ebe196b777a98eea9381f6a1f10778fe27ff6 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Wed, 2 Jul 2014 00:17:35 +0200 Subject: [PATCH] Several improvements to CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(): - Use AST in some places - Fixed misusage of Token::isStandardType (fixes false negative) - Removed some redundant conditions --- lib/checkunusedvar.cpp | 34 ++++++++++++---------------------- test/testunusedvar.cpp | 2 +- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 867643871..eb3b56144 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -759,7 +759,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const // bailout when for_each is used - if (Token::Match(tok,"%var% (") && Token::simpleMatch(tok->linkAt(1),") {")) { + if (Token::Match(tok, "%var% (") && Token::simpleMatch(tok->linkAt(1), ") {") && !Token::Match(tok, "if|for|while|switch")) { // does the name contain "for_each" or "foreach"? std::string nameTok; nameTok.resize(tok->str().size()); @@ -783,7 +783,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const // C++11 std::for_each // No warning should be written if a variable is first read and // then written in the body. - if (Token::simpleMatch(tok, "for_each (") && Token::simpleMatch(tok->linkAt(1), ") ;")) { + else if (Token::simpleMatch(tok, "for_each (") && Token::simpleMatch(tok->linkAt(1), ") ;")) { const Token *end = tok->linkAt(1); if (end->previous()->str() == "}") { std::set readvar; @@ -798,7 +798,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } } - if (Token::Match(tok->previous(), "[;{}]")) { + else if (Token::Match(tok->previous(), "[;{}]")) { for (const Token* tok2 = tok->next(); tok2; tok2 = tok2->next()) { if (tok2->varId()) { const Variable *var = tok2->variable(); @@ -992,12 +992,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } else if (Token::Match(tok, "& %var%")) { - if (tok->previous()->isName() || tok->previous()->isNumber()) { // bitop + if (tok->astOperand2()) { // bitop variables.read(tok->next()->varId(), tok); } else // addressof variables.use(tok->next()->varId(), tok); // use = read + write } else if (Token::Match(tok, ">>|>>= %var%")) { - if (_tokenizer->isC() || tok->previous()->isStandardType()) + if (_tokenizer->isC() || (tok->previous()->variable() && tok->previous()->variable()->typeEndToken()->isStandardType())) variables.read(tok->next()->varId(), tok); else variables.use(tok->next()->varId(), tok); // use = read + write @@ -1018,21 +1018,17 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const // function else if (Token::Match(tok, "%var% (")) { variables.read(tok->varId(), tok); - if (Token::Match(tok->tokAt(2), "%var% =")) { - variables.read(tok->tokAt(2)->varId(), tok); - } } else if (Token::Match(tok, "[{,] %var% [,}]")) { variables.read(tok->next()->varId(), tok); } - else if (Token::Match(tok, "%var% .")) { + else if (tok->varId() && Token::Match(tok, "%var% .")) { variables.use(tok->varId(), tok); // use = read + write } - else if (tok->isExtendedOp() && tok->next() && - tok->next()->varId() && !Token::Match(tok->next(), "true|false|new") && tok->strAt(2) != "=") { + else if (tok->isExtendedOp() && tok->next() && tok->next()->varId() && tok->strAt(2) != "=") { variables.readAll(tok->next()->varId(), tok); } @@ -1044,18 +1040,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const variables.readAll(tok->varId(), tok); } - else if (Token::Match(tok, "++|-- %var%")) { - if (!Token::Match(tok->previous(), "[;{}:]")) - variables.use(tok->next()->varId(), tok); + // ++|-- + else if (tok->next() && tok->next()->type() == Token::eIncDecOp && tok->next()->astOperand1() && tok->next()->astOperand1()->varId()) { + if (tok->next()->astParent()) + variables.use(tok->next()->astOperand1()->varId(), tok); else - variables.modified(tok->next()->varId(), tok); - } - - else if (Token::Match(tok, "%var% ++|--")) { - if (!Token::Match(tok->previous(), "[;{}:]")) - variables.use(tok->varId(), tok); - else - variables.modified(tok->varId(), tok); + variables.modified(tok->next()->astOperand1()->varId(), tok); } else if (tok->isAssignmentOp()) { diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 3dcc20450..b2da49dce 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -1681,7 +1681,7 @@ private: " int x;\n" " if (c >> x) {}\n" "}"); - TODO_ASSERT_EQUALS("[test.c:2]: (style) Variable 'x' is not assigned a value.\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'x' is not assigned a value.\n", errout.str()); } void localvar33() { // ticket #2345