errormessage: Added a few more messages for checkother.cpp

This commit is contained in:
Daniel Marjamäki 2009-01-08 20:56:51 +00:00
parent 4dfc3a9c3d
commit 5d0f8f0cfa
6 changed files with 117 additions and 83 deletions

View File

@ -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 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 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 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 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 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 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 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 g++ $(CXXFLAGS) -c -o src/errormessage.o src/errormessage.cpp
src/filelister.o: src/filelister.cpp src/filelister.h src/filelister.o: src/filelister.cpp src/filelister.h

View File

@ -19,7 +19,7 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "checkother.h" #include "checkother.h"
#include "errormessage.h"
#include <list> #include <list>
#include <map> #include <map>
#include <sstream> #include <sstream>
@ -59,9 +59,7 @@ void CheckOther::WarningOldStylePointerCast()
if (!Token::findmatch(_tokenizer->tokens(), pattern.c_str())) if (!Token::findmatch(_tokenizer->tokens(), pattern.c_str()))
continue; continue;
std::ostringstream ostr; _errorLogger->reportErr( ErrorMessage::cstyleCast(_tokenizer, tok) );
ostr << _tokenizer->fileLine(tok) << ": C-style pointer casting";
_errorLogger->reportErr(ostr.str());
} }
} }
@ -142,9 +140,7 @@ void CheckOther::WarningRedundantCode()
if (err) if (err)
{ {
std::ostringstream ostr; _errorLogger->reportErr( ErrorMessage::redundantIfDelete0(_tokenizer, tok) );
ostr << _tokenizer->fileLine(tok) << ": Redundant condition. It is safe to deallocate a NULL pointer";
_errorLogger->reportErr(ostr.str());
} }
} }
@ -180,10 +176,7 @@ void CheckOther::redundantCondition2()
var2->str() == var3->str() && var2->str() == var3->str() &&
any1->str() == any2->str()) any1->str() == any2->str())
{ {
std::ostringstream errmsg; _errorLogger->reportErr( ErrorMessage::redundantIfRemove(_tokenizer, tok) );
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());
} }
tok = Token::findmatch(tok->next(), pattern); tok = Token::findmatch(tok->next(), pattern);

View File

@ -309,21 +309,26 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
checkOther.InvalidFunctionUsage(); 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) if (_settings._checkCodingStyle)
{ {
// Check that all private functions are called. // Check that all private functions are called.
checkClass.privateFunctions(); checkClass.privateFunctions();
// Warning upon c-style pointer casts
const char *ext = strrchr(FileName, '.');
if (ext && strcmp(ext, ".cpp") == 0)
checkOther.WarningOldStylePointerCast();
checkClass.operatorEq(); checkClass.operatorEq();
// if (a) delete a;
checkOther.WarningRedundantCode();
// if (condition); // if (condition);
checkOther.WarningIf(); checkOther.WarningIf();

View File

@ -32,13 +32,28 @@ private:
public: public:
static std::string memleak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) static std::string memleak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname)
{ return msg1(tokenizer, Location) + "Memory leak: " + varname + ""; } { return msg1(tokenizer, Location) + "Memory leak: " + varname + ""; }
static bool memleak(const Settings &s) static bool memleak(const Settings &s)
{ return true; } { return true; }
static std::string resourceLeak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) static std::string resourceLeak(const Tokenizer *tokenizer, const Token *Location, const std::string &varname)
{ return msg1(tokenizer, Location) + "Resource leak: " + varname + ""; } { return msg1(tokenizer, Location) + "Resource leak: " + varname + ""; }
static bool resourceLeak(const Settings &s) static bool resourceLeak(const Settings &s)
{ return true; } { 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 #endif

View File

@ -65,7 +65,7 @@ private:
" if (haystack.find(needle) != haystack.end())\n" " if (haystack.find(needle) != haystack.end())\n"
" haystack.remove(needle);" " haystack.remove(needle);"
"}\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());
} }
void remove2() void remove2()
@ -77,7 +77,7 @@ private:
" haystack.remove(needle);\n" " haystack.remove(needle);\n"
" }\n" " }\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());
} }
}; };

View File

@ -29,68 +29,17 @@ private:
std::string _par1; std::string _par1;
unsigned int _settings; unsigned int _settings;
std::string msg(bool code) const;
public: public:
Message(std::string funcname, unsigned int settings, std::string msg, std::string par1) Message(std::string funcname, unsigned int settings, std::string msg, std::string par1);
: _funcname(funcname), _msg(msg), _par1(par1), _settings(settings)
{ }
static const unsigned int ALL = 1; static const unsigned int ALL = 1;
static const unsigned int STYLE = 2; static const unsigned int STYLE = 2;
std::string msg(bool code) const void generateCode(std::ostream &ostr) 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 generateDoc(std::ostream &ostr, unsigned int i) const;
}; };
@ -100,9 +49,16 @@ int main()
{ {
// Error messages.. // Error messages..
std::list<Message> err; std::list<Message> err;
// checkmemoryleak.cpp..
err.push_back(Message("memleak", 0, "Memory leak: %1", "varname")); err.push_back(Message("memleak", 0, "Memory leak: %1", "varname"));
err.push_back(Message("resourceLeak", 0, "Resource 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.. // Generate code..
std::cout << "Generate code.." << std::endl; std::cout << "Generate code.." << std::endl;
std::ofstream fout("errormessage.h"); std::ofstream fout("errormessage.h");
@ -157,3 +113,68 @@ int main()
return 0; 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;
}
}