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 */ /** report an error */
void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string& msg, bool inconclusive) { 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; ErrorLogger::ErrorMessage errmsg(callstack, _tokenizer?&_tokenizer->list:0, severity, id, msg, inconclusive);
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];
if (_errorLogger) if (_errorLogger)
_errorLogger->reportErr(errmsg); _errorLogger->reportErr(errmsg);
else else

View File

@ -19,6 +19,7 @@
#include "errorlogger.h" #include "errorlogger.h"
#include "path.h" #include "path.h"
#include "cppcheck.h" #include "cppcheck.h"
#include "tokenlist.h"
#include <cassert> #include <cassert>
#include <sstream> #include <sstream>
@ -44,6 +45,24 @@ ErrorLogger::ErrorMessage::ErrorMessage(const std::list<FileLocation> &callStack
setmsg(msg); 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) void ErrorLogger::ErrorMessage::setmsg(const std::string &msg)
{ {
// If a message ends to a '\n' and contains only a one '\n' // 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; std::ostringstream ostr;
for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok) { for (std::list<ErrorLogger::ErrorMessage::FileLocation>::const_iterator tok = callStack.begin(); tok != callStack.end(); ++tok) {
ostr << (tok == callStack.begin() ? "" : " -> ") << '[' << (*tok).getfile(); ostr << (tok == callStack.begin() ? "" : " -> ") << tok->stringify();
if ((*tok).line != 0)
ostr << ':' << (*tok).line;
ostr << ']';
} }
return ostr.str(); 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 std::string ErrorLogger::ErrorMessage::FileLocation::getfile(bool convert) const
{ {
if (convert) if (convert)
@ -328,3 +349,13 @@ void ErrorLogger::ErrorMessage::FileLocation::setfile(const std::string &file)
_file = Path::fromNativeSeparators(_file); _file = Path::fromNativeSeparators(_file);
_file = Path::simplifyPath(_file.c_str()); _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" #include "suppressions.h"
class Token; class Token;
class Tokenizer; class TokenList;
/// @addtogroup Core /// @addtogroup Core
/// @{ /// @{
@ -160,6 +160,8 @@ public:
: line(aline), _file(file) { : line(aline), _file(file) {
} }
FileLocation(const Token* tok, const TokenList* list);
/** /**
* Return the filename. * Return the filename.
* @param convert If true convert path to native separators. * @param convert If true convert path to native separators.
@ -172,6 +174,13 @@ public:
* @param file Filename to set. * @param file Filename to set.
*/ */
void setfile(const std::string &file); void setfile(const std::string &file);
/**
* Set the filename.
* @param file Filename to set.
*/
std::string stringify() const;
unsigned int line; unsigned int line;
private: private:
std::string _file; 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<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(); 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 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; ErrorLogger::ErrorMessage errmsg(callstack, &list, severity, id, msg, inconclusive);
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];
if (_errorLogger) if (_errorLogger)
_errorLogger->reportErr(errmsg); _errorLogger->reportErr(errmsg);
else else

View File

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