simplify the alias handling in the checking of variable usage. Ticket: #1729

This commit is contained in:
Robert Reif 2010-06-16 17:57:56 +02:00 committed by Daniel Marjamäki
parent 61e7e759f0
commit 7dba21858a
2 changed files with 88 additions and 74 deletions

View File

@ -437,8 +437,7 @@ public:
_type(type),
_read(read),
_write(write),
_modified(modified),
_noAlias(false)
_modified(modified)
{
}
@ -460,7 +459,6 @@ public:
bool _read;
bool _write;
bool _modified; // read/modify/write
bool _noAlias; // don't alias
std::set<unsigned int> _aliases;
};
@ -491,10 +489,7 @@ 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);
void clearAliases(unsigned int varid);
private:
VariableMap _varUsage;
@ -507,10 +502,7 @@ void Variables::alias(unsigned int varid1, unsigned int varid2)
{
VariableUsage *var = find(varid1);
if (var)
{
var->_noAlias = false;
var->use();
}
return;
}
@ -529,7 +521,6 @@ void Variables::alias(unsigned int varid1, unsigned int varid2)
// remove all aliases from var1
var1->_aliases.clear();
var1->_noAlias = false;
VariableUsage *var2 = find(varid2);
@ -548,6 +539,28 @@ void Variables::alias(unsigned int varid1, unsigned int varid2)
var2->_read = true;
}
void Variables::clearAliases(unsigned int varid)
{
VariableUsage *usage = find(varid);
if (usage)
{
// remove usage from all aliases
std::set<unsigned int>::iterator i;
for (i = usage->_aliases.begin(); i != usage->_aliases.end(); ++i)
{
VariableUsage *temp = find(*i);
if (temp)
temp->_aliases.erase(usage->_name->varId());
}
// remove all aliases from usage
usage->_aliases.clear();
}
}
void Variables::eraseAliases(unsigned int varid)
{
VariableUsage *usage = find(varid);
@ -567,14 +580,6 @@ 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_)
@ -595,8 +600,6 @@ void Variables::readAliases(unsigned int varid)
VariableUsage *usage = find(varid);
if (usage)
{
if (!usage->_noAlias)
{
std::set<unsigned int>::iterator aliases;
@ -608,7 +611,6 @@ void Variables::readAliases(unsigned int varid)
aliased->_read = true;
}
}
}
}
void Variables::readAll(unsigned int varid)
@ -619,8 +621,6 @@ 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)
@ -631,7 +631,6 @@ void Variables::readAll(unsigned int varid)
aliased->_read = true;
}
}
}
}
void Variables::write(unsigned int varid)
@ -647,8 +646,6 @@ void Variables::writeAliases(unsigned int varid)
VariableUsage *usage = find(varid);
if (usage)
{
if (!usage->_noAlias)
{
std::set<unsigned int>::iterator aliases;
@ -660,7 +657,6 @@ void Variables::writeAliases(unsigned int varid)
aliased->_write = true;
}
}
}
}
void Variables::writeAll(unsigned int varid)
@ -671,8 +667,6 @@ 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)
@ -683,7 +677,6 @@ void Variables::writeAll(unsigned int varid)
aliased->_write = true;
}
}
}
}
void Variables::use(unsigned int varid)
@ -694,8 +687,6 @@ 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)
@ -706,7 +697,6 @@ void Variables::use(unsigned int varid)
aliased->use();
}
}
}
}
void Variables::modified(unsigned int varid)
@ -717,8 +707,6 @@ 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)
@ -729,7 +717,6 @@ void Variables::modified(unsigned int varid)
aliased->_modified = true;
}
}
}
}
Variables::VariableUsage *Variables::find(unsigned int varid)
@ -884,7 +871,7 @@ static int doAssignment(Variables &variables, const Token *tok, bool pointer, bo
if (var1->_type == Variables::pointer && !pointer)
{
// aliased variables in a larger scope are not supported
var1->_noAlias = true;
variables.clearAliases(varid1);
}
}
}

View File

@ -1605,6 +1605,33 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: a\n"
"[test.cpp:5]: (style) Variable 's' is assigned a value that is never used\n", errout.str());
functionVariableUsage("int a[10];\n"
"void foo()\n"
"{\n"
" int b[10];\n"
" int c[10];\n"
" int *d;\n"
" d = b;\n"
" d = a;\n"
" d = c;\n"
" *d = 0;\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:4]: (style) Unused variable: b\n"
"[test.cpp:5]: (style) Variable 'c' is assigned a value that is never used\n"), errout.str());
functionVariableUsage("int a[10];\n"
"void foo()\n"
"{\n"
" int b[10];\n"
" int c[10];\n"
" int *d;\n"
" d = b; *d = 0;\n"
" d = a; *d = 0;\n"
" d = c; *d = 0;\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n"
"[test.cpp:5]: (style) Variable 'c' is assigned a value that is never used\n"), errout.str());
}
void localvaralias2() // ticket 1637