cppcheck/tools/errmsg.cpp

107 lines
2.6 KiB
C++
Raw Normal View History

#include <iostream>
#include <list>
#include <string>
class Message
{
private:
std::string _funcname;
std::string _msg;
std::string _par1;
unsigned int _settings;
public:
Message(std::string funcname, unsigned int settings, std::string msg, std::string par1)
: _funcname(funcname), _settings(settings), _msg(msg), _par1(par1)
{ }
static const unsigned int ALL = 1;
static const unsigned int STYLE = 2;
2009-01-06 18:20:19 +01:00
std::string msg(bool code) const
{
2009-01-06 18:20:19 +01:00
const char *str = code ? "\"" : "";
std::string ret( str + _msg + str );
2009-01-06 18:09:27 +01:00
if (! _par1.empty())
{
std::string::size_type pos = 0;
while ((pos = ret.find("%1", pos)) != std::string::npos)
{
ret.erase(pos, 2);
2009-01-06 18:20:19 +01:00
if ( code )
ret.insert(pos, "\" + " + _par1 + " + \"");
else
ret.insert(pos, _par1);
2009-01-06 18:09:27 +01:00
}
}
return ret;
}
2009-01-06 18:09:27 +01:00
void generateCode(std::ostream &ostr) const
{
// Error message..
2009-01-06 18:09:27 +01:00
ostr << " static std::string " << _funcname << "(";
if (! _par1.empty())
2009-01-06 18:09:27 +01:00
ostr << "const std::string &" << _par1;
ostr << ") const\n";
2009-01-06 18:22:35 +01:00
ostr << " { return " << msg(true) << "; }" << std::endl;
// Settings..
2009-01-06 18:09:27 +01:00
ostr << std::endl;
ostr << " static bool " << _funcname << "(const Settings &s) const" << std::endl;
ostr << " { return ";
if (_settings == 0)
2009-01-06 18:09:27 +01:00
ostr << "true";
else
{
if (_settings & ALL)
2009-01-06 18:09:27 +01:00
ostr << "s._showAll";
if (_settings & (ALL | STYLE))
2009-01-06 18:09:27 +01:00
ostr << " & ";
if (_settings & STYLE)
2009-01-06 18:09:27 +01:00
ostr << "s._checkCodingStyle";
}
2009-01-06 18:09:27 +01:00
ostr << "; }" << std::endl;
}
2009-01-06 18:09:27 +01:00
void generateDoc(std::ostream &ostr, unsigned int i) const
{
if ( _settings == i )
{
2009-01-06 18:22:35 +01:00
ostr << " " << msg(false) << std::endl;
2009-01-06 18:09:27 +01:00
}
}
};
int main()
{
// Error messages..
std::list<Message> err;
err.push_back(Message("memleak", 0, "Memory leak: %1", "varname"));
// Generate code..
2009-01-06 18:09:27 +01:00
std::cout << "Generate code.." << std::endl;
for (std::list<Message>::const_iterator it = err.begin(); it != err.end(); ++it)
2009-01-06 18:09:27 +01:00
it->generateCode(std::cout);
std::cout << std::endl;
// Generate documentation..
std::cout << "Generate doc.." << std::endl;
for ( unsigned int i = 0; i < 4; ++i )
{
const char *suite[4] = { "standard", "all", "style", "all + style" };
2009-01-06 18:22:35 +01:00
std::cout << " =" << suite[i] << "=" << std::endl;
2009-01-06 18:09:27 +01:00
for (std::list<Message>::const_iterator it = err.begin(); it != err.end(); ++it)
it->generateDoc(std::cout, i);
}
std::cout << std::endl;
return 0;
}