From cc65242626296c28a1d15274da3fb40c01835a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 12 Jan 2009 18:52:43 +0000 Subject: [PATCH] errmsg: added message about bad usage of char variable --- src/checkother.cpp | 8 ++------ src/cppcheck.cpp | 7 ++----- src/errormessage.h | 18 ++++++++++++++++++ tools/errmsg.cpp | 2 ++ 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/checkother.cpp b/src/checkother.cpp index 6fa235005..aefd5eae9 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -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; } } diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index fa2d2b135..4351a0719 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -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(); diff --git a/src/errormessage.h b/src/errormessage.h index 45186bc26..67fadac7d 100644 --- a/src/errormessage.h +++ b/src/errormessage.h @@ -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 diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp index bd12dbb4d..224aa9141 100644 --- a/tools/errmsg.cpp +++ b/tools/errmsg.cpp @@ -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;