Fixed #4143: Give correct line numbers in checkunusedvar.cpp

This commit is contained in:
PKEuS 2012-09-04 14:53:24 +02:00
parent 913670d254
commit 8924e8af43
2 changed files with 185 additions and 166 deletions

View File

@ -38,15 +38,14 @@ public:
/** Store information about variable usage */
class VariableUsage {
public:
VariableUsage(const Token *name = 0,
VariableUsage(const Variable *var = 0,
VariableType type = standard,
const Scope *scope = NULL,
bool read = false,
bool write = false,
bool modified = false,
bool allocateMemory = false) :
_name(name),
_scope(scope),
_var(var),
_lastAccess(var?var->nameToken():0),
_type(type),
_read(read),
_write(write),
@ -68,8 +67,8 @@ public:
std::set<unsigned int> _aliases;
std::set<const Scope*> _assignments;
const Token *_name;
const Scope *_scope;
const Variable* _var;
const Token* _lastAccess;
VariableType _type;
bool _read;
bool _write;
@ -85,16 +84,16 @@ public:
const VariableMap &varUsage() const {
return _varUsage;
}
void addVar(const Token *name, VariableType type, const Scope *scope, bool write_);
void allocateMemory(unsigned int varid);
void read(unsigned int varid);
void readAliases(unsigned int varid);
void readAll(unsigned int varid);
void write(unsigned int varid);
void writeAliases(unsigned int varid);
void writeAll(unsigned int varid);
void use(unsigned int varid);
void modified(unsigned int varid);
void addVar(const Variable *var, VariableType type, bool write_);
void allocateMemory(unsigned int varid, const Token* tok);
void read(unsigned int varid, const Token* tok);
void readAliases(unsigned int varid, const Token* tok);
void readAll(unsigned int varid, const Token* tok);
void write(unsigned int varid, const Token* tok);
void writeAliases(unsigned int varid, const Token* tok);
void writeAll(unsigned int varid, const Token* tok);
void use(unsigned int varid, const Token* tok);
void modified(unsigned int varid, const Token* tok);
VariableUsage *find(unsigned int varid);
void alias(unsigned int varid1, unsigned int varid2, bool replace);
void erase(unsigned int varid) {
@ -134,7 +133,7 @@ void Variables::alias(unsigned int varid1, unsigned int varid2, bool replace)
VariableUsage *temp = find(*i);
if (temp)
temp->_aliases.erase(var1->_name->varId());
temp->_aliases.erase(var1->_var->varId());
}
// remove all aliases from var1
@ -167,7 +166,7 @@ void Variables::clearAliases(unsigned int varid)
VariableUsage *temp = find(*i);
if (temp)
temp->_aliases.erase(usage->_name->varId());
temp->_aliases.erase(usage->_var->varId());
}
// remove all aliases from usage
@ -193,32 +192,35 @@ void Variables::eraseAll(unsigned int varid)
erase(varid);
}
void Variables::addVar(const Token *name,
void Variables::addVar(const Variable *var,
VariableType type,
const Scope *scope,
bool write_)
{
if (name->varId() > 0)
_varUsage.insert(std::make_pair(name->varId(), VariableUsage(name, type, scope, false, write_, false)));
if (var->varId() > 0)
_varUsage.insert(std::make_pair(var->varId(), VariableUsage(var, type, false, write_, false)));
}
void Variables::allocateMemory(unsigned int varid)
void Variables::allocateMemory(unsigned int varid, const Token* tok)
{
VariableUsage *usage = find(varid);
if (usage)
if (usage) {
usage->_allocateMemory = true;
usage->_lastAccess = tok;
}
}
void Variables::read(unsigned int varid)
void Variables::read(unsigned int varid, const Token* tok)
{
VariableUsage *usage = find(varid);
if (usage)
if (usage) {
usage->_read = true;
usage->_lastAccess = tok;
}
}
void Variables::readAliases(unsigned int varid)
void Variables::readAliases(unsigned int varid, const Token* tok)
{
VariableUsage *usage = find(varid);
@ -228,41 +230,47 @@ void Variables::readAliases(unsigned int varid)
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
VariableUsage *aliased = find(*aliases);
if (aliased)
if (aliased) {
aliased->_read = true;
aliased->_lastAccess = tok;
}
}
}
}
void Variables::readAll(unsigned int varid)
void Variables::readAll(unsigned int varid, const Token* tok)
{
VariableUsage *usage = find(varid);
if (usage) {
usage->_read = true;
usage->_lastAccess = tok;
std::set<unsigned int>::iterator aliases;
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
VariableUsage *aliased = find(*aliases);
if (aliased)
if (aliased) {
aliased->_read = true;
aliased->_lastAccess = tok;
}
}
}
}
void Variables::write(unsigned int varid)
void Variables::write(unsigned int varid, const Token* tok)
{
VariableUsage *usage = find(varid);
if (usage) {
usage->_write = true;
usage->_read = false;
usage->_lastAccess = tok;
}
}
void Variables::writeAliases(unsigned int varid)
void Variables::writeAliases(unsigned int varid, const Token* tok)
{
VariableUsage *usage = find(varid);
@ -272,62 +280,73 @@ void Variables::writeAliases(unsigned int varid)
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
VariableUsage *aliased = find(*aliases);
if (aliased)
if (aliased) {
aliased->_write = true;
aliased->_lastAccess = tok;
}
}
}
}
void Variables::writeAll(unsigned int varid)
void Variables::writeAll(unsigned int varid, const Token* tok)
{
VariableUsage *usage = find(varid);
if (usage) {
usage->_write = true;
usage->_lastAccess = tok;
std::set<unsigned int>::iterator aliases;
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
VariableUsage *aliased = find(*aliases);
if (aliased)
if (aliased) {
aliased->_write = true;
aliased->_lastAccess = tok;
}
}
}
}
void Variables::use(unsigned int varid)
void Variables::use(unsigned int varid, const Token* tok)
{
VariableUsage *usage = find(varid);
if (usage) {
usage->use();
usage->_lastAccess = tok;
std::set<unsigned int>::iterator aliases;
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
VariableUsage *aliased = find(*aliases);
if (aliased)
if (aliased) {
aliased->use();
aliased->_lastAccess = tok;
}
}
}
}
void Variables::modified(unsigned int varid)
void Variables::modified(unsigned int varid, const Token* tok)
{
VariableUsage *usage = find(varid);
if (usage) {
usage->_modified = true;
usage->_lastAccess = tok;
std::set<unsigned int>::iterator aliases;
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
VariableUsage *aliased = find(*aliases);
if (aliased)
if (aliased) {
aliased->_modified = true;
aliased->_lastAccess = tok;
}
}
}
}
@ -360,7 +379,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de
tok = tok->next();
while (tok->str() != "=") {
if (tok->varId())
variables.read(tok->varId());
variables.read(tok->varId(), tok);
tok = tok->next();
}
tok = tok->next();
@ -372,7 +391,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de
bool addressOf = false;
if (Token::Match(tok, "%var% ."))
variables.use(tok->varId()); // use = read + write
variables.use(tok->varId(), tok); // use = read + write
// check for C style cast
if (tok->str() == "(") {
@ -437,7 +456,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de
if (var2) { // local variable (alias or read it)
if (var1->_type == Variables::pointer || var1->_type == Variables::pointerArray) {
if (dereference)
variables.read(varid2);
variables.read(varid2, tok);
else {
if (addressOf ||
var2->_type == Variables::array ||
@ -449,7 +468,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de
replace = false;
// check if variable declared in same scope
else if (scope == var1->_scope)
else if (scope == var1->_var->scope())
replace = true;
// not in same scope as declaration
@ -481,25 +500,25 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de
variables.alias(varid1, varid2, replace);
} else if (tok->strAt(1) == "?") {
if (var2->_type == Variables::reference)
variables.readAliases(varid2);
variables.readAliases(varid2, tok);
else
variables.read(varid2);
variables.read(varid2, tok);
} else {
variables.read(varid2);
variables.read(varid2, tok);
}
}
} else if (var1->_type == Variables::reference) {
variables.alias(varid1, varid2, true);
} else {
if (var2->_type == Variables::pointer && tok->strAt(1) == "[")
variables.readAliases(varid2);
variables.readAliases(varid2, tok);
variables.read(varid2);
variables.read(varid2, tok);
}
} else { // not a local variable (or an unsupported local variable)
if (var1->_type == Variables::pointer && !dereference) {
// check if variable declaration is in this scope
if (var1->_scope == scope)
if (var1->_var->scope() == scope)
variables.clearAliases(varid1);
else {
// no other assignment in this scope
@ -602,7 +621,7 @@ static const Token * skipBracketsAndMembers(const Token *tok)
void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const scope, Variables& variables, bool insideLoop, std::vector<unsigned int> &usedVariables)
{
// Find declarations if the scope is executable..
if (scope->type != Scope::eClass && scope->type != Scope::eUnion && scope->type != Scope::eStruct) {
if (scope->isExecutable()) {
// Find declarations
for (std::list<Variable>::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) {
if (i->isThrow() || i->isExtern())
@ -629,27 +648,27 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
if (defValTok->str() == "[")
defValTok = defValTok->link();
else if (defValTok->str() == "(" || defValTok->str() == "=") {
variables.addVar(i->nameToken(), type, scope, true);
variables.addVar(&*i, type, true);
break;
} else if (defValTok->str() == ";" || defValTok->str() == "," || defValTok->str() == ")") {
variables.addVar(i->nameToken(), type, scope, i->isStatic());
variables.addVar(&*i, type, i->isStatic());
break;
}
}
if (i->isArray() && i->isClass()) // Array of class/struct members. Initialized by ctor.
variables.write(i->varId());
variables.write(i->varId(), i->nameToken());
if (i->isArray() && Token::Match(i->nameToken(), "%var% [ %var% ]")) // Array index variable read.
variables.read(i->nameToken()->tokAt(2)->varId());
variables.read(i->nameToken()->tokAt(2)->varId(), i->nameToken());
if (defValTok && defValTok->str() == "=") {
if (defValTok->next() && defValTok->next()->str() == "{") {
for (const Token* tok = defValTok; tok && tok != defValTok->linkAt(1); tok = tok->next())
if (Token::Match(tok, "%var%")) // Variables used to initialize the array read.
variables.read(tok->varId());
variables.read(tok->varId(), i->nameToken());
} else
doAssignment(variables, i->nameToken(), false, scope);
} else if (Token::Match(defValTok, "( %var% )")) // Variables used to initialize the variable read.
variables.readAll(defValTok->next()->varId()); // ReadAll?
variables.readAll(defValTok->next()->varId(), i->nameToken()); // ReadAll?
}
}
@ -666,7 +685,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
insideLoop = false;
std::vector<unsigned int>::iterator it;
for (it = usedVariables.begin(); it != usedVariables.end(); it++) {
variables.read((*it));
variables.read((*it), tok);
}
tok = (*i)->classStart->link();
break;
@ -728,21 +747,21 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
Variables::VariableUsage *var = variables.find(varid);
if (var && !var->_allocateMemory) {
variables.readAll(varid);
variables.readAll(varid, tok);
}
}
else if (Token::Match(tok, "return|throw")) {
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->varId())
variables.readAll(tok2->varId());
variables.readAll(tok2->varId(), tok);
else if (tok2->str() == ";")
break;
}
}
else if (Token::Match(tok->tokAt(-2), "while|if") && Token::Match(tok->tokAt(1), "=") && tok->varId() && tok->varId() == tok->tokAt(2)->varId()) {
variables.use(tok->tokAt(2)->varId());
variables.use(tok->tokAt(2)->varId(), tok);
}
// assignment
else if (!Token::Match(tok->tokAt(-2), "[;{}.] %var% (") &&
@ -777,19 +796,19 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
tok = doAssignment(variables, tok, dereference, scope);
if (pre || post)
variables.use(varid1);
variables.use(varid1, tok);
if (dereference) {
Variables::VariableUsage *var = variables.find(varid1);
if (var && var->_type == Variables::array)
variables.write(varid1);
variables.writeAliases(varid1);
variables.read(varid1);
variables.write(varid1, tok);
variables.writeAliases(varid1, tok);
variables.read(varid1, tok);
} else {
Variables::VariableUsage *var = variables.find(varid1);
if (var && var->_type == Variables::reference) {
variables.writeAliases(varid1);
variables.read(varid1);
variables.writeAliases(varid1, tok);
variables.read(varid1, tok);
}
// Consider allocating memory separately because allocating/freeing alone does not constitute using the variable
else if (var && var->_type == Variables::pointer &&
@ -813,26 +832,26 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
}
if (allocate)
variables.allocateMemory(varid1);
variables.allocateMemory(varid1, tok);
else
variables.write(varid1);
variables.write(varid1, tok);
} else if (varid1 && Token::Match(tok, "%varid% .", varid1)) {
variables.use(varid1);
variables.use(varid1, tok);
} else {
variables.write(varid1);
variables.write(varid1, tok);
}
Variables::VariableUsage *var2 = variables.find(tok->varId());
if (var2) {
if (var2->_type == Variables::reference) {
variables.writeAliases(tok->varId());
variables.read(tok->varId());
variables.writeAliases(tok->varId(), tok);
variables.read(tok->varId(), tok);
} else if (tok->varId() != varid1 && Token::Match(tok, "%var% ."))
variables.read(tok->varId());
variables.read(tok->varId(), tok);
else if (tok->varId() != varid1 &&
var2->_type == Variables::standard &&
tok->strAt(-1) != "&")
variables.use(tok->varId());
variables.use(tok->varId(), tok);
}
}
@ -865,93 +884,93 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
// Consider allocating memory separately because allocating/freeing alone does not constitute using the variable
if (var->_type == Variables::pointer &&
Token::Match(skipBrackets(tok->next()), "= new|malloc|calloc|kmalloc|kzalloc|kcalloc|strdup|strndup|vmalloc|g_new0|g_try_new|g_new|g_malloc|g_malloc0|g_try_malloc|g_try_malloc0|g_strdup|g_strndup|g_strdup_printf")) {
variables.allocateMemory(varid);
variables.allocateMemory(varid, tok);
} else if (var->_type == Variables::pointer || var->_type == Variables::reference) {
variables.read(varid);
variables.writeAliases(varid);
variables.read(varid, tok);
variables.writeAliases(varid, tok);
} else if (var->_type == Variables::pointerArray) {
tok = doAssignment(variables, tok, false, scope);
} else
variables.writeAll(varid);
variables.writeAll(varid, tok);
}
}
else if (Token::Match(tok, "& %var%")) {
if (tok->previous()->isName() || tok->previous()->isNumber()) { // bitop
variables.read(tok->next()->varId());
variables.read(tok->next()->varId(), tok);
} else // addressof
variables.use(tok->next()->varId()); // use = read + write
variables.use(tok->next()->varId(), tok); // use = read + write
usedVariables.push_back(tok->next()->varId());
} else if (Token::Match(tok, ">> %var%")) {
variables.use(tok->next()->varId()); // use = read + write
variables.use(tok->next()->varId(), tok); // use = read + write
usedVariables.push_back(tok->next()->varId());
} else if (Token::Match(tok, "%var% >>|&") && Token::Match(tok->previous(), "[{};:]")) {
variables.read(tok->varId());
variables.read(tok->varId(), tok);
usedVariables.push_back(tok->next()->varId());
}
// function parameter
else if (Token::Match(tok, "[(,] %var% [")) {
variables.use(tok->next()->varId()); // use = read + write
variables.use(tok->next()->varId(), tok); // use = read + write
usedVariables.push_back(tok->next()->varId());
} else if (Token::Match(tok, "[(,] %var% [,)]") && tok->previous()->str() != "*") {
variables.use(tok->next()->varId()); // use = read + write
variables.use(tok->next()->varId(), tok); // use = read + write
usedVariables.push_back(tok->next()->varId());
} else if (Token::Match(tok, "[(,] (") &&
Token::Match(tok->next()->link(), ") %var% [,)]")) {
variables.use(tok->next()->link()->next()->varId()); // use = read + write
variables.use(tok->next()->link()->next()->varId(), tok); // use = read + write
usedVariables.push_back(tok->next()->link()->next()->varId());
}
// function
else if (Token::Match(tok, "%var% (")) {
variables.read(tok->varId());
variables.read(tok->varId(), tok);
usedVariables.push_back(tok->varId());
if (Token::Match(tok->tokAt(2), "%var% =")) {
variables.read(tok->tokAt(2)->varId());
variables.read(tok->tokAt(2)->varId(), tok);
usedVariables.push_back(tok->tokAt(2)->varId());
}
}
else if (Token::Match(tok, "[{,] %var% [,}]")) {
variables.read(tok->next()->varId());
variables.read(tok->next()->varId(), tok);
usedVariables.push_back(tok->next()->varId());
}
else if (Token::Match(tok, "%var% .")) {
variables.use(tok->varId()); // use = read + write
variables.use(tok->varId(), tok); // use = read + write
usedVariables.push_back(tok->varId());
}
else if (tok->isExtendedOp() &&
Token::Match(tok->next(), "%var%") && !Token::Match(tok->next(), "true|false|new") && tok->strAt(2) != "=") {
variables.readAll(tok->next()->varId());
variables.readAll(tok->next()->varId(), tok);
usedVariables.push_back(tok->next()->varId());
}
else if (Token::Match(tok, "%var%") && tok->next() && (tok->next()->str() == ")" || tok->next()->isExtendedOp())) {
variables.readAll(tok->varId());
variables.readAll(tok->varId(), tok);
usedVariables.push_back(tok->varId());
}
else if (Token::Match(tok, "%var% ;") && Token::Match(tok->previous(), "[;{}:]")) {
variables.readAll(tok->varId());
variables.readAll(tok->varId(), tok);
usedVariables.push_back(tok->varId());
}
else if (Token::Match(tok, "++|-- %var%")) {
if (!Token::Match(tok->previous(), "[;{}:]"))
variables.use(tok->next()->varId());
variables.use(tok->next()->varId(), tok);
else
variables.modified(tok->next()->varId());
variables.modified(tok->next()->varId(), tok);
usedVariables.push_back(tok->next()->varId());
}
else if (Token::Match(tok, "%var% ++|--")) {
if (!Token::Match(tok->previous(), "[;{}:]"))
variables.use(tok->varId());
variables.use(tok->varId(), tok);
else
variables.modified(tok->varId());
variables.modified(tok->varId(), tok);
usedVariables.push_back(tok->varId());
}
@ -959,9 +978,9 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
if (tok2->varId()) {
if (tok2->next()->isAssignmentOp())
variables.write(tok2->varId());
variables.write(tok2->varId(), tok);
else
variables.read(tok2->varId());
variables.read(tok2->varId(), tok);
usedVariables.push_back(tok2->varId());
}
}
@ -992,11 +1011,11 @@ void CheckUnusedVar::checkFunctionVariableUsage()
// Check usage of all variables in the current scope..
for (Variables::VariableMap::const_iterator it = variables.varUsage().begin(); it != variables.varUsage().end(); ++it) {
const Variables::VariableUsage &usage = it->second;
const std::string &varname = usage._name->str();
const std::string &varname = usage._var->name();
const Variable* var = symbolDatabase->getVariableFromVarId(it->first);
// variable has been marked as unused so ignore it
if (usage._name->isUnused())
if (usage._var->nameToken()->isUnused())
continue;
// skip things that are only partially implemented to prevent false positives
@ -1008,23 +1027,23 @@ void CheckUnusedVar::checkFunctionVariableUsage()
// variable has had memory allocated for it, but hasn't done
// anything with that memory other than, perhaps, freeing it
if (usage.unused() && !usage._modified && usage._allocateMemory)
allocatedButUnusedVariableError(usage._name, varname);
allocatedButUnusedVariableError(usage._lastAccess, varname);
// variable has not been written, read, or modified
else if (usage.unused() && !usage._modified)
unusedVariableError(usage._name, varname);
unusedVariableError(usage._var->nameToken(), varname);
// variable has not been written but has been modified
else if (usage._modified && !usage._write && !usage._allocateMemory && !Token::simpleMatch(var->typeStartToken(), "std ::"))
unassignedVariableError(usage._name, varname);
unassignedVariableError(usage._var->nameToken(), varname);
// variable has been written but not read
else if (!usage._read && !usage._modified)
unreadVariableError(usage._name, varname);
unreadVariableError(usage._lastAccess, varname);
// variable has been read but not written
else if (!usage._write && !usage._allocateMemory && !Token::simpleMatch(var->typeStartToken(), "std ::"))
unassignedVariableError(usage._name, varname);
unassignedVariableError(usage._var->nameToken(), varname);
}
}
}

