Refactoring: reportErr takes now only one parameter, ErrorLogger::ErrorMessage, which contains all required information and also some help

functions for formatting it for output.
This commit is contained in:
Reijo Tomperi 2009-02-10 21:51:52 +00:00
parent 71b4e5a912
commit 9b9223480d
10 changed files with 109 additions and 68 deletions

View File

@ -64,10 +64,9 @@ void CheckHeaders::WarningHeaderWithImplementation()
// TODO, this check is currently not used, but if it is some day
// it should give correct id and severity by calling proper function
// from errorLogger. It should not call reportErr directly.
std::list<FileLocation> empty;
empty.push_back(FileLocation());
_errorLogger->reportErr(empty, "id", "severity", ostr.str());
// std::list<ErrorLogger::ErrorMessage::FileLocation> empty;
// empty.push_back(FileLocation());
// _errorLogger->reportErr(empty, "severity", ostr.str(), "id");
// Goto next file..
unsigned int fileindex = tok->fileIndex();
while (tok->next() && tok->fileIndex() == fileindex)
@ -258,9 +257,9 @@ void CheckHeaders::WarningIncludeHeader()
// TODO, this check is currently not used, but if it is some day
// it should give correct id and severity by calling proper function
// from errorLogger. It should not call reportErr directly.
std::list<FileLocation> empty;
empty.push_back(FileLocation());
_errorLogger->reportErr(empty, "id", "severity", ostr.str()); // TODO
// std::list<FileLocation> empty;
// empty.push_back(FileLocation());
// _errorLogger->reportErr(empty, "severity", ostr.str(), "id"); // TODO
}
}
}

View File

@ -424,11 +424,9 @@ Settings CppCheck::settings() const
//---------------------------------------------------------------------------
void CppCheck::reportErr(const std::list<FileLocation> &callStack, const std::string &id, const std::string &severity, const std::string &msg)
void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg)
{
std::ostringstream text;
text << ErrorLogger::callStackToString(callStack) << ": (" << severity << ") " << msg;
std::string errmsg = text.str();
std::string errmsg = msg.toText();
// Alert only about unique errors
if (std::find(_errorList.begin(), _errorList.end(), errmsg) != _errorList.end())
@ -441,7 +439,7 @@ void CppCheck::reportErr(const std::list<FileLocation> &callStack, const std::st
errmsg2 += "\n Defines=\'" + cfg + "\'\n";
}
_errorLogger->reportErr(callStack, id, severity, msg);
_errorLogger->reportErr(msg);
_errout << errmsg2 << std::endl;
}

View File

@ -110,7 +110,7 @@ private:
* "[filepath:line number] Message", e.g.
* "[main.cpp:4] Uninitialized member variable"
*/
virtual void reportErr(const std::list<FileLocation> &callStack, const std::string &id, const std::string &severity, const std::string &msg);
virtual void reportErr(const ErrorLogger::ErrorMessage &msg);
/**
* Information about progress is directed here.

View File

@ -70,25 +70,14 @@ void CppCheckExecutor::reportOut(const std::string &outmsg)
std::cout << outmsg << std::endl;
}
void CppCheckExecutor::reportErr(const std::list<FileLocation> &callStack, const std::string &id, const std::string &severity, const std::string &msg)
void CppCheckExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
{
if (_useXML)
{
std::ostringstream xml;
xml << "<error";
xml << " file=\"" << callStack.back().file << "\"";
xml << " line=\"" << callStack.back().line << "\"";
xml << " id=\"" << id << "\"";
xml << " severity=\"" << severity << "\"";
xml << " msg=\"" << msg << "\"";
xml << "/>";
reportErr(xml.str());
reportErr(msg.toXML());
}
else
{
std::ostringstream text;
text << ErrorLogger::callStackToString(callStack) << ": (" << severity << ") " << msg;
reportErr(text.str());
reportErr(msg.toText());
}
}

View File

@ -65,7 +65,7 @@ public:
virtual void reportOut(const std::string &outmsg);
/** xml output of errors */
virtual void reportErr(const std::list<FileLocation> &callStack, const std::string &id, const std::string &severity, const std::string &msg);
virtual void reportErr(const ErrorLogger::ErrorMessage &msg);
private:

