errmsg: Added messages for 'variable is not used / not read / not assigned'

This commit is contained in:
Daniel Marjamäki 2009-01-12 17:40:20 +00:00
parent 13e2396345
commit a5eb8894d4
4 changed files with 38 additions and 11 deletions

View File

@ -938,23 +938,17 @@ void CheckOther::functionVariableUsage()
if (usage == USAGE_DECLARE) if (usage == USAGE_DECLARE)
{ {
std::ostringstream errmsg; _errorLogger->reportErr(ErrorMessage::unusedVariable(_tokenizer, tok1->next(), varname));
errmsg << _tokenizer->fileLine(tok1->next()) << ": Unused variable '" << varname << "'";
_errorLogger->reportErr(errmsg.str());
} }
else if (!(usage & USAGE_READ)) else if (!(usage & USAGE_READ))
{ {
std::ostringstream errmsg; _errorLogger->reportErr(ErrorMessage::unreadVariable(_tokenizer, tok1->next(), varname));
errmsg << _tokenizer->fileLine(tok1->next()) << ": Variable '" << varname << "' is assigned a value that is never used";
_errorLogger->reportErr(errmsg.str());
} }
else if (!(usage & USAGE_WRITE)) else if (!(usage & USAGE_WRITE))
{ {
std::ostringstream errmsg; _errorLogger->reportErr(ErrorMessage::unassignedVariable(_tokenizer, tok1->next(), varname));
errmsg << _tokenizer->fileLine(tok1->next()) << ": Variable '" << varname << "' is not assigned a value";
_errorLogger->reportErr(errmsg.str());
} }
} }
} }

View File

@ -249,10 +249,13 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
// Give warning when using char variable as array index // Give warning when using char variable as array index
checkOther.CheckCharVariable(); checkOther.CheckCharVariable();
// Usage of local variables
checkOther.functionVariableUsage();
} }
// Usage of local variables
if (ErrorMessage::unusedVariable(_settings))
checkOther.functionVariableUsage();
_tokenizer.simplifyTokenList(); _tokenizer.simplifyTokenList();

View File

@ -246,5 +246,32 @@ public:
return true; return true;
} }
static std::string unusedVariable(const Tokenizer *tokenizer, const Token *Location, const std::string &varname)
{
return msg1(tokenizer, Location) + "Unused variable '" + varname + "'";
}
static bool unusedVariable(const Settings &s)
{
return s._checkCodingStyle;
}
static std::string unreadVariable(const Tokenizer *tokenizer, const Token *Location, const std::string &varname)
{
return msg1(tokenizer, Location) + "Variable '" + varname + "' is assigned a value that is never used";
}
static bool unreadVariable(const Settings &s)
{
return s._checkCodingStyle;
}
static std::string unassignedVariable(const Tokenizer *tokenizer, const Token *Location, const std::string &varname)
{
return msg1(tokenizer, Location) + "Variable '" + varname + "' is not assigned a value";
}
static bool unassignedVariable(const Settings &s)
{
return s._checkCodingStyle;
}
}; };
#endif #endif

View File

@ -86,6 +86,9 @@ int main()
err.push_back(Message("unusedStructMember", Message::STYLE, "struct or union member '%1::%2' is never used", "structname", "varname")); err.push_back(Message("unusedStructMember", Message::STYLE, "struct or union member '%1::%2' is never used", "structname", "varname"));
err.push_back(Message("unreachableCode", 0, "Unreachable code below a 'return'")); err.push_back(Message("unreachableCode", 0, "Unreachable code below a 'return'"));
err.push_back(Message("passedByValue", 0, "Function parameter '%1' is passed by value. It could be passed by reference instead.", "parname")); err.push_back(Message("passedByValue", 0, "Function parameter '%1' is passed by value. It could be passed by reference instead.", "parname"));
err.push_back(Message("unusedVariable", Message::STYLE, "Unused variable '%1'", "varname"));
err.push_back(Message("unreadVariable", Message::STYLE, "Variable '%1' is assigned a value that is never used", "varname"));
err.push_back(Message("unassignedVariable", Message::STYLE, "Variable '%1' is not assigned a value", "varname"));
// Generate code.. // Generate code..
std::cout << "Generate code.." << std::endl; std::cout << "Generate code.." << std::endl;