errmsg: added message about bad usage of char variable

This commit is contained in:
Daniel Marjamäki 2009-01-12 18:52:43 +00:00
parent ee54d4da91
commit cc65242626
4 changed files with 24 additions and 11 deletions

View File

@ -737,9 +737,7 @@ void CheckOther::CheckCharVariable()
std::string temp = "%var% [ " + tok->str() + " ]";
if ((tok2->str() != ".") && Token::Match(tok2->next(), temp.c_str()))
{
std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(tok2->next()) << ": Warning - using char variable as array index";
_errorLogger->reportErr(errmsg.str());
_errorLogger->reportErr(ErrorMessage::charArrayIndex(_tokenizer, tok2->next()));
break;
}
@ -747,9 +745,7 @@ void CheckOther::CheckCharVariable()
std::string tempSecond = tok->str() + " [&|]";
if (Token::Match(tok2, tempFirst.c_str()) || Token::Match(tok2, tempSecond.c_str()))
{
std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(tok2) << ": Warning - using char variable in bit operation";
_errorLogger->reportErr(errmsg.str());
_errorLogger->reportErr(ErrorMessage::charBitOp(_tokenizer, tok2));
break;
}
}

View File

@ -244,13 +244,10 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
if (ErrorMessage::udivWarning(_settings) || ErrorMessage::udivError(_settings))
checkOther.CheckUnsignedDivision();
if (_settings._checkCodingStyle)
{
// Give warning when using char variable as array index
// Give warning when using char variable as array index
if (ErrorMessage::charArrayIndex(_settings) || ErrorMessage::charBitOp(_settings))
checkOther.CheckCharVariable();
}
// Usage of local variables
if (ErrorMessage::unusedVariable(_settings))
checkOther.functionVariableUsage();

View File

@ -282,5 +282,23 @@ public:
return s._checkCodingStyle;
}
static std::string charArrayIndex(const Tokenizer *tokenizer, const Token *Location)
{
return msg1(tokenizer, Location) + "Warning - using char variable as array index";
}
static bool charArrayIndex(const Settings &s)
{
return s._checkCodingStyle;
}
static std::string charBitOp(const Tokenizer *tokenizer, const Token *Location)
{
return msg1(tokenizer, Location) + "Warning - using char variable in bit operation";
}
static bool charBitOp(const Settings &s)
{
return s._checkCodingStyle;
}
};
#endif

View File

@ -90,6 +90,8 @@ int main()
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"));
err.push_back(Message("constStatement", Message::STYLE, "Redundant code: Found a statement that begins with %1 constant", "type"));
err.push_back(Message("charArrayIndex", Message::STYLE, "Warning - using char variable as array index"));
err.push_back(Message("charBitOp", Message::STYLE, "Warning - using char variable in bit operation"));
// Generate code..
std::cout << "Generate code.." << std::endl;