View File

@ -23,6 +23,34 @@
#include <sstream>
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, const std::string &severity, const std::string &msg, const std::string &id)
{
_callStack = callStack;
_severity = severity;
_msg = msg;
_id = id;
}
std::string ErrorLogger::ErrorMessage::toXML() const
{
std::ostringstream xml;
xml << "<error";
xml << " file=\"" << _callStack.back().file << "\"";
xml << " line=\"" << _callStack.back().line << "\"";
xml << " id=\"" << _id << "\"";
xml << " severity=\"" << _severity << "\"";
xml << " msg=\"" << _msg << "\"";
xml << "/>";
return xml.str();
}
std::string ErrorLogger::ErrorMessage::toText() const
{
std::ostringstream text;
text << callStackToString(_callStack) << ": (" << _severity << ") " << _msg;
return text.str();
}
void ErrorLogger::_writemsg(const Tokenizer *tokenizer, const Token *tok, const char severity[], const std::string msg, const std::string &id)
{
std::list<const Token *> callstack;
@ -32,19 +60,16 @@ void ErrorLogger::_writemsg(const Tokenizer *tokenizer, const Token *tok, const
void ErrorLogger::_writemsg(const Tokenizer *tokenizer, const std::list<const Token *> &callstack, const char severity[], const std::string msg, const std::string &id)
{
std::list<FileLocation> locationList;
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
for (std::list<const Token *>::const_iterator tok = callstack.begin(); tok != callstack.end(); ++tok)
{
FileLocation loc;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.file = tokenizer->file(*tok);
loc.line = (*tok)->linenr();
locationList.push_back(loc);
}
reportErr(locationList,
id,
severity,
msg);
reportErr(ErrorLogger::ErrorMessage(locationList, severity, msg, id));
}
@ -56,15 +81,15 @@ void ErrorLogger::_writemsg(const std::string msg, const std::string &id)
xml << " msg=\"" << msg << "\"";
xml << ">";
std::list<FileLocation> empty;
empty.push_back(FileLocation());
reportErr(empty, "id", "severity", msg);
std::list<ErrorLogger::ErrorMessage::FileLocation> empty;
empty.push_back(ErrorLogger::ErrorMessage::FileLocation());
reportErr(ErrorLogger::ErrorMessage(empty, "severity", msg, "id"));
}
std::string ErrorLogger::callStackToString(const std::list<FileLocation> &callStack)
std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack)
{
std::ostringstream ostr;
for (std::list<FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok)
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok)
ostr << (tok == callStack.begin() ? "" : " -> ") << "[" << (*tok).file << ":" << (*tok).line << "]";
return ostr.str();
}

View File

@ -27,16 +27,6 @@
class Token;
class Tokenizer;
/**
* File name and line number.
*/
class FileLocation
{
public:
std::string file;
unsigned int line;
};
/**
* This is an interface, which the class responsible of error logging
* should implement.
@ -45,6 +35,32 @@ class ErrorLogger
{
public:
/**
* reportErr()
*/
class ErrorMessage
{
public:
/**
* File name and line number.
*/
class FileLocation
{
public:
std::string file;
unsigned int line;
};
ErrorMessage(const std::list<FileLocation> &callStack, const std::string &severity, const std::string &msg, const std::string &id);
std::string toXML() const;
std::string toText() const;
private:
std::list<FileLocation> _callStack;
std::string _severity;
std::string _msg;
std::string _id;
};
ErrorLogger() { }
virtual ~ErrorLogger() { }
@ -66,7 +82,7 @@ public:
* @param severity severity of error (always, all, style, all+style, never)
* @param msg error message in plain text
*/
virtual void reportErr(const std::list<FileLocation> &callStack, const std::string &id, const std::string &severity, const std::string &msg) = 0;
virtual void reportErr(const ErrorLogger::ErrorMessage &msg) = 0;
void arrayIndexOutOfBounds(const Tokenizer *tokenizer, const std::list<const Token *> &Location)
{
@ -429,7 +445,7 @@ public:
}
static std::string callStackToString(const std::list<FileLocation> &callStack);
static std::string callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack);
private:
void _writemsg(const Tokenizer *tokenizer, const Token *tok, const char severity[], const std::string msg, const std::string &id);

