Fixed #1607 (false positive: variable is assigned a value that is never used)
This commit is contained in:
parent
43dd749cc6
commit
45573ad101
|
@ -470,6 +470,7 @@ public:
|
||||||
read = false;
|
read = false;
|
||||||
write = false;
|
write = false;
|
||||||
modified = false;
|
modified = false;
|
||||||
|
aliased = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** variable is used.. set both read+write */
|
/** variable is used.. set both read+write */
|
||||||
|
@ -489,6 +490,7 @@ public:
|
||||||
bool read;
|
bool read;
|
||||||
bool write;
|
bool write;
|
||||||
bool modified; // read/modify/write
|
bool modified; // read/modify/write
|
||||||
|
bool aliased; // pointer or reference
|
||||||
};
|
};
|
||||||
|
|
||||||
void CheckOther::functionVariableUsage()
|
void CheckOther::functionVariableUsage()
|
||||||
|
@ -572,7 +574,19 @@ void CheckOther::functionVariableUsage()
|
||||||
{
|
{
|
||||||
varUsage[tok->strAt(3)].declare = true;
|
varUsage[tok->strAt(3)].declare = true;
|
||||||
if (tok->tokAt(4)->str() == "=")
|
if (tok->tokAt(4)->str() == "=")
|
||||||
|
{
|
||||||
varUsage[tok->strAt(3)].write = true;
|
varUsage[tok->strAt(3)].write = true;
|
||||||
|
if (Token::Match(tok->tokAt(5), "%var% = &| %var%") &&
|
||||||
|
tok->tokAt(5)->str() == tok->tokAt(3)->str())
|
||||||
|
{
|
||||||
|
varUsage[tok->strAt(3)].aliased = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (Token::Match(tok->tokAt(5), "%var% = &| %var%") &&
|
||||||
|
tok->tokAt(5)->str() == tok->tokAt(3)->str())
|
||||||
|
{
|
||||||
|
varUsage[tok->strAt(3)].aliased = true;
|
||||||
|
}
|
||||||
tok = tok->tokAt(3);
|
tok = tok->tokAt(3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -580,6 +594,7 @@ void CheckOther::functionVariableUsage()
|
||||||
else if (Token::Match(tok, "[;{}] const %type% *|& %var% ;|="))
|
else if (Token::Match(tok, "[;{}] const %type% *|& %var% ;|="))
|
||||||
{
|
{
|
||||||
varUsage[tok->strAt(4)].declare = true;
|
varUsage[tok->strAt(4)].declare = true;
|
||||||
|
varUsage[tok->strAt(4)].aliased = true;
|
||||||
if (tok->tokAt(5)->str() == "=")
|
if (tok->tokAt(5)->str() == "=")
|
||||||
varUsage[tok->strAt(4)].write = true;
|
varUsage[tok->strAt(4)].write = true;
|
||||||
tok = tok->tokAt(4);
|
tok = tok->tokAt(4);
|
||||||
|
@ -588,6 +603,7 @@ void CheckOther::functionVariableUsage()
|
||||||
else if (Token::Match(tok, "[;{}] struct|union %type% *|& %var% ;|="))
|
else if (Token::Match(tok, "[;{}] struct|union %type% *|& %var% ;|="))
|
||||||
{
|
{
|
||||||
varUsage[tok->strAt(4)].declare = true;
|
varUsage[tok->strAt(4)].declare = true;
|
||||||
|
varUsage[tok->strAt(4)].aliased = true;
|
||||||
if (tok->tokAt(5)->str() == "=")
|
if (tok->tokAt(5)->str() == "=")
|
||||||
varUsage[tok->strAt(4)].write = true;
|
varUsage[tok->strAt(4)].write = true;
|
||||||
tok = tok->tokAt(4);
|
tok = tok->tokAt(4);
|
||||||
|
@ -596,6 +612,7 @@ void CheckOther::functionVariableUsage()
|
||||||
else if (Token::Match(tok, "[;{}] const struct|union %type% *|& %var% ;|="))
|
else if (Token::Match(tok, "[;{}] const struct|union %type% *|& %var% ;|="))
|
||||||
{
|
{
|
||||||
varUsage[tok->strAt(5)].declare = true;
|
varUsage[tok->strAt(5)].declare = true;
|
||||||
|
varUsage[tok->strAt(5)].aliased = true;
|
||||||
if (tok->tokAt(6)->str() == "=")
|
if (tok->tokAt(6)->str() == "=")
|
||||||
varUsage[tok->strAt(5)].write = true;
|
varUsage[tok->strAt(5)].write = true;
|
||||||
tok = tok->tokAt(5);
|
tok = tok->tokAt(5);
|
||||||
|
@ -605,6 +622,7 @@ void CheckOther::functionVariableUsage()
|
||||||
{
|
{
|
||||||
varUsage[tok->strAt(3)].declare = true;
|
varUsage[tok->strAt(3)].declare = true;
|
||||||
varUsage[tok->strAt(3)].write = true;
|
varUsage[tok->strAt(3)].write = true;
|
||||||
|
varUsage[tok->strAt(3)].aliased = true;
|
||||||
if (tok->tokAt(5)->varId() > 0)
|
if (tok->tokAt(5)->varId() > 0)
|
||||||
{
|
{
|
||||||
if (varUsage.find(tok->tokAt(5)->str()) != varUsage.end())
|
if (varUsage.find(tok->tokAt(5)->str()) != varUsage.end())
|
||||||
|
@ -621,6 +639,7 @@ void CheckOther::functionVariableUsage()
|
||||||
else if (Token::Match(tok, "[;{}] %type% *|& %var% [ %num% ] ;|=") && (tok->next()->isStandardType() || tok->next()->str() == "void"))
|
else if (Token::Match(tok, "[;{}] %type% *|& %var% [ %num% ] ;|=") && (tok->next()->isStandardType() || tok->next()->str() == "void"))
|
||||||
{
|
{
|
||||||
varUsage[tok->strAt(3)].declare = true;
|
varUsage[tok->strAt(3)].declare = true;
|
||||||
|
varUsage[tok->strAt(3)].aliased = true;
|
||||||
if (tok->tokAt(7)->str() == "=")
|
if (tok->tokAt(7)->str() == "=")
|
||||||
varUsage[tok->strAt(3)].write = true;
|
varUsage[tok->strAt(3)].write = true;
|
||||||
tok = tok->tokAt(6);
|
tok = tok->tokAt(6);
|
||||||
|
@ -629,6 +648,7 @@ void CheckOther::functionVariableUsage()
|
||||||
else if (Token::Match(tok, "[;{}] const %type% *|& %var% [ %num% ] ;|=") && (tok->tokAt(2)->isStandardType() || tok->tokAt(2)->str() == "void"))
|
else if (Token::Match(tok, "[;{}] const %type% *|& %var% [ %num% ] ;|=") && (tok->tokAt(2)->isStandardType() || tok->tokAt(2)->str() == "void"))
|
||||||
{
|
{
|
||||||
varUsage[tok->strAt(4)].declare = true;
|
varUsage[tok->strAt(4)].declare = true;
|
||||||
|
varUsage[tok->strAt(4)].aliased = true;
|
||||||
if (tok->tokAt(8)->str() == "=")
|
if (tok->tokAt(8)->str() == "=")
|
||||||
varUsage[tok->strAt(4)].write = true;
|
varUsage[tok->strAt(4)].write = true;
|
||||||
tok = tok->tokAt(7);
|
tok = tok->tokAt(7);
|
||||||
|
@ -697,7 +717,7 @@ void CheckOther::functionVariableUsage()
|
||||||
unassignedVariableError(tok1, varname);
|
unassignedVariableError(tok1, varname);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (!usage.read && !usage.modified)
|
else if (!usage.read && !usage.modified && !usage.aliased)
|
||||||
{
|
{
|
||||||
unreadVariableError(tok1, varname);
|
unreadVariableError(tok1, varname);
|
||||||
}
|
}
|
||||||
|
|
|
@ -491,7 +491,7 @@ private:
|
||||||
" char *i;\n"
|
" char *i;\n"
|
||||||
" i = fgets();\n"
|
" i = fgets();\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
|
||||||
|
|
||||||
functionVariableUsage("void foo()\n"
|
functionVariableUsage("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -506,6 +506,15 @@ private:
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
functionVariableUsage("int a[10];\n"
|
||||||
|
"void foo()\n"
|
||||||
|
"{\n"
|
||||||
|
" int *p = a;\n"
|
||||||
|
" for (int i = 0; i < 10; i++)\n"
|
||||||
|
" p[i] = 0;\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
functionVariableUsage("int a[10];\n"
|
functionVariableUsage("int a[10];\n"
|
||||||
"void foo()\n"
|
"void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -513,7 +522,7 @@ private:
|
||||||
" for (int i = 0; i < 10; i++)\n"
|
" for (int i = 0; i < 10; i++)\n"
|
||||||
" p[i] = 0;\n"
|
" p[i] = 0;\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'p' is assigned a value that is never used\n", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void localvarasm()
|
void localvarasm()
|
||||||
|
|
Loading…
Reference in New Issue