View File

@ -483,7 +483,7 @@ private:
" char *i;\n"
" i = fgets();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
// undefined variables are not reported because they may be classes with constructors
functionVariableUsage("undefined foo()\n"
@ -609,7 +609,7 @@ private:
" d += code;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'd' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:7]: (style) Variable 'd' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -630,7 +630,7 @@ private:
" d += code;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'd' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:7]: (style) Variable 'd' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -805,7 +805,7 @@ private:
" ;\n"
" else i = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
}
void localvar4() {
@ -840,7 +840,7 @@ private:
" for (int i=0;i<10;++i)\n"
" b[i] = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -849,7 +849,7 @@ private:
" for (int i=0;i<10;++i)\n"
" b[i] = ++a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
}
void localvar7() { // ticket 1253
@ -984,7 +984,7 @@ private:
" int &j = i;\n"
" j = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'i' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1052,16 +1052,16 @@ private:
" a[0] = 0;\n"
" x = a[0];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'x' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'x' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int a, b, c;\n"
" a = b = c = f();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n"
"[test.cpp:3]: (style) Variable 'b' is assigned a value that is never used\n"
"[test.cpp:3]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'a' is assigned a value that is never used\n"
"[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n"
"[test.cpp:4]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
functionVariableUsage("int * foo()\n"
"{\n"
@ -1078,7 +1078,7 @@ private:
" for (int i = 0; i < 10; )\n"
" a[i++] = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
}
void localvar10() {
@ -1130,7 +1130,7 @@ private:
" }\n"
" i = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'i' is assigned a value that is never used\n"
ASSERT_EQUALS("[test.cpp:9]: (style) Variable 'i' is assigned a value that is never used\n"
"[test.cpp:5]: (style) Unused variable: i\n"
"[test.cpp:7]: (style) Unused variable: i\n", errout.str());
}
@ -1173,12 +1173,12 @@ private:
"\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n"
"[test.cpp:3]: (style) Variable 'b' is assigned a value that is never used\n"
"[test.cpp:3]: (style) Variable 'c' is assigned a value that is never used\n"
"[test.cpp:3]: (style) Variable 'd' is assigned a value that is never used\n"
"[test.cpp:3]: (style) Variable 'e' is assigned a value that is never used\n"
"[test.cpp:3]: (style) Variable 'f' is assigned a value that is never used\n",
"[test.cpp:4]: (style) Variable 'a' is assigned a value that is never used\n"
"[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n"
"[test.cpp:4]: (style) Variable 'c' is assigned a value that is never used\n"
"[test.cpp:4]: (style) Variable 'd' is assigned a value that is never used\n"
"[test.cpp:4]: (style) Variable 'e' is assigned a value that is never used\n"
"[test.cpp:4]: (style) Variable 'f' is assigned a value that is never used\n",
errout.str());
functionVariableUsage("void foo()\n"
@ -1189,12 +1189,12 @@ private:
"}\n");
TODO_ASSERT_EQUALS(
"[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n"
"[test.cpp:3]: (style) Variable 'b' is assigned a value that is never used\n"
"[test.cpp:4]: (style) Variable 'a' is assigned a value that is never used\n"
"[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n"
"[test.cpp:3]: (style) Variable 'c' is assigned a value that is never used\n",
"[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n"
"[test.cpp:3]: (style) Variable 'b' is assigned a value that is never used\n",
"[test.cpp:4]: (style) Variable 'a' is assigned a value that is never used\n"
"[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n",
errout.str());
}
@ -1204,7 +1204,7 @@ private:
" int x;\n"
" x = obj->ySize / 8;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'x' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'x' is assigned a value that is never used\n", errout.str());
}
void localvar14() {
@ -1270,7 +1270,7 @@ private:
" char *ptr = buf;\n"
" *(ptr++) = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'buf' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'buf' is assigned a value that is never used\n", errout.str());
functionVariableUsage("int foo()\n"
"{\n"
@ -1313,7 +1313,7 @@ private:
" data->info = k;\n"
" line_start = ptr;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'line_start' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:10]: (style) Variable 'line_start' is assigned a value that is never used\n", errout.str());
}
void localvar18() { // ticket #1723
@ -1331,7 +1331,7 @@ private:
" c = *(a);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'a' is not assigned a value\n"
"[test.cpp:3]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
"[test.cpp:4]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
}
void localvar20() { // ticket #1799
@ -1638,7 +1638,7 @@ private:
" int *b = &a;\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1646,7 +1646,7 @@ private:
" char *b = (char *)&a;\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1654,7 +1654,7 @@ private:
" char *b = (char *)(&a);\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1662,7 +1662,7 @@ private:
" const char *b = (const char *)&a;\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1670,7 +1670,7 @@ private:
" const char *b = (const char *)(&a);\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1678,7 +1678,7 @@ private:
" char *b = static_cast<char *>(&a);\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1686,7 +1686,7 @@ private:
" const char *b = static_cast<const char *>(&a);\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (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
functionVariableUsage("int a;\n"
@ -1746,7 +1746,7 @@ private:
" int *b = a;\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1754,7 +1754,7 @@ private:
" char *b = (char *)a;\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1762,7 +1762,7 @@ private:
" char *b = (char *)(a);\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1770,7 +1770,7 @@ private:
" const char *b = (const char *)a;\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1778,7 +1778,7 @@ private:
" const char *b = (const char *)(a);\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1786,7 +1786,7 @@ private:
" char *b = static_cast<char *>(a);\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1794,7 +1794,7 @@ private:
" const char *b = static_cast<const char *>(a);\n"
" *b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("int a[10];\n"
"void foo()\n"
@ -1901,7 +1901,7 @@ private:
" int *c = b;\n"
" *c = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1912,7 +1912,7 @@ private:
" *d = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: a\n"
"[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n"
"[test.cpp:7]: (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());
functionVariableUsage("void foo()\n"
@ -1924,7 +1924,7 @@ private:
" *c = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: a\n"
"[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
"[test.cpp:7]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1936,15 +1936,15 @@ private:
" c = a;\n"
" *c = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n"
"[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:9]: (style) Variable 'a' is assigned a value that is never used\n"
"[test.cpp:7]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
" int a[10], * b = a + 10;\n"
" b[-10] = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -1952,7 +1952,7 @@ private:
" b[-10] = 0;\n"
" int * c = b - 10;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n"
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'a' 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());
functionVariableUsage("void foo()\n"
@ -1969,7 +1969,7 @@ private:
" int * c = b - 10;\n"
" c[1] = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -2045,7 +2045,7 @@ private:
" *d = 0;\n"
"}\n");
ASSERT_EQUALS("[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());
"[test.cpp:10]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
functionVariableUsage("int a[10];\n"
"void foo()\n"
@ -2057,8 +2057,8 @@ private:
" d = a; *d = 0;\n"
" d = c; *d = 0;\n"
"}\n");
ASSERT_EQUALS("[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());
ASSERT_EQUALS("[test.cpp:7]: (style) Variable 'b' is assigned a value that is never used\n"
"[test.cpp:9]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
}
void localvaralias2() { // ticket 1637
@ -2180,7 +2180,7 @@ private:
" }\n"
" b(srcdata);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'buf' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'buf' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -2193,7 +2193,7 @@ private:
" srcdata = vdata;\n"
" b(srcdata);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'buf' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'buf' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -2246,7 +2246,7 @@ private:
" }\n"
" b(srcdata);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'buf' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:7]: (style) Variable 'buf' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -2260,7 +2260,7 @@ private:
" srcdata = vdata;\n"
" b(srcdata);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'buf' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:7]: (style) Variable 'buf' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n"
"{\n"
@ -2501,7 +2501,7 @@ private:
" ref[0] = 123;\n"
"}",
"test.c");
ASSERT_EQUALS("[test.c:3]: (style) Variable 'foo' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.c:5]: (style) Variable 'foo' is assigned a value that is never used\n", errout.str());
}
void localvaralias10() { // ticket 2004
@ -2703,7 +2703,7 @@ private:
" struct X x[10];\n"
" x[0].a = 0;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'x' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'x' is assigned a value that is never used\n", errout.str());
}
void localvarOp() {
@ -2757,7 +2757,7 @@ private:
" int b = 2;\n"
" a |= b;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo() {\n"
" int a = 1;\n"
@ -2900,7 +2900,7 @@ private:
" b[1] = 1;\n"
" return x ? a : c;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
}
void localvarextern() {
@ -3195,7 +3195,7 @@ private:
" std::string s;\n"
" s = \"foo\";\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 's' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo() {\n"
" std::string s = \"foo\";\n"