From 5d0f8f0cfa923bb663149a2ca49c8dfe5b98d715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 8 Jan 2009 20:56:51 +0000 Subject: [PATCH] errormessage: Added a few more messages for checkother.cpp --- Makefile | 8 +-- src/checkother.cpp | 15 ++--- src/cppcheck.cpp | 21 ++++--- src/errormessage.h | 19 +++++- test/testredundantif.cpp | 4 +- tools/errmsg.cpp | 133 ++++++++++++++++++++++----------------- 6 files changed, 117 insertions(+), 83 deletions(-) diff --git a/Makefile b/Makefile index a9cd0ce81..ef0ef376c 100644 --- a/Makefile +++ b/Makefile @@ -89,19 +89,19 @@ src/checkfunctionusage.o: src/checkfunctionusage.cpp src/checkfunctionusage.h sr src/checkheaders.o: src/checkheaders.cpp src/checkheaders.h src/tokenize.h src/settings.h src/errorlogger.h src/token.h g++ $(CXXFLAGS) -c -o src/checkheaders.o src/checkheaders.cpp -src/checkmemoryleak.o: src/checkmemoryleak.cpp src/checkmemoryleak.h src/tokenize.h src/settings.h src/errorlogger.h src/token.h +src/checkmemoryleak.o: src/checkmemoryleak.cpp src/checkmemoryleak.h src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/errormessage.h g++ $(CXXFLAGS) -c -o src/checkmemoryleak.o src/checkmemoryleak.cpp -src/checkother.o: src/checkother.cpp src/checkother.h src/tokenize.h src/settings.h src/errorlogger.h src/token.h +src/checkother.o: src/checkother.cpp src/checkother.h src/tokenize.h src/settings.h src/errorlogger.h src/token.h src/errormessage.h g++ $(CXXFLAGS) -c -o src/checkother.o src/checkother.cpp -src/cppcheck.o: src/cppcheck.cpp src/cppcheck.h src/settings.h src/errorlogger.h src/checkfunctionusage.h src/tokenize.h src/token.h src/preprocessor.h src/checkmemoryleak.h src/checkbufferoverrun.h src/checkclass.h src/checkheaders.h src/checkother.h src/filelister.h +src/cppcheck.o: src/cppcheck.cpp src/cppcheck.h src/settings.h src/errorlogger.h src/checkfunctionusage.h src/tokenize.h src/token.h src/preprocessor.h src/checkmemoryleak.h src/checkbufferoverrun.h src/checkclass.h src/checkheaders.h src/checkother.h src/filelister.h src/errormessage.h g++ $(CXXFLAGS) -c -o src/cppcheck.o src/cppcheck.cpp src/cppcheckexecutor.o: src/cppcheckexecutor.cpp src/cppcheckexecutor.h src/errorlogger.h src/cppcheck.h src/settings.h src/checkfunctionusage.h src/tokenize.h src/token.h g++ $(CXXFLAGS) -c -o src/cppcheckexecutor.o src/cppcheckexecutor.cpp -src/errormessage.o: src/errormessage.cpp src/errormessage.h +src/errormessage.o: src/errormessage.cpp src/errormessage.h src/settings.h src/tokenize.h src/errorlogger.h src/token.h g++ $(CXXFLAGS) -c -o src/errormessage.o src/errormessage.cpp src/filelister.o: src/filelister.cpp src/filelister.h diff --git a/src/checkother.cpp b/src/checkother.cpp index 6332b5ddf..b39406493 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -19,7 +19,7 @@ //--------------------------------------------------------------------------- #include "checkother.h" - +#include "errormessage.h" #include #include #include @@ -59,9 +59,7 @@ void CheckOther::WarningOldStylePointerCast() if (!Token::findmatch(_tokenizer->tokens(), pattern.c_str())) continue; - std::ostringstream ostr; - ostr << _tokenizer->fileLine(tok) << ": C-style pointer casting"; - _errorLogger->reportErr(ostr.str()); + _errorLogger->reportErr( ErrorMessage::cstyleCast(_tokenizer, tok) ); } } @@ -142,9 +140,7 @@ void CheckOther::WarningRedundantCode() if (err) { - std::ostringstream ostr; - ostr << _tokenizer->fileLine(tok) << ": Redundant condition. It is safe to deallocate a NULL pointer"; - _errorLogger->reportErr(ostr.str()); + _errorLogger->reportErr( ErrorMessage::redundantIfDelete0(_tokenizer, tok) ); } } @@ -180,10 +176,7 @@ void CheckOther::redundantCondition2() var2->str() == var3->str() && any1->str() == any2->str()) { - std::ostringstream errmsg; - errmsg << _tokenizer->fileLine(tok) - << ": Redundant condition found. The remove function in the STL will not do anything if element doesn't exist"; - _errorLogger->reportErr(errmsg.str()); + _errorLogger->reportErr( ErrorMessage::redundantIfRemove(_tokenizer, tok) ); } tok = Token::findmatch(tok->next(), pattern); diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index 290040eee..8cb5005bd 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -309,21 +309,26 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) checkOther.InvalidFunctionUsage(); + // Warning upon c-style pointer casts + if ( ErrorMessage::cstyleCast(_settings) ) + { + const char *ext = strrchr(FileName, '.'); + if (ext && strcmp(ext, ".cpp") == 0) + checkOther.WarningOldStylePointerCast(); + } + + // if (a) delete a; + if ( ErrorMessage::redundantIfDelete0(_settings) ) + checkOther.WarningRedundantCode(); + + if (_settings._checkCodingStyle) { // Check that all private functions are called. checkClass.privateFunctions(); - // Warning upon c-style pointer casts - const char *ext = strrchr(FileName, '.'); - if (ext && strcmp(ext, ".cpp") == 0) - checkOther.WarningOldStylePointerCast(); - checkClass.operatorEq(); - // if (a) delete a; - checkOther.WarningRedundantCode(); - // if (condition); checkOther.WarningIf(); diff --git a/src/errormessage.h b/src/errormessage.h index 2d057b2e4..01ac8ccc4 100644 --- a/src/errormessage.h +++ b/src/errormessage.h @@ -32,13 +32,28 @@ private: public: static std::string memleak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) { return msg1(tokenizer, Location) + "Memory leak: " + varname + ""; } - static bool memleak(const Settings &s) { return true; } + static std::string resourceLeak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) { return msg1(tokenizer, Location) + "Resource leak: " + varname + ""; } - static bool resourceLeak(const Settings &s) { return true; } + + static std::string cstyleCast(const Tokenizer *tokenizer, const Token *Location) + { return msg1(tokenizer, Location) + "C-style pointer casting"; } + static bool cstyleCast(const Settings &s) + { return & s._checkCodingStyle; } + + static std::string redundantIfDelete0(const Tokenizer *tokenizer, const Token *Location) + { return msg1(tokenizer, Location) + "Redundant condition. It is safe to deallocate a NULL pointer"; } + static bool redundantIfDelete0(const Settings &s) + { return & s._checkCodingStyle; } + + static std::string redundantIfRemove(const Tokenizer *tokenizer, const Token *Location) + { return msg1(tokenizer, Location) + "Redundant condition. The remove function in the STL will not do anything if element doesn't exist"; } + static bool redundantIfRemove(const Settings &s) + { return & s._checkCodingStyle; } + }; #endif diff --git a/test/testredundantif.cpp b/test/testredundantif.cpp index 33aae704d..5861c6b13 100644 --- a/test/testredundantif.cpp +++ b/test/testredundantif.cpp @@ -65,7 +65,7 @@ private: " if (haystack.find(needle) != haystack.end())\n" " haystack.remove(needle);" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant condition found. The remove function in the STL will not do anything if element doesn't exist\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant condition. The remove function in the STL will not do anything if element doesn't exist\n"), errout.str()); } void remove2() @@ -77,7 +77,7 @@ private: " haystack.remove(needle);\n" " }\n" "}\n"); - ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant condition found. The remove function in the STL will not do anything if element doesn't exist\n"), errout.str()); + ASSERT_EQUALS(std::string("[test.cpp:3]: Redundant condition. The remove function in the STL will not do anything if element doesn't exist\n"), errout.str()); } }; diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp index e8384bc1b..6eb2f2e14 100644 --- a/tools/errmsg.cpp +++ b/tools/errmsg.cpp @@ -29,68 +29,17 @@ private: std::string _par1; unsigned int _settings; + std::string msg(bool code) const; + public: - Message(std::string funcname, unsigned int settings, std::string msg, std::string par1) - : _funcname(funcname), _msg(msg), _par1(par1), _settings(settings) - { } + Message(std::string funcname, unsigned int settings, std::string msg, std::string par1); static const unsigned int ALL = 1; static const unsigned int STYLE = 2; - std::string msg(bool code) const - { - const char *str = code ? "\"" : ""; - std::string ret(str + _msg + str); - if (! _par1.empty()) - { - std::string::size_type pos = 0; - while ((pos = ret.find("%1", pos)) != std::string::npos) - { - ret.erase(pos, 2); - if (code) - ret.insert(pos, "\" + " + _par1 + " + \""); - else - ret.insert(pos, _par1); - } - } - return ret; - } - - void generateCode(std::ostream &ostr) const - { - // Error message.. - ostr << " static std::string " << _funcname << "(const Tokenizer *tokenizer, const Token *Location, "; - if (! _par1.empty()) - ostr << "const std::string &" << _par1; - ostr << ")\n"; - ostr << " { return msg1(tokenizer, Location) + " << msg(true) << "; }" << std::endl; - - // Settings.. - ostr << std::endl; - ostr << " static bool " << _funcname << "(const Settings &s)" << std::endl; - ostr << " { return "; - if (_settings == 0) - ostr << "true"; - else - { - if (_settings & ALL) - ostr << "s._showAll"; - if (_settings & (ALL | STYLE)) - ostr << " & "; - if (_settings & STYLE) - ostr << "s._checkCodingStyle"; - } - ostr << "; }" << std::endl; - } - - void generateDoc(std::ostream &ostr, unsigned int i) const - { - if (_settings == i) - { - ostr << " " << msg(false) << std::endl; - } - } + void generateCode(std::ostream &ostr) const; + void generateDoc(std::ostream &ostr, unsigned int i) const; }; @@ -100,9 +49,16 @@ int main() { // Error messages.. std::list err; + + // checkmemoryleak.cpp.. err.push_back(Message("memleak", 0, "Memory leak: %1", "varname")); err.push_back(Message("resourceLeak", 0, "Resource leak: %1", "varname")); + // checkother.cpp.. + err.push_back(Message("cstyleCast", Message::STYLE, "C-style pointer casting", "")); + err.push_back(Message("redundantIfDelete0", Message::STYLE, "Redundant condition. It is safe to deallocate a NULL pointer", "")); + err.push_back(Message("redundantIfRemove", Message::STYLE, "Redundant condition. The remove function in the STL will not do anything if element doesn't exist", "")); + // Generate code.. std::cout << "Generate code.." << std::endl; std::ofstream fout("errormessage.h"); @@ -157,3 +113,68 @@ int main() return 0; } + + + + +Message::Message(std::string funcname, unsigned int settings, std::string msg, std::string par1) + : _funcname(funcname), _msg(msg), _par1(par1), _settings(settings) +{ } + +std::string Message::msg(bool code) const +{ + const char *str = code ? "\"" : ""; + std::string ret(str + _msg + str); + if (! _par1.empty()) + { + std::string::size_type pos = 0; + while ((pos = ret.find("%1", pos)) != std::string::npos) + { + ret.erase(pos, 2); + if (code) + ret.insert(pos, "\" + " + _par1 + " + \""); + else + ret.insert(pos, _par1); + } + } + return ret; +} + +void Message::generateCode(std::ostream &ostr) const +{ + // Error message.. + ostr << " static std::string " << _funcname << "(const Tokenizer *tokenizer, const Token *Location"; + if (! _par1.empty()) + ostr << ", const std::string &" << _par1; + ostr << ")\n"; + ostr << " { return msg1(tokenizer, Location) + " << msg(true) << "; }" << std::endl; + + // Settings.. + ostr << " static bool " << _funcname << "(const Settings &s)" << std::endl; + ostr << " { return "; + if (_settings == 0) + ostr << "true"; + else + { + if (_settings & ALL) + ostr << "s._showAll"; + if (_settings & (ALL | STYLE)) + ostr << " & "; + if (_settings & STYLE) + ostr << "s._checkCodingStyle"; + } + ostr << "; }" << std::endl << std::endl; +} + +void Message::generateDoc(std::ostream &ostr, unsigned int i) const +{ + if (_settings == i) + { + ostr << " " << msg(false) << std::endl; + } +} + + + + +