From f105bf75a6f19230b442e9d096dbb724ded79f32 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sun, 6 May 2012 01:17:15 -0700 Subject: [PATCH] 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. --- lib/check.h | 16 +--------------- lib/errorlogger.cpp | 39 +++++++++++++++++++++++++++++++++++---- lib/errorlogger.h | 12 +++++++++++- lib/tokenize.cpp | 16 +--------------- lib/tokenlist.cpp | 5 ++--- 5 files changed, 50 insertions(+), 38 deletions(-) diff --git a/lib/check.h b/lib/check.h index 5ef0e2744..cf7d7996f 100644 --- a/lib/check.h +++ b/lib/check.h @@ -148,21 +148,7 @@ private: /** report an error */ void reportError(const std::list &callstack, Severity::SeverityType severity, const std::string &id, const std::string& msg, bool inconclusive) { - std::list locationList; - for (std::list::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 diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 5c1b63c84..fc0564c3b 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -19,6 +19,7 @@ #include "errorlogger.h" #include "path.h" #include "cppcheck.h" +#include "tokenlist.h" #include #include @@ -44,6 +45,24 @@ ErrorLogger::ErrorMessage::ErrorMessage(const std::list &callStack setmsg(msg); } +ErrorLogger::ErrorMessage::ErrorMessage(const std::list& 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_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::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(); +} diff --git a/lib/errorlogger.h b/lib/errorlogger.h index 855c69f61..15365bcae 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -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 &callStack, Severity::SeverityType severity, const std::string &msg, const std::string &id, bool inconclusive); + ErrorMessage(const std::list& callstack, const TokenList* list, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive); ErrorMessage(); /** diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4c244c526..2915a48ea 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8847,21 +8847,7 @@ void Tokenizer::reportError(const Token* tok, const Severity::SeverityType sever void Tokenizer::reportError(const std::list& callstack, Severity::SeverityType severity, const std::string& id, const std::string& msg, bool inconclusive) const { - std::list locationList; - for (std::list::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 diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index f3ed64563..30e7cff9a 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -23,6 +23,7 @@ #include "path.h" #include "preprocessor.h" #include "settings.h" +#include "errorlogger.h" #include #include @@ -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(); }