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 */ /** Store information about variable usage */
class VariableUsage { class VariableUsage {
public: public:
VariableUsage(const Token *name = 0, VariableUsage(const Variable *var = 0,
VariableType type = standard, VariableType type = standard,
const Scope *scope = NULL,
bool read = false, bool read = false,
bool write = false, bool write = false,
bool modified = false, bool modified = false,
bool allocateMemory = false) : bool allocateMemory = false) :
_name(name), _var(var),
_scope(scope), _lastAccess(var?var->nameToken():0),
_type(type), _type(type),
_read(read), _read(read),
_write(write), _write(write),
@ -68,8 +67,8 @@ public:
std::set<unsigned int> _aliases; std::set<unsigned int> _aliases;
std::set<const Scope*> _assignments; std::set<const Scope*> _assignments;
const Token *_name; const Variable* _var;
const Scope *_scope; const Token* _lastAccess;
VariableType _type; VariableType _type;
bool _read; bool _read;
bool _write; bool _write;
@ -85,16 +84,16 @@ public:
const VariableMap &varUsage() const { const VariableMap &varUsage() const {
return _varUsage; return _varUsage;
} }
void addVar(const Token *name, VariableType type, const Scope *scope, bool write_); void addVar(const Variable *var, VariableType type, bool write_);
void allocateMemory(unsigned int varid); void allocateMemory(unsigned int varid, const Token* tok);
void read(unsigned int varid); void read(unsigned int varid, const Token* tok);
void readAliases(unsigned int varid); void readAliases(unsigned int varid, const Token* tok);
void readAll(unsigned int varid); void readAll(unsigned int varid, const Token* tok);
void write(unsigned int varid); void write(unsigned int varid, const Token* tok);
void writeAliases(unsigned int varid); void writeAliases(unsigned int varid, const Token* tok);
void writeAll(unsigned int varid); void writeAll(unsigned int varid, const Token* tok);
void use(unsigned int varid); void use(unsigned int varid, const Token* tok);
void modified(unsigned int varid); void modified(unsigned int varid, const Token* tok);
VariableUsage *find(unsigned int varid); VariableUsage *find(unsigned int varid);
void alias(unsigned int varid1, unsigned int varid2, bool replace); void alias(unsigned int varid1, unsigned int varid2, bool replace);
void erase(unsigned int varid) { void erase(unsigned int varid) {
@ -134,7 +133,7 @@ void Variables::alias(unsigned int varid1, unsigned int varid2, bool replace)
VariableUsage *temp = find(*i); VariableUsage *temp = find(*i);
if (temp) if (temp)
temp->_aliases.erase(var1->_name->varId()); temp->_aliases.erase(var1->_var->varId());
} }
// remove all aliases from var1 // remove all aliases from var1
@ -167,7 +166,7 @@ void Variables::clearAliases(unsigned int varid)
VariableUsage *temp = find(*i); VariableUsage *temp = find(*i);
if (temp) if (temp)
temp->_aliases.erase(usage->_name->varId()); temp->_aliases.erase(usage->_var->varId());
} }
// remove all aliases from usage // remove all aliases from usage
@ -193,32 +192,35 @@ void Variables::eraseAll(unsigned int varid)
erase(varid); erase(varid);
} }
void Variables::addVar(const Token *name, void Variables::addVar(const Variable *var,
VariableType type, VariableType type,
const Scope *scope,
bool write_) bool write_)
{ {
if (name->varId() > 0) if (var->varId() > 0)
_varUsage.insert(std::make_pair(name->varId(), VariableUsage(name, type, scope, false, write_, false))); _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); VariableUsage *usage = find(varid);
if (usage) if (usage) {
usage->_allocateMemory = true; 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); VariableUsage *usage = find(varid);
if (usage) if (usage) {
usage->_read = true; 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); VariableUsage *usage = find(varid);
@ -228,41 +230,47 @@ void Variables::readAliases(unsigned int varid)
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
VariableUsage *aliased = find(*aliases); VariableUsage *aliased = find(*aliases);
if (aliased) if (aliased) {
aliased->_read = true; 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); VariableUsage *usage = find(varid);
if (usage) { if (usage) {
usage->_read = true; usage->_read = true;
usage->_lastAccess = tok;
std::set<unsigned int>::iterator aliases; std::set<unsigned int>::iterator aliases;
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
VariableUsage *aliased = find(*aliases); VariableUsage *aliased = find(*aliases);
if (aliased) if (aliased) {
aliased->_read = true; 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); VariableUsage *usage = find(varid);
if (usage) { if (usage) {
usage->_write = true; usage->_write = true;
usage->_read = false; 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); VariableUsage *usage = find(varid);
@ -272,62 +280,73 @@ void Variables::writeAliases(unsigned int varid)
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
VariableUsage *aliased = find(*aliases); VariableUsage *aliased = find(*aliases);
if (aliased) if (aliased) {
aliased->_write = true; 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); VariableUsage *usage = find(varid);
if (usage) { if (usage) {
usage->_write = true; usage->_write = true;
usage->_lastAccess = tok;
std::set<unsigned int>::iterator aliases; std::set<unsigned int>::iterator aliases;
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
VariableUsage *aliased = find(*aliases); VariableUsage *aliased = find(*aliases);
if (aliased) if (aliased) {
aliased->_write = true; 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); VariableUsage *usage = find(varid);
if (usage) { if (usage) {
usage->use(); usage->use();
usage->_lastAccess = tok;
std::set<unsigned int>::iterator aliases; std::set<unsigned int>::iterator aliases;
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
VariableUsage *aliased = find(*aliases); VariableUsage *aliased = find(*aliases);
if (aliased) if (aliased) {
aliased->use(); aliased->use();
aliased->_lastAccess = tok;
}
} }
} }
} }
void Variables::modified(unsigned int varid) void Variables::modified(unsigned int varid, const Token* tok)
{ {
VariableUsage *usage = find(varid); VariableUsage *usage = find(varid);
if (usage) { if (usage) {
usage->_modified = true; usage->_modified = true;
usage->_lastAccess = tok;
std::set<unsigned int>::iterator aliases; std::set<unsigned int>::iterator aliases;
for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) { for (aliases = usage->_aliases.begin(); aliases != usage->_aliases.end(); ++aliases) {
VariableUsage *aliased = find(*aliases); VariableUsage *aliased = find(*aliases);
if (aliased) if (aliased) {
aliased->_modified = true; aliased->_modified = true;
aliased->_lastAccess = tok;
}
} }
} }
} }
@ -360,7 +379,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de
tok = tok->next(); tok = tok->next();
while (tok->str() != "=") { while (tok->str() != "=") {
if (tok->varId()) if (tok->varId())
variables.read(tok->varId()); variables.read(tok->varId(), tok);
tok = tok->next(); tok = tok->next();
} }
tok = tok->next(); tok = tok->next();
@ -372,7 +391,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de
bool addressOf = false; bool addressOf = false;
if (Token::Match(tok, "%var% .")) 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 // check for C style cast
if (tok->str() == "(") { 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 (var2) { // local variable (alias or read it)
if (var1->_type == Variables::pointer || var1->_type == Variables::pointerArray) { if (var1->_type == Variables::pointer || var1->_type == Variables::pointerArray) {
if (dereference) if (dereference)
variables.read(varid2); variables.read(varid2, tok);
else { else {
if (addressOf || if (addressOf ||
var2->_type == Variables::array || var2->_type == Variables::array ||
@ -449,7 +468,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de
replace = false; replace = false;
// check if variable declared in same scope // check if variable declared in same scope
else if (scope == var1->_scope) else if (scope == var1->_var->scope())
replace = true; replace = true;
// not in same scope as declaration // 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); variables.alias(varid1, varid2, replace);
} else if (tok->strAt(1) == "?") { } else if (tok->strAt(1) == "?") {
if (var2->_type == Variables::reference) if (var2->_type == Variables::reference)
variables.readAliases(varid2); variables.readAliases(varid2, tok);
else else
variables.read(varid2); variables.read(varid2, tok);
} else { } else {
variables.read(varid2); variables.read(varid2, tok);
} }
} }
} else if (var1->_type == Variables::reference) { } else if (var1->_type == Variables::reference) {
variables.alias(varid1, varid2, true); variables.alias(varid1, varid2, true);
} else { } else {
if (var2->_type == Variables::pointer && tok->strAt(1) == "[") 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) } else { // not a local variable (or an unsupported local variable)
if (var1->_type == Variables::pointer && !dereference) { if (var1->_type == Variables::pointer && !dereference) {
// check if variable declaration is in this scope // check if variable declaration is in this scope
if (var1->_scope == scope) if (var1->_var->scope() == scope)
variables.clearAliases(varid1); variables.clearAliases(varid1);
else { else {
// no other assignment in this scope // 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) void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const scope, Variables& variables, bool insideLoop, std::vector<unsigned int> &usedVariables)
{ {
// Find declarations if the scope is executable.. // 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 // Find declarations
for (std::list<Variable>::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) { for (std::list<Variable>::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) {
if (i->isThrow() || i->isExtern()) if (i->isThrow() || i->isExtern())
@ -629,27 +648,27 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
if (defValTok->str() == "[") if (defValTok->str() == "[")
defValTok = defValTok->link(); defValTok = defValTok->link();
else if (defValTok->str() == "(" || defValTok->str() == "=") { else if (defValTok->str() == "(" || defValTok->str() == "=") {
variables.addVar(i->nameToken(), type, scope, true); variables.addVar(&*i, type, true);
break; break;
} else if (defValTok->str() == ";" || defValTok->str() == "," || defValTok->str() == ")") { } else if (defValTok->str() == ";" || defValTok->str() == "," || defValTok->str() == ")") {
variables.addVar(i->nameToken(), type, scope, i->isStatic()); variables.addVar(&*i, type, i->isStatic());
break; break;
} }
} }
if (i->isArray() && i->isClass()) // Array of class/struct members. Initialized by ctor. 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. 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 && defValTok->str() == "=") {
if (defValTok->next() && defValTok->next()->str() == "{") { if (defValTok->next() && defValTok->next()->str() == "{") {
for (const Token* tok = defValTok; tok && tok != defValTok->linkAt(1); tok = tok->next()) 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. if (Token::Match(tok, "%var%")) // Variables used to initialize the array read.
variables.read(tok->varId()); variables.read(tok->varId(), i->nameToken());
} else } else
doAssignment(variables, i->nameToken(), false, scope); doAssignment(variables, i->nameToken(), false, scope);
} else if (Token::Match(defValTok, "( %var% )")) // Variables used to initialize the variable read. } 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; insideLoop = false;
std::vector<unsigned int>::iterator it; std::vector<unsigned int>::iterator it;
for (it = usedVariables.begin(); it != usedVariables.end(); it++) { for (it = usedVariables.begin(); it != usedVariables.end(); it++) {
variables.read((*it)); variables.read((*it), tok);
} }
tok = (*i)->classStart->link(); tok = (*i)->classStart->link();
break; break;
@ -728,21 +747,21 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
Variables::VariableUsage *var = variables.find(varid); Variables::VariableUsage *var = variables.find(varid);
if (var && !var->_allocateMemory) { if (var && !var->_allocateMemory) {
variables.readAll(varid); variables.readAll(varid, tok);
} }
} }
else if (Token::Match(tok, "return|throw")) { else if (Token::Match(tok, "return|throw")) {
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->varId()) if (tok2->varId())
variables.readAll(tok2->varId()); variables.readAll(tok2->varId(), tok);
else if (tok2->str() == ";") else if (tok2->str() == ";")
break; break;
} }
} }
else if (Token::Match(tok->tokAt(-2), "while|if") && Token::Match(tok->tokAt(1), "=") && tok->varId() && tok->varId() == tok->tokAt(2)->varId()) { 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 // assignment
else if (!Token::Match(tok->tokAt(-2), "[;{}.] %var% (") && 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); tok = doAssignment(variables, tok, dereference, scope);
if (pre || post) if (pre || post)
variables.use(varid1); variables.use(varid1, tok);
if (dereference) { if (dereference) {
Variables::VariableUsage *var = variables.find(varid1); Variables::VariableUsage *var = variables.find(varid1);
if (var && var->_type == Variables::array) if (var && var->_type == Variables::array)
variables.write(varid1); variables.write(varid1, tok);
variables.writeAliases(varid1); variables.writeAliases(varid1, tok);
variables.read(varid1); variables.read(varid1, tok);
} else { } else {
Variables::VariableUsage *var = variables.find(varid1); Variables::VariableUsage *var = variables.find(varid1);
if (var && var->_type == Variables::reference) { if (var && var->_type == Variables::reference) {
variables.writeAliases(varid1); variables.writeAliases(varid1, tok);
variables.read(varid1); variables.read(varid1, tok);
} }
// Consider allocating memory separately because allocating/freeing alone does not constitute using the variable // Consider allocating memory separately because allocating/freeing alone does not constitute using the variable
else if (var && var->_type == Variables::pointer && else if (var && var->_type == Variables::pointer &&
@ -813,26 +832,26 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
} }
if (allocate) if (allocate)
variables.allocateMemory(varid1); variables.allocateMemory(varid1, tok);
else else
variables.write(varid1); variables.write(varid1, tok);
} else if (varid1 && Token::Match(tok, "%varid% .", varid1)) { } else if (varid1 && Token::Match(tok, "%varid% .", varid1)) {
variables.use(varid1); variables.use(varid1, tok);
} else { } else {
variables.write(varid1); variables.write(varid1, tok);
} }
Variables::VariableUsage *var2 = variables.find(tok->varId()); Variables::VariableUsage *var2 = variables.find(tok->varId());
if (var2) { if (var2) {
if (var2->_type == Variables::reference) { if (var2->_type == Variables::reference) {
variables.writeAliases(tok->varId()); variables.writeAliases(tok->varId(), tok);
variables.read(tok->varId()); variables.read(tok->varId(), tok);
} else if (tok->varId() != varid1 && Token::Match(tok, "%var% .")) } else if (tok->varId() != varid1 && Token::Match(tok, "%var% ."))
variables.read(tok->varId()); variables.read(tok->varId(), tok);
else if (tok->varId() != varid1 && else if (tok->varId() != varid1 &&
var2->_type == Variables::standard && var2->_type == Variables::standard &&
tok->strAt(-1) != "&") 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 // Consider allocating memory separately because allocating/freeing alone does not constitute using the variable
if (var->_type == Variables::pointer && 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")) { 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) { } else if (var->_type == Variables::pointer || var->_type == Variables::reference) {
variables.read(varid); variables.read(varid, tok);
variables.writeAliases(varid); variables.writeAliases(varid, tok);
} else if (var->_type == Variables::pointerArray) { } else if (var->_type == Variables::pointerArray) {
tok = doAssignment(variables, tok, false, scope); tok = doAssignment(variables, tok, false, scope);
} else } else
variables.writeAll(varid); variables.writeAll(varid, tok);
} }
} }
else if (Token::Match(tok, "& %var%")) { else if (Token::Match(tok, "& %var%")) {
if (tok->previous()->isName() || tok->previous()->isNumber()) { // bitop if (tok->previous()->isName() || tok->previous()->isNumber()) { // bitop
variables.read(tok->next()->varId()); variables.read(tok->next()->varId(), tok);
} else // addressof } 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()); usedVariables.push_back(tok->next()->varId());
} else if (Token::Match(tok, ">> %var%")) { } 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()); usedVariables.push_back(tok->next()->varId());
} else if (Token::Match(tok, "%var% >>|&") && Token::Match(tok->previous(), "[{};:]")) { } 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()); usedVariables.push_back(tok->next()->varId());
} }
// function parameter // function parameter
else if (Token::Match(tok, "[(,] %var% [")) { 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()); usedVariables.push_back(tok->next()->varId());
} else if (Token::Match(tok, "[(,] %var% [,)]") && tok->previous()->str() != "*") { } 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()); usedVariables.push_back(tok->next()->varId());
} else if (Token::Match(tok, "[(,] (") && } else if (Token::Match(tok, "[(,] (") &&
Token::Match(tok->next()->link(), ") %var% [,)]")) { 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()); usedVariables.push_back(tok->next()->link()->next()->varId());
} }
// function // function
else if (Token::Match(tok, "%var% (")) { else if (Token::Match(tok, "%var% (")) {
variables.read(tok->varId()); variables.read(tok->varId(), tok);
usedVariables.push_back(tok->varId()); usedVariables.push_back(tok->varId());
if (Token::Match(tok->tokAt(2), "%var% =")) { 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()); usedVariables.push_back(tok->tokAt(2)->varId());
} }
} }
else if (Token::Match(tok, "[{,] %var% [,}]")) { else if (Token::Match(tok, "[{,] %var% [,}]")) {
variables.read(tok->next()->varId()); variables.read(tok->next()->varId(), tok);
usedVariables.push_back(tok->next()->varId()); usedVariables.push_back(tok->next()->varId());
} }
else if (Token::Match(tok, "%var% .")) { 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()); usedVariables.push_back(tok->varId());
} }
else if (tok->isExtendedOp() && else if (tok->isExtendedOp() &&
Token::Match(tok->next(), "%var%") && !Token::Match(tok->next(), "true|false|new") && tok->strAt(2) != "=") { 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()); usedVariables.push_back(tok->next()->varId());
} }
else if (Token::Match(tok, "%var%") && tok->next() && (tok->next()->str() == ")" || tok->next()->isExtendedOp())) { 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()); usedVariables.push_back(tok->varId());
} }
else if (Token::Match(tok, "%var% ;") && Token::Match(tok->previous(), "[;{}:]")) { else if (Token::Match(tok, "%var% ;") && Token::Match(tok->previous(), "[;{}:]")) {
variables.readAll(tok->varId()); variables.readAll(tok->varId(), tok);
usedVariables.push_back(tok->varId()); usedVariables.push_back(tok->varId());
} }
else if (Token::Match(tok, "++|-- %var%")) { else if (Token::Match(tok, "++|-- %var%")) {
if (!Token::Match(tok->previous(), "[;{}:]")) if (!Token::Match(tok->previous(), "[;{}:]"))
variables.use(tok->next()->varId()); variables.use(tok->next()->varId(), tok);
else else
variables.modified(tok->next()->varId()); variables.modified(tok->next()->varId(), tok);
usedVariables.push_back(tok->next()->varId()); usedVariables.push_back(tok->next()->varId());
} }
else if (Token::Match(tok, "%var% ++|--")) { else if (Token::Match(tok, "%var% ++|--")) {
if (!Token::Match(tok->previous(), "[;{}:]")) if (!Token::Match(tok->previous(), "[;{}:]"))
variables.use(tok->varId()); variables.use(tok->varId(), tok);
else else
variables.modified(tok->varId()); variables.modified(tok->varId(), tok);
usedVariables.push_back(tok->varId()); 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()) { for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
if (tok2->varId()) { if (tok2->varId()) {
if (tok2->next()->isAssignmentOp()) if (tok2->next()->isAssignmentOp())
variables.write(tok2->varId()); variables.write(tok2->varId(), tok);
else else
variables.read(tok2->varId()); variables.read(tok2->varId(), tok);
usedVariables.push_back(tok2->varId()); usedVariables.push_back(tok2->varId());
} }
} }
@ -992,11 +1011,11 @@ void CheckUnusedVar::checkFunctionVariableUsage()
// Check usage of all variables in the current scope.. // Check usage of all variables in the current scope..
for (Variables::VariableMap::const_iterator it = variables.varUsage().begin(); it != variables.varUsage().end(); ++it) { for (Variables::VariableMap::const_iterator it = variables.varUsage().begin(); it != variables.varUsage().end(); ++it) {
const Variables::VariableUsage &usage = it->second; 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); const Variable* var = symbolDatabase->getVariableFromVarId(it->first);
// variable has been marked as unused so ignore it // variable has been marked as unused so ignore it
if (usage._name->isUnused()) if (usage._var->nameToken()->isUnused())
continue; continue;
// skip things that are only partially implemented to prevent false positives // 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 // variable has had memory allocated for it, but hasn't done
// anything with that memory other than, perhaps, freeing it // anything with that memory other than, perhaps, freeing it
if (usage.unused() && !usage._modified && usage._allocateMemory) if (usage.unused() && !usage._modified && usage._allocateMemory)
allocatedButUnusedVariableError(usage._name, varname); allocatedButUnusedVariableError(usage._lastAccess, varname);
// variable has not been written, read, or modified // variable has not been written, read, or modified
else if (usage.unused() && !usage._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 // variable has not been written but has been modified
else if (usage._modified && !usage._write && !usage._allocateMemory && !Token::simpleMatch(var->typeStartToken(), "std ::")) 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 // variable has been written but not read
else if (!usage._read && !usage._modified) else if (!usage._read && !usage._modified)
unreadVariableError(usage._name, varname); unreadVariableError(usage._lastAccess, varname);
// variable has been read but not written // variable has been read but not written
else if (!usage._write && !usage._allocateMemory && !Token::simpleMatch(var->typeStartToken(), "std ::")) 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" " char *i;\n"
" i = fgets();\n" " i = fgets();\n"
"}\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 // undefined variables are not reported because they may be classes with constructors
functionVariableUsage("undefined foo()\n" functionVariableUsage("undefined foo()\n"
@ -609,7 +609,7 @@ private:
" d += code;\n" " d += code;\n"
" }\n" " }\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -630,7 +630,7 @@ private:
" d += code;\n" " d += code;\n"
" }\n" " }\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -805,7 +805,7 @@ private:
" ;\n" " ;\n"
" else i = 0;\n" " else i = 0;\n"
"}\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() { void localvar4() {
@ -840,7 +840,7 @@ private:
" for (int i=0;i<10;++i)\n" " for (int i=0;i<10;++i)\n"
" b[i] = 0;\n" " b[i] = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -849,7 +849,7 @@ private:
" for (int i=0;i<10;++i)\n" " for (int i=0;i<10;++i)\n"
" b[i] = ++a;\n" " b[i] = ++a;\n"
"}\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 void localvar7() { // ticket 1253
@ -984,7 +984,7 @@ private:
" int &j = i;\n" " int &j = i;\n"
" j = 0;\n" " j = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1052,16 +1052,16 @@ private:
" a[0] = 0;\n" " a[0] = 0;\n"
" x = a[0];\n" " x = a[0];\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
" int a, b, c;\n" " int a, b, c;\n"
" a = b = c = f();\n" " a = b = c = f();\n"
"}\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:3]: (style) Variable 'b' 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", errout.str()); "[test.cpp:4]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
functionVariableUsage("int * foo()\n" functionVariableUsage("int * foo()\n"
"{\n" "{\n"
@ -1078,7 +1078,7 @@ private:
" for (int i = 0; i < 10; )\n" " for (int i = 0; i < 10; )\n"
" a[i++] = 0;\n" " a[i++] = 0;\n"
"}\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() { void localvar10() {
@ -1130,7 +1130,7 @@ private:
" }\n" " }\n"
" i = 0;\n" " i = 0;\n"
"}\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:5]: (style) Unused variable: i\n"
"[test.cpp:7]: (style) Unused variable: i\n", errout.str()); "[test.cpp:7]: (style) Unused variable: i\n", errout.str());
} }
@ -1173,12 +1173,12 @@ private:
"\n" "\n"
"}\n"); "}\n");
ASSERT_EQUALS( ASSERT_EQUALS(
"[test.cpp:3]: (style) Variable 'a' 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:3]: (style) Variable 'b' 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:4]: (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:4]: (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:4]: (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 'f' is assigned a value that is never used\n",
errout.str()); errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
@ -1189,12 +1189,12 @@ private:
"}\n"); "}\n");
TODO_ASSERT_EQUALS( TODO_ASSERT_EQUALS(
"[test.cpp:3]: (style) Variable 'a' 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:3]: (style) Variable 'b' 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 '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:4]: (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 'b' is assigned a value that is never used\n",
errout.str()); errout.str());
} }
@ -1204,7 +1204,7 @@ private:
" int x;\n" " int x;\n"
" x = obj->ySize / 8;\n" " x = obj->ySize / 8;\n"
"}\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() { void localvar14() {
@ -1270,7 +1270,7 @@ private:
" char *ptr = buf;\n" " char *ptr = buf;\n"
" *(ptr++) = 0;\n" " *(ptr++) = 0;\n"
"}\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" functionVariableUsage("int foo()\n"
"{\n" "{\n"
@ -1313,7 +1313,7 @@ private:
" data->info = k;\n" " data->info = k;\n"
" line_start = ptr;\n" " line_start = ptr;\n"
"}\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 void localvar18() { // ticket #1723
@ -1331,7 +1331,7 @@ private:
" c = *(a);\n" " c = *(a);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'a' is not assigned a value\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 void localvar20() { // ticket #1799
@ -1638,7 +1638,7 @@ private:
" int *b = &a;\n" " int *b = &a;\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1646,7 +1646,7 @@ private:
" char *b = (char *)&a;\n" " char *b = (char *)&a;\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1654,7 +1654,7 @@ private:
" char *b = (char *)(&a);\n" " char *b = (char *)(&a);\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1662,7 +1662,7 @@ private:
" const char *b = (const char *)&a;\n" " const char *b = (const char *)&a;\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1670,7 +1670,7 @@ private:
" const char *b = (const char *)(&a);\n" " const char *b = (const char *)(&a);\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1678,7 +1678,7 @@ private:
" char *b = static_cast<char *>(&a);\n" " char *b = static_cast<char *>(&a);\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1686,7 +1686,7 @@ private:
" const char *b = static_cast<const char *>(&a);\n" " const char *b = static_cast<const char *>(&a);\n"
" *b = 0;\n" " *b = 0;\n"
"}\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 // a is not a local variable and b is aliased to it
functionVariableUsage("int a;\n" functionVariableUsage("int a;\n"
@ -1746,7 +1746,7 @@ private:
" int *b = a;\n" " int *b = a;\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1754,7 +1754,7 @@ private:
" char *b = (char *)a;\n" " char *b = (char *)a;\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1762,7 +1762,7 @@ private:
" char *b = (char *)(a);\n" " char *b = (char *)(a);\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1770,7 +1770,7 @@ private:
" const char *b = (const char *)a;\n" " const char *b = (const char *)a;\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1778,7 +1778,7 @@ private:
" const char *b = (const char *)(a);\n" " const char *b = (const char *)(a);\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1786,7 +1786,7 @@ private:
" char *b = static_cast<char *>(a);\n" " char *b = static_cast<char *>(a);\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1794,7 +1794,7 @@ private:
" const char *b = static_cast<const char *>(a);\n" " const char *b = static_cast<const char *>(a);\n"
" *b = 0;\n" " *b = 0;\n"
"}\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" functionVariableUsage("int a[10];\n"
"void foo()\n" "void foo()\n"
@ -1901,7 +1901,7 @@ private:
" int *c = b;\n" " int *c = b;\n"
" *c = 0;\n" " *c = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1912,7 +1912,7 @@ private:
" *d = 0;\n" " *d = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: a\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()); "[test.cpp:5]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
@ -1924,7 +1924,7 @@ private:
" *c = 0;\n" " *c = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Unused variable: a\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1936,15 +1936,15 @@ private:
" c = a;\n" " c = a;\n"
" *c = 0;\n" " *c = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n" ASSERT_EQUALS("[test.cpp:9]: (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()); "[test.cpp:7]: (style) Variable 'b' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
"{\n" "{\n"
" int a[10], * b = a + 10;\n" " int a[10], * b = a + 10;\n"
" b[-10] = 0;\n" " b[-10] = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -1952,7 +1952,7 @@ private:
" b[-10] = 0;\n" " b[-10] = 0;\n"
" int * c = b - 10;\n" " int * c = b - 10;\n"
"}\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()); "[test.cpp:5]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo()\n" functionVariableUsage("void foo()\n"
@ -1969,7 +1969,7 @@ private:
" int * c = b - 10;\n" " int * c = b - 10;\n"
" c[1] = 0;\n" " c[1] = 0;\n"
"}\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -2045,7 +2045,7 @@ private:
" *d = 0;\n" " *d = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Unused variable: b\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" functionVariableUsage("int a[10];\n"
"void foo()\n" "void foo()\n"
@ -2057,8 +2057,8 @@ private:
" d = a; *d = 0;\n" " d = a; *d = 0;\n"
" d = c; *d = 0;\n" " d = c; *d = 0;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'b' is assigned a value that is never used\n" ASSERT_EQUALS("[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()); "[test.cpp:9]: (style) Variable 'c' is assigned a value that is never used\n", errout.str());
} }
void localvaralias2() { // ticket 1637 void localvaralias2() { // ticket 1637
@ -2180,7 +2180,7 @@ private:
" }\n" " }\n"
" b(srcdata);\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -2193,7 +2193,7 @@ private:
" srcdata = vdata;\n" " srcdata = vdata;\n"
" b(srcdata);\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -2246,7 +2246,7 @@ private:
" }\n" " }\n"
" b(srcdata);\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -2260,7 +2260,7 @@ private:
" srcdata = vdata;\n" " srcdata = vdata;\n"
" b(srcdata);\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" functionVariableUsage("void foo()\n"
"{\n" "{\n"
@ -2501,7 +2501,7 @@ private:
" ref[0] = 123;\n" " ref[0] = 123;\n"
"}", "}",
"test.c"); "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 void localvaralias10() { // ticket 2004
@ -2703,7 +2703,7 @@ private:
" struct X x[10];\n" " struct X x[10];\n"
" x[0].a = 0;\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() { void localvarOp() {
@ -2757,7 +2757,7 @@ private:
" int b = 2;\n" " int b = 2;\n"
" a |= b;\n" " a |= b;\n"
"}\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" functionVariableUsage("void foo() {\n"
" int a = 1;\n" " int a = 1;\n"
@ -2900,7 +2900,7 @@ private:
" b[1] = 1;\n" " b[1] = 1;\n"
" return x ? a : c;\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() { void localvarextern() {
@ -3195,7 +3195,7 @@ private:
" std::string s;\n" " std::string s;\n"
" s = \"foo\";\n" " s = \"foo\";\n"
"}\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" functionVariableUsage("void foo() {\n"
" std::string s = \"foo\";\n" " std::string s = \"foo\";\n"