Skip to content

Commit 3d0ebe1

Browse files
committed
Several improvements to CheckUnusedVar::checkFunctionVariableUsage_iterateScopes():
- Use AST in some places - Fixed misusage of Token::isStandardType (fixes false negative) - Removed some redundant conditions
1 parent 220f750 commit 3d0ebe1

2 files changed

Lines changed: 13 additions & 23 deletions

File tree

lib/checkunusedvar.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
759759

760760

761761
// bailout when for_each is used
762-
if (Token::Match(tok,"%var% (") && Token::simpleMatch(tok->linkAt(1),") {")) {
762+
if (Token::Match(tok, "%var% (") && Token::simpleMatch(tok->linkAt(1), ") {") && !Token::Match(tok, "if|for|while|switch")) {
763763
// does the name contain "for_each" or "foreach"?
764764
std::string nameTok;
765765
nameTok.resize(tok->str().size());
@@ -783,7 +783,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
783783
// C++11 std::for_each
784784
// No warning should be written if a variable is first read and
785785
// then written in the body.
786-
if (Token::simpleMatch(tok, "for_each (") && Token::simpleMatch(tok->linkAt(1), ") ;")) {
786+
else if (Token::simpleMatch(tok, "for_each (") && Token::simpleMatch(tok->linkAt(1), ") ;")) {
787787
const Token *end = tok->linkAt(1);
788788
if (end->previous()->str() == "}") {
789789
std::set<unsigned int> readvar;
@@ -798,7 +798,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
798798
}
799799
}
800800

801-
if (Token::Match(tok->previous(), "[;{}]")) {
801+
else if (Token::Match(tok->previous(), "[;{}]")) {
802802
for (const Token* tok2 = tok->next(); tok2; tok2 = tok2->next()) {
803803
if (tok2->varId()) {
804804
const Variable *var = tok2->variable();
@@ -992,12 +992,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
992992
}
993993

994994
else if (Token::Match(tok, "& %var%")) {
995-
if (tok->previous()->isName() || tok->previous()->isNumber()) { // bitop
995+
if (tok->astOperand2()) { // bitop
996996
variables.read(tok->next()->varId(), tok);
997997
} else // addressof
998998
variables.use(tok->next()->varId(), tok); // use = read + write
999999
} else if (Token::Match(tok, ">>|>>= %var%")) {
1000-
if (_tokenizer->isC() || tok->previous()->isStandardType())
1000+
if (_tokenizer->isC() || (tok->previous()->variable() && tok->previous()->variable()->typeEndToken()->isStandardType()))
10011001
variables.read(tok->next()->varId(), tok);
10021002
else
10031003
variables.use(tok->next()->varId(), tok); // use = read + write
@@ -1018,21 +1018,17 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
10181018
// function
10191019
else if (Token::Match(tok, "%var% (")) {
10201020
variables.read(tok->varId(), tok);
1021-
if (Token::Match(tok->tokAt(2), "%var% =")) {
1022-
variables.read(tok->tokAt(2)->varId(), tok);
1023-
}
10241021
}
10251022

10261023
else if (Token::Match(tok, "[{,] %var% [,}]")) {
10271024
variables.read(tok->next()->varId(), tok);
10281025
}
10291026

1030-
else if (Token::Match(tok, "%var% .")) {
1027+
else if (tok->varId() && Token::Match(tok, "%var% .")) {
10311028
variables.use(tok->varId(), tok); // use = read + write
10321029
}
10331030

1034-
else if (tok->isExtendedOp() && tok->next() &&
1035-
tok->next()->varId() && !Token::Match(tok->next(), "true|false|new") && tok->strAt(2) != "=") {
1031+
else if (tok->isExtendedOp() && tok->next() && tok->next()->varId() && tok->strAt(2) != "=") {
10361032
variables.readAll(tok->next()->varId(), tok);
10371033
}
10381034

@@ -1044,18 +1040,12 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
10441040
variables.readAll(tok->varId(), tok);
10451041
}
10461042

1047-
else if (Token::Match(tok, "++|-- %var%")) {
1048-
if (!Token::Match(tok->previous(), "[;{}:]"))
1049-
variables.use(tok->next()->varId(), tok);
1050-
else
1051-
variables.modified(tok->next()->varId(), tok);
1052-
}
1053-
1054-
else if (Token::Match(tok, "%var% ++|--")) {
1055-
if (!Token::Match(tok->previous(), "[;{}:]"))
1056-
variables.use(tok->varId(), tok);
1043+
// ++|--
1044+
else if (tok->next() && tok->next()->type() == Token::eIncDecOp && tok->next()->astOperand1() && tok->next()->astOperand1()->varId()) {
1045+
if (tok->next()->astParent())
1046+
variables.use(tok->next()->astOperand1()->varId(), tok);
10571047
else
1058-
variables.modified(tok->varId(), tok);
1048+
variables.modified(tok->next()->astOperand1()->varId(), tok);
10591049
}
10601050

10611051
else if (tok->isAssignmentOp()) {

test/testunusedvar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ class TestUnusedVar : public TestFixture {
16811681
" int x;\n"
16821682
" if (c >> x) {}\n"
16831683
"}");
1684-
TODO_ASSERT_EQUALS("[test.c:2]: (style) Variable 'x' is not assigned a value.\n", "", errout.str());
1684+
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'x' is not assigned a value.\n", errout.str());
16851685
}
16861686

16871687
void localvar33() { // ticket #2345

0 commit comments

Comments
 (0)