From 7dba21858a7965b76de2a7c2cd0d6abecb2a096c Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Wed, 16 Jun 2010 17:57:56 +0200 Subject: [PATCH] simplify the alias handling in the checking of variable usage. Ticket: #1729 --- lib/checkother.cpp | 135 +++++++++++++++++++---------------------- test/testunusedvar.cpp | 27 +++++++++ 2 files changed, 88 insertions(+), 74 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 945509e27..f72756d6e 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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 _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::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_) @@ -596,17 +601,14 @@ void Variables::readAliases(unsigned int varid) if (usage) { - if (!usage->_noAlias) + std::set::iterator aliases; + + for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { - std::set::iterator aliases; + VariableUsage *aliased = find(*aliases); - for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) - { - VariableUsage *aliased = find(*aliases); - - if (aliased) - aliased->_read = true; - } + if (aliased) + aliased->_read = true; } } } @@ -619,17 +621,14 @@ void Variables::readAll(unsigned int varid) { usage->_read = true; - if (!usage->_noAlias) + std::set::iterator aliases; + + for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { - std::set::iterator aliases; + VariableUsage *aliased = find(*aliases); - for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) - { - VariableUsage *aliased = find(*aliases); - - if (aliased) - aliased->_read = true; - } + if (aliased) + aliased->_read = true; } } } @@ -648,17 +647,14 @@ void Variables::writeAliases(unsigned int varid) if (usage) { - if (!usage->_noAlias) + std::set::iterator aliases; + + for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { - std::set::iterator aliases; + VariableUsage *aliased = find(*aliases); - for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) - { - VariableUsage *aliased = find(*aliases); - - if (aliased) - aliased->_write = true; - } + if (aliased) + aliased->_write = true; } } } @@ -671,17 +667,14 @@ void Variables::writeAll(unsigned int varid) { usage->_write = true; - if (!usage->_noAlias) + std::set::iterator aliases; + + for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { - std::set::iterator aliases; + VariableUsage *aliased = find(*aliases); - for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) - { - VariableUsage *aliased = find(*aliases); - - if (aliased) - aliased->_write = true; - } + if (aliased) + aliased->_write = true; } } } @@ -694,17 +687,14 @@ void Variables::use(unsigned int varid) { usage->use(); - if (!usage->_noAlias) + std::set::iterator aliases; + + for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { - std::set::iterator aliases; + VariableUsage *aliased = find(*aliases); - for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) - { - VariableUsage *aliased = find(*aliases); - - if (aliased) - aliased->use(); - } + if (aliased) + aliased->use(); } } } @@ -717,17 +707,14 @@ void Variables::modified(unsigned int varid) { usage->_modified = true; - if (!usage->_noAlias) + std::set::iterator aliases; + + for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { - std::set::iterator aliases; + VariableUsage *aliased = find(*aliases); - for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) - { - VariableUsage *aliased = find(*aliases); - - if (aliased) - aliased->_modified = true; - } + if (aliased) + aliased->_modified = true; } } } @@ -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); } } } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 007c10d31..0d76fead7 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -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