View File

@ -172,9 +172,7 @@ void TestFixture::reportOut(const std::string & /*outmsg*/)
// These can probably be ignored
}
void TestFixture::reportErr(const std::list<FileLocation> &callStack, const std::string & /*id*/, const std::string &severity, const std::string &msg)
void TestFixture::reportErr(const ErrorLogger::ErrorMessage &msg)
{
std::ostringstream text;
text << ErrorLogger::callStackToString(callStack) << ": (" << severity << ") " << msg;
errout << text.str() << std::endl;
errout << msg.toText() << std::endl;
}

View File

@ -43,7 +43,7 @@ protected:
public:
virtual void reportOut(const std::string &outmsg);
virtual void reportErr(const std::list<FileLocation> &callStack, const std::string &id, const std::string &severity, const std::string &msg);
virtual void reportErr(const ErrorLogger::ErrorMessage &msg);
void run(const std::string &str);

View File

@ -148,16 +148,6 @@ int main()
fout << "class Tokenizer;\n";
fout << "\n";
fout << "/**\n";
fout << " * File name and line number.\n";
fout << " */\n";
fout << "class FileLocation\n";
fout << "{\n";
fout << "public:\n";
fout << " std::string file;\n";
fout << " unsigned int line;\n";
fout << "};\n";
fout << "\n";
fout << "/**\n";
fout << " * This is an interface, which the class responsible of error logging\n";
fout << " * should implement.\n";
fout << " */\n";
@ -165,6 +155,32 @@ int main()
fout << "{\n";
fout << "public:\n";
fout << "\n";
fout << " /**\n";
fout << " * reportErr()\n";
fout << " */\n";
fout << " class ErrorMessage\n";
fout << " {\n";
fout << " public:\n";
fout << " /**\n";
fout << " * File name and line number.\n";
fout << " */\n";
fout << " class FileLocation\n";
fout << " {\n";
fout << " public:\n";
fout << " std::string file;\n";
fout << " unsigned int line;\n";
fout << " };\n";
fout << "\n";
fout << " ErrorMessage(const std::list<FileLocation> &callStack, const std::string &severity, const std::string &msg, const std::string &id);\n";
fout << " std::string toXML() const;\n";
fout << " std::string toText() const;\n";
fout << " private:\n";
fout << " std::list<FileLocation> _callStack;\n";
fout << " std::string _severity;\n";
fout << " std::string _msg;\n";
fout << " std::string _id;\n";
fout << " };\n";
fout << "\n";
fout << " ErrorLogger() { }\n";
fout << " virtual ~ErrorLogger() { }\n";
fout << "\n";
@ -186,14 +202,14 @@ int main()
fout << " * @param severity severity of error (always, all, style, all+style, never)\n";
fout << " * @param msg error message in plain text\n";
fout << " */\n";
fout << " virtual void reportErr(const std::list<FileLocation> &callStack, const std::string &id, const std::string &severity, const std::string &msg) = 0;\n";
fout << " virtual void reportErr(const ErrorLogger::ErrorMessage &msg) = 0;\n";
fout << "\n";
for (std::list<Message>::const_iterator it = err.begin(); it != err.end(); ++it)
it->generateCode(fout);
fout << "\n";
fout << " static std::string callStackToString(const std::list<FileLocation> &callStack);\n";
fout << " static std::string callStackToString(const std::list<ErrorLogger::ErrorMessage::FileLocation> &callStack);\n";
fout << "\n";
fout << "private:\n";
fout << " void _writemsg(const Tokenizer *tokenizer, const Token *tok, const char severity[], const std::string msg, const std::string &id);\n";