Fixed #2167 (Drop linefeeds from error messages)

This commit is contained in:
Daniel Marjamäki 2010-11-11 19:54:43 +01:00
parent 0328d26fcb
commit ba7a3b376e
11 changed files with 87 additions and 50 deletions

View File

@ -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));
}
}

View File

@ -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);

View File

@ -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)

View File

@ -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<const Token *> &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<ErrorLogger::ErrorMessage::FileLocation> locationList;
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it)
{

View File

@ -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.");
}

View File

@ -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())

View File

@ -32,16 +32,31 @@ ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &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<ErrorLogger::ErrorMessage::FileLocation>::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 << "<error";
@ -157,7 +172,7 @@ std::string ErrorLogger::ErrorMessage::toXML() const
}
xml << " id=\"" << _id << "\"";
xml << " severity=\"" << (_severity == Severity::error ? "error" : "style") << "\"";
xml << " msg=\"" << stringToXml(_msg) << "\"";
xml << " msg=\"" << stringToXml(verbose ? _verboseMessage : _shortMessage) << "\"";
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())
{

View File

@ -120,18 +120,50 @@ public:
ErrorMessage(const std::list<FileLocation> &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<FileLocation> _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<FileLocation> _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() { }

View File

@ -845,7 +845,7 @@ std::list<std::string> 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<std::string> 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;
}

View File

@ -388,8 +388,8 @@ private:
{
// Test the errorlogger..
ErrorLogger::ErrorMessage errorMessage;
errorMessage._msg = "ab<cd>ef";
ASSERT_EQUALS("<error id=\"\" severity=\"style\" msg=\"ab&lt;cd&gt;ef\"/>", errorMessage.toXML());
errorMessage.setmsg("ab<cd>ef");
ASSERT_EQUALS("<error id=\"\" severity=\"style\" msg=\"ab&lt;cd&gt;ef\"/>", 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("<error file=\"" + fname + "\" line=\"0\" id=\"\" severity=\"style\" msg=\"\"/>", errorMessage.toXML());
ASSERT_EQUALS("[" + fname + ":0]: ", errorMessage.toString());
ASSERT_EQUALS("<error file=\"" + fname + "\" line=\"0\" id=\"\" severity=\"style\" msg=\"\"/>", 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("<error file=\"" + fname + "\" line=\"10\" id=\"testId\" severity=\"error\" msg=\"long testMessage\"/>", 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("<error file=\"" + fname + "\" line=\"10\" id=\"testId\" severity=\"error\" msg=\"long testMessage\"/>", 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()

View File

@ -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;
}