errormessage: Added a few more messages for checkother.cpp
This commit is contained in:
parent
4dfc3a9c3d
commit
5d0f8f0cfa
8
Makefile
8
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
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
//---------------------------------------------------------------------------
|
||||
#include "checkother.h"
|
||||
|
||||
#include "errormessage.h"
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
};
|
||||
|
|
133
tools/errmsg.cpp
133
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<Message> 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue