Fixed #1643 (Variable usage (pointer is assigned but never used))

This commit is contained in:
Robert Reif 2010-05-04 17:28:03 +02:00 committed by Daniel Marjamäki
parent eb276346c5
commit 7ce70777a4
2 changed files with 192 additions and 48 deletions

View File

@ -453,7 +453,8 @@ public:
_type(type),
_read(read),
_write(write),
_modified(modified)
_modified(modified),
_noAlias(false)
{
}
@ -475,6 +476,7 @@ public:
bool _read;
bool _write;
bool _modified; // read/modify/write
bool _noAlias; // don't alias
std::set<unsigned int> _aliases;
};
@ -506,6 +508,10 @@ public:
void eraseAliases(unsigned int varid);
void eraseAll(unsigned int varid);
// don't alias this variable because it's aliased to something undefined or unsupported
// or because it's aliased to something in a larger scope
void noAlias(unsigned int varid);
private:
VariableMap _varUsage;
};
@ -517,7 +523,10 @@ void Variables::alias(unsigned int varid1, unsigned int varid2)
{
VariableUsage *var = find(varid1);
if (var)
{
var->_noAlias = false;
var->use();
}
return;
}
@ -535,6 +544,7 @@ void Variables::alias(unsigned int varid1, unsigned int varid2)
// remove all aliases from var1
var1->_aliases.clear();
var1->_noAlias = false;
VariableUsage *var2 = find(varid2);
@ -572,6 +582,14 @@ void Variables::eraseAll(unsigned int varid)
erase(varid);
}
void Variables::noAlias(unsigned int varid)
{
VariableUsage *usage = find(varid);
if (usage)
usage->_noAlias = true;
}
void Variables::addVar(const Token *name,
VariableType type,
bool write_)
@ -592,6 +610,8 @@ void Variables::readAliases(unsigned int varid)
VariableUsage *usage = find(varid);
if (usage)
{
if (!usage->_noAlias)
{
std::set<unsigned int>::iterator aliases;
@ -603,6 +623,7 @@ void Variables::readAliases(unsigned int varid)
aliased->_read = true;
}
}
}
}
void Variables::readAll(unsigned int varid)
@ -613,6 +634,8 @@ void Variables::readAll(unsigned int varid)
{
usage->_read = true;
if (!usage->_noAlias)
{
std::set<unsigned int>::iterator aliases;
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases)
@ -623,9 +646,9 @@ void Variables::readAll(unsigned int varid)
aliased->_read = true;
}
}
}
}
void Variables::write(unsigned int varid)
{
VariableUsage *usage = find(varid);
@ -639,6 +662,8 @@ void Variables::writeAliases(unsigned int varid)
VariableUsage *usage = find(varid);
if (usage)
{
if (!usage->_noAlias)
{
std::set<unsigned int>::iterator aliases;
@ -650,6 +675,7 @@ void Variables::writeAliases(unsigned int varid)
aliased->_write = true;
}
}
}
}
void Variables::writeAll(unsigned int varid)
@ -660,6 +686,8 @@ void Variables::writeAll(unsigned int varid)
{
usage->_write = true;
if (!usage->_noAlias)
{
std::set<unsigned int>::iterator aliases;
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases)
@ -670,6 +698,7 @@ void Variables::writeAll(unsigned int varid)
aliased->_write = true;
}
}
}
}
void Variables::use(unsigned int varid)
@ -680,6 +709,8 @@ void Variables::use(unsigned int varid)
{
usage->use();
if (!usage->_noAlias)
{
std::set<unsigned int>::iterator aliases;
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases)
@ -690,6 +721,7 @@ void Variables::use(unsigned int varid)
aliased->use();
}
}
}
}
void Variables::modified(unsigned int varid)
@ -700,6 +732,8 @@ void Variables::modified(unsigned int varid)
{
usage->_modified = true;
if (!usage->_noAlias)
{
std::set<unsigned int>::iterator aliases;
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases)
@ -710,6 +744,7 @@ void Variables::modified(unsigned int varid)
aliased->_modified = true;
}
}
}
}
Variables::VariableUsage *Variables::find(unsigned int varid)
@ -840,14 +875,15 @@ static int doAssignment(Variables &variables, const Token *tok, bool pointer)
{
variables.alias(varid1, varid2);
}
else
variables.read(varid2);
}
else // not a local variable
else // not a local variable (or an unsupported local variable)
{
if (var1->_type == Variables::pointer && !pointer)
{
// aliased variables in a larger scope are not supported yet
if (varid2)
variables.erase(varid1);
// aliased variables in a larger scope are not supported
var1->_noAlias = true;
}
}
}

