From ba7a3b376e1f96062b3f256eacf3270ef4903534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 11 Nov 2010 19:54:43 +0100 Subject: [PATCH] Fixed #2167 (Drop linefeeds from error messages) --- cli/cppcheckexecutor.cpp | 4 ++-- cli/threadexecutor.cpp | 2 +- gui/threadresult.cpp | 2 +- lib/check.h | 10 +------- lib/checkother.cpp | 9 ++++---- lib/cppcheck.cpp | 2 +- lib/errorlogger.cpp | 31 ++++++++++++++++++------- lib/errorlogger.h | 49 ++++++++++++++++++++++++++++++++-------- lib/preprocessor.cpp | 10 ++++---- test/testcppcheck.cpp | 16 ++++++------- test/testsuite.cpp | 2 +- 11 files changed, 87 insertions(+), 50 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index fdd6a4cc3..54828fbe0 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -184,10 +184,10 @@ void CppCheckExecutor::reportErr(const ErrorLogger::ErrorMessage &msg) { if (_settings._xml) { - reportErr(msg.toXML()); + reportErr(msg.toXML(_settings._verbose)); } else { - reportErr(msg.toString(_settings._outputFormat)); + reportErr(msg.toString(_settings._verbose, _settings._outputFormat)); } } diff --git a/cli/threadexecutor.cpp b/cli/threadexecutor.cpp index 9e572510e..38a7e9062 100644 --- a/cli/threadexecutor.cpp +++ b/cli/threadexecutor.cpp @@ -93,7 +93,7 @@ int ThreadExecutor::handleRead(unsigned int &result) msg.deserialize(buf); // Alert only about unique errors - std::string errmsg = msg.toString(); + std::string errmsg = msg.toString(_settings._verbose); if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end()) { _errorList.push_back(errmsg); diff --git a/gui/threadresult.cpp b/gui/threadresult.cpp index 259b95193..890356133 100644 --- a/gui/threadresult.cpp +++ b/gui/threadresult.cpp @@ -69,7 +69,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg) item.files = files; item.id = QString(msg._id.c_str()); item.lines = lines; - item.msg = QString(msg._msg.c_str()); + item.msg = QString::fromStdString(msg.verboseMessage()); item.severity = QString::fromStdString(Severity::toString(msg._severity)); if (msg._severity != Severity::debug) diff --git a/lib/check.h b/lib/check.h index 0d2b47e2d..a17c9ca3c 100644 --- a/lib/check.h +++ b/lib/check.h @@ -110,7 +110,7 @@ public: */ static void reportError(const ErrorLogger::ErrorMessage &errmsg) { - std::cout << errmsg.toXML() << std::endl; + std::cout << errmsg.toXML(true) << std::endl; } protected: @@ -130,14 +130,6 @@ protected: /** report an error */ void reportError(const std::list &callstack, Severity::SeverityType severity, const std::string &id, std::string msg) { - // If the verbose flag hasn't been given, don't show verbose information - if (!_settings || !_settings->_verbose) - { - std::string::size_type pos = msg.find("\n"); - if (pos != std::string::npos) - msg.erase(pos); - } - std::list locationList; for (std::list::const_iterator it = callstack.begin(); it != callstack.end(); ++it) { diff --git a/lib/checkother.cpp b/lib/checkother.cpp index c027bea06..f240928a4 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -398,7 +398,8 @@ void CheckOther::invalidScanfError(const Token *tok) { reportError(tok, Severity::warning, "invalidscanf", "scanf without field width limits can crash with huge input data\n" - "To fix this error message add a field width specifier:\n" + "scanf without field width limits can crash with huge input data. To fix this error " + "message add a field width specifier:\n" " %s => %20s\n" " %i => %3i\n" "\n" @@ -2429,8 +2430,9 @@ void CheckOther::variableScopeError(const Token *tok, const std::string &varname Severity::style, "variableScope", "The scope of the variable " + varname + " can be reduced\n" - "Warning: It can be unsafe to fix this message. Be careful. Especially when there are inner loops.\n" - "Here is an example where cppcheck will write that the scope for 'i' can be reduced:\n" + "The scope of the variable " + varname + " can be reduced. Warning: It can be unsafe " + "to fix this message. Be careful. Especially when there are inner loops. Here is an " + "example where cppcheck will write that the scope for 'i' can be reduced:\n" "void f(int x)\n" "{\n" " int i = 0;\n" @@ -2442,7 +2444,6 @@ void CheckOther::variableScopeError(const Token *tok, const std::string &varname " }\n" " }\n" "}\n" - "\n" "When you see this message it is always safe to reduce the variable scope 1 level."); } diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 2784ec6c4..488288816 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -308,7 +308,7 @@ Settings CppCheck::settings() const void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg) { - std::string errmsg = msg.toString(); + std::string errmsg = msg.toString(_settings._verbose); // Alert only about unique errors if (std::find(_errorList.begin(), _errorList.end(), errmsg) != _errorList.end()) diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 99251da1d..0847a1d41 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -32,16 +32,31 @@ ErrorLogger::ErrorMessage::ErrorMessage(const std::list &callStack { _callStack = callStack; _severity = severity; - _msg = msg; + setmsg(msg); _id = id; } +void ErrorLogger::ErrorMessage::setmsg(const std::string &msg) +{ + const std::string::size_type pos = msg.find("\n"); + if (pos == std::string::npos) + { + _shortMessage = msg; + _verboseMessage = msg; + } + else + { + _shortMessage = msg.substr(0, pos - 1); + _verboseMessage = msg.substr(pos + 1); + } +} + std::string ErrorLogger::ErrorMessage::serialize() const { std::ostringstream oss; oss << _id.length() << " " << _id; oss << Severity::toString(_severity).length() << " " << Severity::toString(_severity); - oss << _msg.length() << " " << _msg; + oss << _shortMessage.length() << " " << _shortMessage; oss << _callStack.size() << " "; for (std::list::const_iterator tok = _callStack.begin(); tok != _callStack.end(); ++tok) @@ -79,7 +94,7 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data) _id = results[0]; _severity = Severity::fromString(results[1]); - _msg = results[2]; + _shortMessage = results[2]; unsigned int stackSize = 0; if (!(iss >> stackSize)) @@ -146,7 +161,7 @@ static std::string stringToXml(std::string s) return s; } -std::string ErrorLogger::ErrorMessage::toXML() const +std::string ErrorLogger::ErrorMessage::toXML(bool verbose) const { std::ostringstream xml; xml << ""; return xml.str(); } @@ -172,7 +187,7 @@ void ErrorLogger::ErrorMessage::findAndReplace(std::string &source, const std::s } } -std::string ErrorLogger::ErrorMessage::toString(const std::string &outputFormat) const +std::string ErrorLogger::ErrorMessage::toString(bool verbose, const std::string &outputFormat) const { if (outputFormat.length() == 0) { @@ -181,7 +196,7 @@ std::string ErrorLogger::ErrorMessage::toString(const std::string &outputFormat) text << callStackToString(_callStack) << ": "; if (_severity != Severity::none) text << "(" << Severity::toString(_severity) << ") "; - text << _msg; + text << (verbose ? _verboseMessage : _shortMessage); return text.str(); } else @@ -189,7 +204,7 @@ std::string ErrorLogger::ErrorMessage::toString(const std::string &outputFormat) std::string result = outputFormat; findAndReplace(result, "{id}", _id); findAndReplace(result, "{severity}", Severity::toString(_severity)); - findAndReplace(result, "{message}", _msg); + findAndReplace(result, "{message}", verbose ? _verboseMessage : _shortMessage); if (!_callStack.empty()) { diff --git a/lib/errorlogger.h b/lib/errorlogger.h index 64b7e1384..d67292328 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -120,18 +120,50 @@ public: ErrorMessage(const std::list &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id); ErrorMessage(); - std::string toXML() const; + + /** + * Format the error message in XML format + * @param verbose use verbose message + */ + std::string toXML(bool verbose) const; static std::string getXMLHeader(); static std::string getXMLFooter(); /** * Format the error message into a string. + * @param verbose use verbose message * @param outputFormat Empty string to use default output format * or template to be used. E.g. "{file}:{line},{severity},{id},{message}" */ - std::string toString(const std::string &outputFormat = "") const; + std::string toString(bool verbose, const std::string &outputFormat = "") const; + std::string serialize() const; + bool deserialize(const std::string &data); + + std::list _callStack; + Severity::SeverityType _severity; + std::string _id; + + /** source file (not header) */ + std::string file0; + + /** set short and verbose messages */ + void setmsg(const std::string &msg); + + /** Short message (single line short message) */ + const std::string &shortMessage() const + { + return _shortMessage; + } + + /** Verbose message (may be the same as the short message) */ + const std::string &verboseMessage() const + { + return _verboseMessage; + } + + private: /** * Replace all occurances of searchFor with replaceWith in the * given source. @@ -140,15 +172,12 @@ public: * @param replaceWith What will replace the found item */ static void findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith); - std::string serialize() const; - bool deserialize(const std::string &data); - std::list _callStack; - Severity::SeverityType _severity; - std::string _msg; - std::string _id; - /** source file (not header) */ - std::string file0; + /** Short message */ + std::string _shortMessage; + + /** Verbose message */ + std::string _verboseMessage; }; ErrorLogger() { } diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 87bda0ce6..95a16074c 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -845,7 +845,7 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const loc.line = linenr; errmsg._callStack.push_back(loc); errmsg._severity = Severity::fromString("error"); - errmsg._msg = "mismatching number of '(' and ')' in this line: " + def; + errmsg.setmsg("mismatching number of '(' and ')' in this line: " + def); errmsg._id = "preprocessor" + lineStream.str(); _errorLogger->reportErr(errmsg); ret.clear(); @@ -993,8 +993,8 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const loc.setfile(filename); loc.line = 1; errmsg._callStack.push_back(loc); - errmsg._severity = Severity::fromString("error"); - errmsg._msg = "Error parsing this: " + s; + errmsg._severity = Severity::error; + errmsg.setmsg("Error parsing this: " + s); errmsg._id = "preprocessor" + lineStream.str(); _errorLogger->reportErr(errmsg); } @@ -2402,11 +2402,11 @@ void Preprocessor::getErrorMessages(std::ostream &ostr) Severity::style, "Include file: \"\" not found.", "missingInclude"); - ostr << errmsg.toXML() << std::endl; + ostr << errmsg.toXML(false) << std::endl; const ErrorLogger::ErrorMessage errmsg2(locationList, Severity::error, "#error ...", "preprocessorErrorDirective"); - ostr << errmsg2.toXML() << std::endl; + ostr << errmsg2.toXML(false) << std::endl; } diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index 7b6c4e4a0..d2c84458e 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -388,8 +388,8 @@ private: { // Test the errorlogger.. ErrorLogger::ErrorMessage errorMessage; - errorMessage._msg = "abef"; - ASSERT_EQUALS("", errorMessage.toXML()); + errorMessage.setmsg("abef"); + ASSERT_EQUALS("", errorMessage.toXML(false)); } @@ -400,8 +400,8 @@ private: loc.setfile("ab/cd/../ef.h"); errorMessage._callStack.push_back(loc); const std::string fname(Path::toNativeSeparators("ab/ef.h")); - ASSERT_EQUALS("", errorMessage.toXML()); - ASSERT_EQUALS("[" + fname + ":0]: ", errorMessage.toString()); + ASSERT_EQUALS("", errorMessage.toXML(false)); + ASSERT_EQUALS("[" + fname + ":0]: ", errorMessage.toString(false)); } void templateFormat() @@ -413,11 +413,11 @@ private: errorMessage._callStack.push_back(loc); errorMessage._id = "testId"; errorMessage._severity = Severity::fromString("error"); - errorMessage._msg = "long testMessage"; + errorMessage.setmsg("long testMessage"); const std::string fname(Path::toNativeSeparators("some/{file}file.cpp")); - ASSERT_EQUALS("", errorMessage.toXML()); - ASSERT_EQUALS("[" + fname + ":10]: (error) long testMessage", errorMessage.toString()); - ASSERT_EQUALS("testId-" + fname + ",error.10?{long testMessage}", errorMessage.toString("{id}-{file},{severity}.{line}?{{message}}")); + ASSERT_EQUALS("", errorMessage.toXML(false)); + ASSERT_EQUALS("[" + fname + ":10]: (error) long testMessage", errorMessage.toString(false)); + ASSERT_EQUALS("testId-" + fname + ",error.10?{long testMessage}", errorMessage.toString(false, "{id}-{file},{severity}.{line}?{{message}}")); } void getErrorMessages() diff --git a/test/testsuite.cpp b/test/testsuite.cpp index 2c1177b39..8ef5d490d 100644 --- a/test/testsuite.cpp +++ b/test/testsuite.cpp @@ -271,7 +271,7 @@ void TestFixture::reportOut(const std::string & outmsg) void TestFixture::reportErr(const ErrorLogger::ErrorMessage &msg) { - const std::string errormessage(msg.toString()); + const std::string errormessage(msg.toString(false)); if (errout.str().find(errormessage) == std::string::npos) errout << errormessage << std::endl; }