diff --git a/src/checkheaders.cpp b/src/checkheaders.cpp index fdee94113..d76adbb9b 100644 --- a/src/checkheaders.cpp +++ b/src/checkheaders.cpp @@ -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 empty; - empty.push_back(FileLocation()); - _errorLogger->reportErr(empty, "id", "severity", ostr.str()); - +// std::list 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 empty; - empty.push_back(FileLocation()); - _errorLogger->reportErr(empty, "id", "severity", ostr.str()); // TODO +// std::list empty; + // empty.push_back(FileLocation()); + // _errorLogger->reportErr(empty, "severity", ostr.str(), "id"); // TODO } } } diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index 7ed274988..b955321d2 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -424,11 +424,9 @@ Settings CppCheck::settings() const //--------------------------------------------------------------------------- -void CppCheck::reportErr(const std::list &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 &callStack, const std::st errmsg2 += "\n Defines=\'" + cfg + "\'\n"; } - _errorLogger->reportErr(callStack, id, severity, msg); + _errorLogger->reportErr(msg); _errout << errmsg2 << std::endl; } diff --git a/src/cppcheck.h b/src/cppcheck.h index 67df942bd..a09df3aae 100644 --- a/src/cppcheck.h +++ b/src/cppcheck.h @@ -110,7 +110,7 @@ private: * "[filepath:line number] Message", e.g. * "[main.cpp:4] Uninitialized member variable" */ - virtual void reportErr(const std::list &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. diff --git a/src/cppcheckexecutor.cpp b/src/cppcheckexecutor.cpp index 090625306..a1f60e320 100644 --- a/src/cppcheckexecutor.cpp +++ b/src/cppcheckexecutor.cpp @@ -70,25 +70,14 @@ void CppCheckExecutor::reportOut(const std::string &outmsg) std::cout << outmsg << std::endl; } -void CppCheckExecutor::reportErr(const std::list &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 << ""; - reportErr(xml.str()); + reportErr(msg.toXML()); } else { - std::ostringstream text; - - text << ErrorLogger::callStackToString(callStack) << ": (" << severity << ") " << msg; - reportErr(text.str()); + reportErr(msg.toText()); } } diff --git a/src/cppcheckexecutor.h b/src/cppcheckexecutor.h index 190fac528..c3e9218db 100644 --- a/src/cppcheckexecutor.h +++ b/src/cppcheckexecutor.h @@ -65,7 +65,7 @@ public: virtual void reportOut(const std::string &outmsg); /** xml output of errors */ - virtual void reportErr(const std::list &callStack, const std::string &id, const std::string &severity, const std::string &msg); + virtual void reportErr(const ErrorLogger::ErrorMessage &msg); private: diff --git a/src/errorlogger.cpp b/src/errorlogger.cpp index 74c3ac16b..178a7f274 100644 --- a/src/errorlogger.cpp +++ b/src/errorlogger.cpp @@ -23,6 +23,34 @@ #include +ErrorLogger::ErrorMessage::ErrorMessage(const std::list &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 << ""; + 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 callstack; @@ -32,19 +60,16 @@ void ErrorLogger::_writemsg(const Tokenizer *tokenizer, const Token *tok, const void ErrorLogger::_writemsg(const Tokenizer *tokenizer, const std::list &callstack, const char severity[], const std::string msg, const std::string &id) { - std::list locationList; + std::list locationList; for (std::list::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 empty; - empty.push_back(FileLocation()); - reportErr(empty, "id", "severity", msg); + std::list empty; + empty.push_back(ErrorLogger::ErrorMessage::FileLocation()); + reportErr(ErrorLogger::ErrorMessage(empty, "severity", msg, "id")); } -std::string ErrorLogger::callStackToString(const std::list &callStack) +std::string ErrorLogger::callStackToString(const std::list &callStack) { std::ostringstream ostr; - for (std::list::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok) + for (std::list::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok) ostr << (tok == callStack.begin() ? "" : " -> ") << "[" << (*tok).file << ":" << (*tok).line << "]"; return ostr.str(); } diff --git a/src/errorlogger.h b/src/errorlogger.h index ef0f3ce91..eacd35030 100644 --- a/src/errorlogger.h +++ b/src/errorlogger.h @@ -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 &callStack, const std::string &severity, const std::string &msg, const std::string &id); + std::string toXML() const; + std::string toText() const; + private: + std::list _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 &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 &Location) { @@ -429,7 +445,7 @@ public: } - static std::string callStackToString(const std::list &callStack); + static std::string callStackToString(const std::list &callStack); private: void _writemsg(const Tokenizer *tokenizer, const Token *tok, const char severity[], const std::string msg, const std::string &id); diff --git a/test/testsuite.cpp b/test/testsuite.cpp index 8772459e4..80477778f 100644 --- a/test/testsuite.cpp +++ b/test/testsuite.cpp @@ -172,9 +172,7 @@ void TestFixture::reportOut(const std::string & /*outmsg*/) // These can probably be ignored } -void TestFixture::reportErr(const std::list &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; } diff --git a/test/testsuite.h b/test/testsuite.h index 90683f78e..967fcf6f6 100644 --- a/test/testsuite.h +++ b/test/testsuite.h @@ -43,7 +43,7 @@ protected: public: virtual void reportOut(const std::string &outmsg); - virtual void reportErr(const std::list &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); diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp index 998bef84c..f94e1bc88 100644 --- a/tools/errmsg.cpp +++ b/tools/errmsg.cpp @@ -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 &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 _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 &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::const_iterator it = err.begin(); it != err.end(); ++it) it->generateCode(fout); fout << "\n"; - fout << " static std::string callStackToString(const std::list &callStack);\n"; + fout << " static std::string callStackToString(const std::list &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";