simplify the alias handling in the checking of variable usage. Ticket: #1729
This commit is contained in:
parent
61e7e759f0
commit
7dba21858a
|
@ -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;
|
||||
|
||||
|
@ -609,7 +612,6 @@ void Variables::readAliases(unsigned int varid)
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -632,7 +632,6 @@ void Variables::readAll(unsigned int varid)
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
|
@ -661,7 +658,6 @@ void Variables::writeAliases(unsigned int varid)
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -684,7 +678,6 @@ void Variables::writeAll(unsigned int varid)
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -707,7 +698,6 @@ void Variables::use(unsigned int varid)
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -730,7 +718,6 @@ void Variables::modified(unsigned int varid)
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue