Merge remote branch 'remotes/kimmo/severity-refactor'
This commit is contained in:
commit
d811970531
|
@ -108,6 +108,6 @@ void CppCheckExecutor::reportErr(const ErrorLogger::ErrorMessage &msg)
|
|||
}
|
||||
else
|
||||
{
|
||||
reportErr(msg.toText(_settings._outputFormat));
|
||||
reportErr(msg.toString(_settings._outputFormat));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -93,7 +93,7 @@ int ThreadExecutor::handleRead(unsigned int &result)
|
|||
msg.deserialize(buf);
|
||||
|
||||
// Alert only about unique errors
|
||||
std::string errmsg = msg.toText();
|
||||
std::string errmsg = msg.toString();
|
||||
if (std::find(_errorList.begin(), _errorList.end(), errmsg) == _errorList.end())
|
||||
{
|
||||
_errorList.push_back(errmsg);
|
||||
|
|
|
@ -49,7 +49,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
|
|||
QMutexLocker locker(&mutex);
|
||||
|
||||
// GUI doesn't know how to properly handle debug messages so lets ignore them.
|
||||
if (msg._severity == "debug")
|
||||
if (msg._severity == Severity::debug)
|
||||
return;
|
||||
|
||||
QList<unsigned int> lines;
|
||||
|
@ -69,7 +69,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
|
|||
item.id = QString(msg._id.c_str());
|
||||
item.lines = lines;
|
||||
item.msg = QString(msg._msg.c_str());
|
||||
item.severity = QString(msg._severity.c_str());
|
||||
item.severity = QString::fromStdString(Severity::toString(msg._severity));
|
||||
|
||||
emit Error(item);
|
||||
}
|
||||
|
|
|
@ -118,7 +118,7 @@ protected:
|
|||
ErrorLogger * const _errorLogger;
|
||||
|
||||
/** report an error */
|
||||
void reportError(const Token *tok, const Severity::e severity, const std::string &id, const std::string &msg)
|
||||
void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg)
|
||||
{
|
||||
std::list<const Token *> callstack;
|
||||
if (tok)
|
||||
|
@ -127,7 +127,7 @@ protected:
|
|||
}
|
||||
|
||||
/** report an error */
|
||||
void reportError(const std::list<const Token *> &callstack, const Severity::e severity, const std::string &id, std::string msg)
|
||||
void reportError(const std::list<const Token *> &callstack, const 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)
|
||||
|
@ -150,7 +150,7 @@ protected:
|
|||
locationList.push_back(loc);
|
||||
}
|
||||
|
||||
const ErrorLogger::ErrorMessage errmsg(locationList, Severity::stringify(severity), msg, id);
|
||||
const ErrorLogger::ErrorMessage errmsg(locationList, Severity::toString(severity), msg, id);
|
||||
if (_errorLogger)
|
||||
_errorLogger->reportErr(errmsg);
|
||||
else
|
||||
|
|
|
@ -300,7 +300,7 @@ void CheckMemoryLeak::memoryLeak(const Token *tok, const std::string &varname, A
|
|||
//---------------------------------------------------------------------------
|
||||
|
||||
|
||||
void CheckMemoryLeak::reportErr(const Token *tok, Severity::e severity, const std::string &id, const std::string &msg) const
|
||||
void CheckMemoryLeak::reportErr(const Token *tok, Severity::SeverityType severity, const std::string &id, const std::string &msg) const
|
||||
{
|
||||
std::list<const Token *> callstack;
|
||||
|
||||
|
@ -310,7 +310,7 @@ void CheckMemoryLeak::reportErr(const Token *tok, Severity::e severity, const st
|
|||
reportErr(callstack, severity, id, msg);
|
||||
}
|
||||
|
||||
void CheckMemoryLeak::reportErr(const std::list<const Token *> &callstack, Severity::e severity, const std::string &id, const std::string &msg) const
|
||||
void CheckMemoryLeak::reportErr(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg) const
|
||||
{
|
||||
std::list<ErrorLogger::ErrorMessage::FileLocation> locations;
|
||||
|
||||
|
@ -325,7 +325,7 @@ void CheckMemoryLeak::reportErr(const std::list<const Token *> &callstack, Sever
|
|||
locations.push_back(loc);
|
||||
}
|
||||
|
||||
const ErrorLogger::ErrorMessage errmsg(locations, Severity::stringify(severity), msg, id);
|
||||
const ErrorLogger::ErrorMessage errmsg(locations, Severity::toString(severity), msg, id);
|
||||
|
||||
if (errorLogger)
|
||||
errorLogger->reportErr(errmsg);
|
||||
|
|
|
@ -71,7 +71,7 @@ private:
|
|||
* @param id type of message
|
||||
* @param msg text
|
||||
*/
|
||||
void reportErr(const Token *location, Severity::e severity, const std::string &id, const std::string &msg) const;
|
||||
void reportErr(const Token *location, Severity::SeverityType severity, const std::string &id, const std::string &msg) const;
|
||||
|
||||
/**
|
||||
* Report error. Similar with the function Check::reportError
|
||||
|
@ -80,7 +80,7 @@ private:
|
|||
* @param id type of message
|
||||
* @param msg text
|
||||
*/
|
||||
void reportErr(const std::list<const Token *> &callstack, Severity::e severity, const std::string &id, const std::string &msg) const;
|
||||
void reportErr(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg) const;
|
||||
|
||||
public:
|
||||
CheckMemoryLeak(const Tokenizer *t, ErrorLogger *e)
|
||||
|
|
|
@ -192,7 +192,7 @@ void CheckUnusedFunctions::unusedFunctionError(ErrorLogger * const errorLogger,
|
|||
locationList.push_back(fileLoc);
|
||||
}
|
||||
|
||||
const ErrorLogger::ErrorMessage errmsg(locationList, Severity::stringify(Severity::style), "The function '" + funcname + "' is never used", "unusedFunction");
|
||||
const ErrorLogger::ErrorMessage errmsg(locationList, Severity::toString(Severity::style), "The function '" + funcname + "' is never used", "unusedFunction");
|
||||
if (errorLogger)
|
||||
errorLogger->reportErr(errmsg);
|
||||
else
|
||||
|
|
|
@ -836,7 +836,7 @@ Settings CppCheck::settings() const
|
|||
|
||||
void CppCheck::reportErr(const ErrorLogger::ErrorMessage &msg)
|
||||
{
|
||||
std::string errmsg = msg.toText();
|
||||
std::string errmsg = msg.toString();
|
||||
|
||||
// Alert only about unique errors
|
||||
if (std::find(_errorList.begin(), _errorList.end(), errmsg) != _errorList.end())
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include <sstream>
|
||||
|
||||
ErrorLogger::ErrorMessage::ErrorMessage()
|
||||
:_severity(Severity::none)
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -30,7 +31,7 @@ ErrorLogger::ErrorMessage::ErrorMessage()
|
|||
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack, const std::string &severity, const std::string &msg, const std::string &id)
|
||||
{
|
||||
_callStack = callStack;
|
||||
_severity = severity;
|
||||
_severity = Severity::fromString(severity);
|
||||
_msg = msg;
|
||||
_id = id;
|
||||
}
|
||||
|
@ -39,7 +40,7 @@ std::string ErrorLogger::ErrorMessage::serialize() const
|
|||
{
|
||||
std::ostringstream oss;
|
||||
oss << _id.length() << " " << _id;
|
||||
oss << _severity.length() << " " << _severity;
|
||||
oss << Severity::toString(_severity).length() << " " << Severity::toString(_severity);
|
||||
oss << _msg.length() << " " << _msg;
|
||||
oss << _callStack.size() << " ";
|
||||
|
||||
|
@ -77,7 +78,7 @@ bool ErrorLogger::ErrorMessage::deserialize(const std::string &data)
|
|||
}
|
||||
|
||||
_id = results[0];
|
||||
_severity = results[1];
|
||||
_severity = Severity::fromString(results[1]);
|
||||
_msg = results[2];
|
||||
|
||||
unsigned int stackSize = 0;
|
||||
|
@ -153,7 +154,7 @@ std::string ErrorLogger::ErrorMessage::toXML() const
|
|||
xml << " line=\"" << _callStack.back().line << "\"";
|
||||
}
|
||||
xml << " id=\"" << _id << "\"";
|
||||
xml << " severity=\"" << _severity << "\"";
|
||||
xml << " severity=\"" << Severity::toString(_severity) << "\"";
|
||||
xml << " msg=\"" << stringToXml(_msg) << "\"";
|
||||
xml << "/>";
|
||||
return xml.str();
|
||||
|
@ -169,15 +170,15 @@ void ErrorLogger::ErrorMessage::findAndReplace(std::string &source, const std::s
|
|||
}
|
||||
}
|
||||
|
||||
std::string ErrorLogger::ErrorMessage::toText(const std::string &outputFormat) const
|
||||
std::string ErrorLogger::ErrorMessage::toString(const std::string &outputFormat) const
|
||||
{
|
||||
if (outputFormat.length() == 0)
|
||||
{
|
||||
std::ostringstream text;
|
||||
if (!_callStack.empty())
|
||||
text << callStackToString(_callStack) << ": ";
|
||||
if (!_severity.empty())
|
||||
text << "(" << _severity << ") ";
|
||||
if (_severity != Severity::none)
|
||||
text << "(" << Severity::toString(_severity) << ") ";
|
||||
text << _msg;
|
||||
return text.str();
|
||||
}
|
||||
|
@ -185,7 +186,7 @@ std::string ErrorLogger::ErrorMessage::toText(const std::string &outputFormat) c
|
|||
{
|
||||
std::string result = outputFormat;
|
||||
findAndReplace(result, "{id}", _id);
|
||||
findAndReplace(result, "{severity}", _severity);
|
||||
findAndReplace(result, "{severity}", Severity::toString(_severity));
|
||||
findAndReplace(result, "{message}", _msg);
|
||||
|
||||
if (!_callStack.empty())
|
||||
|
|
|
@ -29,6 +29,42 @@ class Tokenizer;
|
|||
/// @addtogroup Core
|
||||
/// @{
|
||||
|
||||
/** @brief enum class for severity. Used when reporting errors. */
|
||||
class Severity
|
||||
{
|
||||
public:
|
||||
enum SeverityType { none, error, style, debug };
|
||||
static std::string toString(SeverityType severity)
|
||||
{
|
||||
switch (severity)
|
||||
{
|
||||
case none:
|
||||
return "";
|
||||
case error:
|
||||
return "error";
|
||||
case style:
|
||||
return "style";
|
||||
case debug:
|
||||
return "debug";
|
||||
};
|
||||
return "???";
|
||||
}
|
||||
static SeverityType fromString(const std::string &severity)
|
||||
{
|
||||
if (severity.empty())
|
||||
return none;
|
||||
if (severity == "none")
|
||||
return none;
|
||||
if (severity == "error")
|
||||
return error;
|
||||
if (severity == "style")
|
||||
return style;
|
||||
if (severity == "debug")
|
||||
return debug;
|
||||
return none;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief This is an interface, which the class responsible of error logging
|
||||
* should implement.
|
||||
|
@ -71,7 +107,7 @@ public:
|
|||
* @param outputFormat Empty string to use default output format
|
||||
* or template to be used. E.g. "{file}:{line},{severity},{id},{message}"
|
||||
*/
|
||||
std::string toText(const std::string &outputFormat = "") const;
|
||||
std::string toString(const std::string &outputFormat = "") const;
|
||||
|
||||
/**
|
||||
* Replace all occurances of searchFor with replaceWith in the
|
||||
|
@ -84,7 +120,7 @@ public:
|
|||
std::string serialize() const;
|
||||
bool deserialize(const std::string &data);
|
||||
std::list<FileLocation> _callStack;
|
||||
std::string _severity;
|
||||
Severity::SeverityType _severity;
|
||||
std::string _msg;
|
||||
std::string _id;
|
||||
};
|
||||
|
@ -307,24 +343,6 @@ private:
|
|||
void _writemsg(const Tokenizer *tokenizer, const std::list<const Token *> &callstack, const char severity[], const std::string &msg, const std::string &id);
|
||||
};
|
||||
|
||||
/** @brief enum class for severity. Used when reporting errors. */
|
||||
class Severity
|
||||
{
|
||||
public:
|
||||
enum e { error, style };
|
||||
static std::string stringify(e severity)
|
||||
{
|
||||
switch (severity)
|
||||
{
|
||||
case error:
|
||||
return "error";
|
||||
case style:
|
||||
return "style";
|
||||
};
|
||||
return "???";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/// @}
|
||||
|
||||
|
|
|
@ -757,7 +757,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
|
|||
loc.file = filename;
|
||||
loc.line = linenr;
|
||||
errmsg._callStack.push_back(loc);
|
||||
errmsg._severity = "error";
|
||||
errmsg._severity = Severity::fromString("error");
|
||||
errmsg._msg = "mismatching number of '(' and ')' in this line: " + def;
|
||||
errmsg._id = "preprocessor" + lineStream.str();
|
||||
_errorLogger->reportErr(errmsg);
|
||||
|
@ -902,7 +902,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
|
|||
loc.file = filename;
|
||||
loc.line = 1;
|
||||
errmsg._callStack.push_back(loc);
|
||||
errmsg._severity = "error";
|
||||
errmsg._severity = Severity::fromString("error");
|
||||
errmsg._msg = "Error parsing this: " + s;
|
||||
errmsg._id = "preprocessor" + lineStream.str();
|
||||
_errorLogger->reportErr(errmsg);
|
||||
|
|
|
@ -385,7 +385,7 @@ private:
|
|||
loc.file = "ab/cd/../ef.h";
|
||||
errorMessage._callStack.push_back(loc);
|
||||
ASSERT_EQUALS("<error file=\"ab/ef.h\" line=\"0\" id=\"\" severity=\"\" msg=\"\"/>", errorMessage.toXML());
|
||||
ASSERT_EQUALS("[ab/ef.h:0]: ", errorMessage.toText());
|
||||
ASSERT_EQUALS("[ab/ef.h:0]: ", errorMessage.toString());
|
||||
}
|
||||
|
||||
void templateFormat()
|
||||
|
@ -396,11 +396,11 @@ private:
|
|||
loc.line = 10;
|
||||
errorMessage._callStack.push_back(loc);
|
||||
errorMessage._id = "testId";
|
||||
errorMessage._severity = "testSeverity";
|
||||
errorMessage._severity = Severity::fromString("error");
|
||||
errorMessage._msg = "long testMessage";
|
||||
ASSERT_EQUALS("<error file=\"some/{file}file.cpp\" line=\"10\" id=\"testId\" severity=\"testSeverity\" msg=\"long testMessage\"/>", errorMessage.toXML());
|
||||
ASSERT_EQUALS("[some/{file}file.cpp:10]: (testSeverity) long testMessage", errorMessage.toText());
|
||||
ASSERT_EQUALS("testId-some/{file}file.cpp,testSeverity.10?{long testMessage}", errorMessage.toText("{id}-{file},{severity}.{line}?{{message}}"));
|
||||
ASSERT_EQUALS("<error file=\"some/{file}file.cpp\" line=\"10\" id=\"testId\" severity=\"error\" msg=\"long testMessage\"/>", errorMessage.toXML());
|
||||
ASSERT_EQUALS("[some/{file}file.cpp:10]: (error) long testMessage", errorMessage.toString());
|
||||
ASSERT_EQUALS("testId-some/{file}file.cpp,error.10?{long testMessage}", errorMessage.toString("{id}-{file},{severity}.{line}?{{message}}"));
|
||||
}
|
||||
|
||||
void getErrorMessages()
|
||||
|
|
|
@ -223,5 +223,5 @@ void TestFixture::reportOut(const std::string & outmsg)
|
|||
|
||||
void TestFixture::reportErr(const ErrorLogger::ErrorMessage &msg)
|
||||
{
|
||||
errout << msg.toText() << std::endl;
|
||||
errout << msg.toString() << std::endl;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue