Refactorizations in ErrorLogger:

- Implemented constructor for ErrorLogger::ErrorMessage that takes a callstack of tokens -> replaced duplicate code in Check and Tokenizer
- Implemented strigify() for ErrorLogger::ErrorMessage::FileLocation to replace two identical implementations of it.
This commit is contained in:
PKEuS 2012-05-06 01:17:15 -07:00
parent 37cdb515af
commit f105bf75a6
5 changed files with 50 additions and 38 deletions

View File

@ -148,21 +148,7 @@ private:
/** report an error */
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string& msg, bool inconclusive) {
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it) {
// --errorlist can provide null values here
if (!(*it))
continue;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = (*it)->linenr();
loc.setfile(_tokenizer->list.file(*it));
locationList.push_back(loc);
}
ErrorLogger::ErrorMessage errmsg(locationList, severity, msg, id, inconclusive);
if (_tokenizer && !_tokenizer->list.getFiles().empty())
errmsg.file0 = _tokenizer->list.getFiles()[0];
ErrorLogger::ErrorMessage errmsg(callstack, _tokenizer?&_tokenizer->list:0, severity, id, msg, inconclusive);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else

View File

@ -19,6 +19,7 @@
#include "errorlogger.h"
#include "path.h"
#include "cppcheck.h"
#include "tokenlist.h"
#include <cassert>
#include <sstream>
@ -44,6 +45,24 @@ ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack
setmsg(msg);
}
ErrorLogger::ErrorMessage::ErrorMessage(const std::list<const Token*>& callstack, const TokenList* list, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive)
: _severity(severity), _id(id), _inconclusive(inconclusive)
{
// Format callstack
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it) {
// --errorlist can provide null values here
if (!(*it))
continue;
_callStack.push_back(ErrorLogger::ErrorMessage::FileLocation(*it, list));
}
if (list && !list->getFiles().empty())
file0 = list->getFiles()[0];
setmsg(msg);
}
void ErrorLogger::ErrorMessage::setmsg(const std::string &msg)
{
// If a message ends to a '\n' and contains only a one '\n'
@ -306,15 +325,17 @@ std::string ErrorLogger::callStackToString(const std::list<ErrorLogger::ErrorMes
{
std::ostringstream ostr;
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok) {
ostr << (tok == callStack.begin() ? "" : " -> ") << '[' << (*tok).getfile();
if ((*tok).line != 0)
ostr << ':' << (*tok).line;
ostr << ']';
ostr << (tok == callStack.begin() ? "" : " -> ") << tok->stringify();
}
return ostr.str();
}
ErrorLogger::ErrorMessage::FileLocation::FileLocation(const Token* tok, const TokenList* list)
: line(tok->linenr()), _file(list->file(tok))
{
}
std::string ErrorLogger::ErrorMessage::FileLocation::getfile(bool convert) const
{
if (convert)
@ -328,3 +349,13 @@ void ErrorLogger::ErrorMessage::FileLocation::setfile(const std::string &file)
_file = Path::fromNativeSeparators(_file);
_file = Path::simplifyPath(_file.c_str());
}
std::string ErrorLogger::ErrorMessage::FileLocation::stringify() const
{
std::ostringstream oss;
oss << '[' << Path::toNativeSeparators(_file);
if (line != 0)
oss << ':' << line;
oss << ']';
return oss.str();
}

View File

@ -26,7 +26,7 @@
#include "suppressions.h"
class Token;
class Tokenizer;
class TokenList;
/// @addtogroup Core
/// @{
@ -160,6 +160,8 @@ public:
: line(aline), _file(file) {
}
FileLocation(const Token* tok, const TokenList* list);
/**
* Return the filename.
* @param convert If true convert path to native separators.
@ -172,6 +174,13 @@ public:
* @param file Filename to set.
*/
void setfile(const std::string &file);
/**
* Set the filename.
* @param file Filename to set.
*/
std::string stringify() const;
unsigned int line;
private:
std::string _file;
@ -179,6 +188,7 @@ public:
};
ErrorMessage(const std::list<FileLocation> &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive);
ErrorMessage(const std::list<const Token*>& callstack, const TokenList* list, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive);
ErrorMessage();
/**

View File

@ -8847,21 +8847,7 @@ void Tokenizer::reportError(const Token* tok, const Severity::SeverityType sever
void Tokenizer::reportError(const std::list<const Token*>& callstack, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive) const
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
for (std::list<const Token *>::const_iterator it = callstack.begin(); it != callstack.end(); ++it) {
// --errorlist can provide null values here
if (!(*it))
continue;
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = (*it)->linenr();
loc.setfile(list.file(*it));
locationList.push_back(loc);
}
ErrorLogger::ErrorMessage errmsg(locationList, severity, msg, id, inconclusive);
if (!list.getFiles().empty())
errmsg.file0 = list.getFiles()[0];
ErrorLogger::ErrorMessage errmsg(callstack, &list, severity, id, msg, inconclusive);
if (_errorLogger)
_errorLogger->reportErr(errmsg);
else

View File

@ -23,6 +23,7 @@
#include "path.h"
#include "preprocessor.h"
#include "settings.h"
#include "errorlogger.h"
#include <cstring>
#include <sstream>
@ -345,7 +346,5 @@ const std::string& TokenList::file(const Token *tok) const
std::string TokenList::fileLine(const Token *tok) const
{
std::ostringstream ostr;
ostr << "[" << _files.at(tok->fileIndex()) << ":" << tok->linenr() << "]";
return ostr.str();
return ErrorLogger::ErrorMessage::FileLocation(tok, this).stringify();
}