View File

@ -79,6 +79,7 @@ private:
TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637
TEST_CASE(localvaralias3); // ticket #1639
TEST_CASE(localvaralias4); // ticket #1643
TEST_CASE(localvarasm);
// Don't give false positives for variables in structs/unions
@ -1125,22 +1126,22 @@ private:
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n"), errout.str());
// a is not a local variable and b is aliased to it (not supported yet)
// a is not a local variable and b is aliased to it
functionVariableUsage("int a;\n"
"void foo()\n"
"{\n"
" int *b = &a;\n"
"}\n");
TODO_ASSERT_EQUALS(std::string("[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n"), errout.str());
ASSERT_EQUALS(std::string("[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n"), errout.str());
// a is not a local variable and b is aliased to it (not supported yet)
// a is not a local variable and b is aliased to it
functionVariableUsage("void foo(int a)\n"
"{\n"
" int *b = &a;\n"
"}\n");
TODO_ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Variable 'b' is assigned a value that is never used\n"), errout.str());
ASSERT_EQUALS(std::string("[test.cpp:3]: (style) Variable 'b' is assigned a value that is never used\n"), errout.str());
// a is not a local variable and b is aliased to it (not supported yet)
// a is not a local variable and b is aliased to it
functionVariableUsage("class A\n"
"{\n"
" int a;\n"
@ -1149,7 +1150,7 @@ private:
" int *b = &a;\n"
" }\n"
"}\n");
TODO_ASSERT_EQUALS(std::string("[test.cpp:6]: (style) Variable 'b' is assigned a value that is never used\n"), errout.str());
ASSERT_EQUALS(std::string("[test.cpp:6]: (style) Variable 'b' is assigned a value that is never used\n"), errout.str());
functionVariableUsage("int a;\n"
"void foo()\n"
@ -1241,6 +1242,78 @@ private:
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
functionVariableUsage("int a[10];\n"
"void foo()\n"
"{\n"
" int *b = a;\n"
" int *c = b;\n"
" *c = 0;\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int *b = a;\n"
" int *c = b;\n"
" *c = 0;\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int *b = a;\n"
" int *c = b;\n"
" *c = b[0];\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int *b = a;\n"
" int *c;\n"
" *c = b[0];\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:4]: (style) Variable 'c' is not assigned a value\n"), errout.str());
functionVariableUsage("int a[10];\n"
"void foo()\n"
"{\n"
" int *b = a;\n"
" int c = b[0];\n"
" c = c;\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int *b = a;\n"
" int c = b[0];\n"
" c = c;\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
functionVariableUsage("int a[10];\n"
"void foo()\n"
"{\n"
" int *b = &a[0];\n"
" a[0] = b[0];\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int *b = &a[0];\n"
" a[0] = b[0];\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int *b = a;\n"
" a[0] = b[0];\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
functionVariableUsage("void foo(int a[10])\n"
"{\n"
" int *b = a;\n"
@ -1421,6 +1494,41 @@ private:
ASSERT_EQUALS(std::string(""), errout.str());
}
void localvaralias4() // ticket 1643
{
functionVariableUsage("struct AB { int a; int b; } ab;\n"
"void foo()\n"
"{\n"
" int * a = &ab.a;\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:4]: (style) Variable 'a' is assigned a value that is never used\n"), errout.str());
functionVariableUsage("struct AB { int a; int b; } ab;\n"
"void foo()\n"
"{\n"
" int * a = &ab.a;\n"
" *a = 0;\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
functionVariableUsage("struct AB { int a; int b; };\n"
"void foo()\n"
"{\n"
" struct AB ab;\n"
" int * a = &ab.a;\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n"), errout.str());
functionVariableUsage("struct AB { int a; int b; };\n"
"void foo()\n"
"{\n"
" struct AB ab;\n"
" int * a = &ab.a;\n"
" *a = 0;\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
void localvarasm()
{
functionVariableUsage("void foo(int &b)\n"