From 9edecd4a3ff2c3077930ebf696a2c7d49e468cd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 3 Aug 2010 16:36:21 +0200 Subject: [PATCH] Added ErrorLogger::reportProgress and removed ErrorLogger::ReportProgress. This will make it easier for GUI and other clients to display progress information. --- lib/checkbufferoverrun.cpp | 4 +++- lib/cppcheck.cpp | 27 +++++++++++++++++++++++++++ lib/cppcheck.h | 12 +++++++++--- lib/errorlogger.cpp | 28 ---------------------------- lib/errorlogger.h | 24 +++++++++++------------- lib/preprocessor.cpp | 9 ++++----- lib/token.cpp | 8 +++++++- lib/token.h | 18 ++++++++++++++++++ lib/tokenize.cpp | 17 ++++++++--------- 9 files changed, 87 insertions(+), 60 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 87a20fa81..ef4de78fe 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -980,7 +980,9 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() if (tok->previous() && (!tok->previous()->isName() && !Token::Match(tok->previous(), "[;{}]"))) continue; - _errorLogger->ReportProgress("CheckBufferOverrun::checkGlobalAndLocalVariable"); + _errorLogger->reportProgress(_tokenizer->getFiles()->front(), + "Check (BufferOverrun::checkGlobalAndLocalVariable)", + tok->progressValue()); ArrayInfo arrayInfo; if (arrayInfo.declare(tok, *_tokenizer)) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 4e5ef3985..025d2eaca 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -171,6 +171,7 @@ CppCheck::CppCheck(ErrorLogger &errorLogger) : _errorLogger(errorLogger) { exitcode = 0; + time1 = std::time(0); } CppCheck::~CppCheck() @@ -930,6 +931,32 @@ void CppCheck::reportStatus(unsigned int /*index*/, unsigned int /*max*/) } +void CppCheck::reportProgress(const std::string &filename, const char stage[], const unsigned char value) +{ + (void)filename; + + // Report progress messages every 10 seconds + const std::time_t time2 = std::time(NULL); + if (time2 >= (time1 + 10)) + { + time1 = time2; + + // current time in the format "Www Mmm dd hh:mm:ss yyyy" + const std::string str(ctime(&time2)); + + // format a progress message + std::ostringstream ostr; + ostr << "progress: " + << stage + << " " << int(value) << "%"; + if (_settings._verbose) + ostr << " time=" << str.substr(11, 8); + + // Report progress message + reportOut(ostr.str()); + } +} + void CppCheck::getErrorMessages() { // call all "getErrorMessages" in all registered Check classes diff --git a/lib/cppcheck.h b/lib/cppcheck.h index 9ec2ef2c7..1c9d90bf8 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -19,14 +19,16 @@ #ifndef CPPCHECK_H #define CPPCHECK_H +#include "settings.h" +#include "errorlogger.h" +#include "checkunusedfunctions.h" + #include #include #include #include #include -#include "settings.h" -#include "errorlogger.h" -#include "checkunusedfunctions.h" +#include /// @addtogroup Core /// @{ @@ -155,6 +157,8 @@ private: */ virtual void reportOut(const std::string &outmsg); + void reportProgress(const std::string &filename, const char stage[], const unsigned char value); + unsigned int exitcode; std::list _errorList; std::ostringstream _errout; @@ -169,6 +173,8 @@ private: /** @brief Current preprocessor configuration */ std::string cfg; + + std::time_t time1; }; /// @} diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 97536aab9..3d946c5cb 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -230,31 +230,3 @@ void ErrorLogger::ErrorMessage::FileLocation::setfile(const std::string &file) _file = file; _file = Path::fromNativeSeparators(_file); } - - -void ErrorLogger::ReportProgress(const char func[]) -{ - if (!func) - { - time1 = std::time(NULL); - return; - } - - const std::time_t time2 = std::time(NULL); - if (time2 >= (time1 + 10)) - { - time1 = time2; - - // current time in the format "Www Mmm dd hh:mm:ss yyyy" - const std::string str(ctime(&time2)); - - // format a progress message - std::ostringstream ostr; - ostr << "progress:" - << " time=" << str.substr(11, 8) - << " function=" << func; - - // Report progress message - reportOut(ostr.str()); - } -} diff --git a/lib/errorlogger.h b/lib/errorlogger.h index 59cc18bd2..e96e985a7 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -20,7 +20,6 @@ #ifndef errorloggerH #define errorloggerH -#include #include #include @@ -141,10 +140,7 @@ public: std::string _id; }; - ErrorLogger() - { - time1 = 0; - } + ErrorLogger() { } virtual ~ErrorLogger() { } /** @@ -173,17 +169,19 @@ public: virtual void reportStatus(unsigned int index, unsigned int max) = 0; /** - * Report progress. - * - * @param func function name (NULL = start command) + * Report progress to client + * @param filename main file that is checked + * @param stage for example preprocess / tokenize / simplify / check + * @param value progress value (0-100) */ - void ReportProgress(const char func[]); + virtual void reportProgress(const std::string &filename, const char stage[], const unsigned char value) + { + (void)filename; + (void)stage; + (void)value; + } static std::string callStackToString(const std::list &callStack); - -private: - /** time variable for the 'ReportProgress' */ - std::time_t time1; }; diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index c938a8e8c..a9dd10588 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -677,9 +677,6 @@ std::string Preprocessor::getdef(std::string line, bool def) std::list Preprocessor::getcfgs(const std::string &filedata, const std::string &filename) { - if (_errorLogger) - _errorLogger->ReportProgress(0); - std::list ret; ret.push_back(""); @@ -702,7 +699,8 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const ++linenr; if (_errorLogger) - _errorLogger->ReportProgress("Preprocessor::getcfgs"); + _errorLogger->reportProgress(filename, "Preprocessing (get configurations 1)", 0); + if (line.compare(0, 6, "#file ") == 0) { @@ -856,10 +854,11 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const } // Remove defined constants from ifdef configurations.. + unsigned int count = 0; for (std::list::iterator it = ret.begin(); it != ret.end(); ++it) { if (_errorLogger) - _errorLogger->ReportProgress("Preprocessor::getcfgs"); + _errorLogger->reportProgress(filename, "Preprocessing (get configurations 2)", (100 * count++) / ret.size()); std::string cfg(*it); for (std::set::const_iterator it2 = defines.begin(); it2 != defines.end(); ++it2) diff --git a/lib/token.cpp b/lib/token.cpp index c76f243ab..c8602c11e 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -43,7 +43,8 @@ Token::Token(Token **t) : _previous(0), _link(0), _fileIndex(0), - _linenr(0) + _linenr(0), + _progressValue(0) { } @@ -589,6 +590,10 @@ void Token::move(Token *srcStart, Token *srcEnd, Token *newLocation) // Fix the tokens at newLocation newLocation->next()->previous(srcEnd); newLocation->next(srcStart); + + // Update _progressValue + for (Token *tok = srcStart; tok && tok != srcEnd; tok = tok->next()) + tok->_progressValue = newLocation->_progressValue; } //--------------------------------------------------------------------------- @@ -619,6 +624,7 @@ void Token::insertToken(const std::string &tokenStr) newToken->str(tokenStr); newToken->_linenr = _linenr; newToken->_fileIndex = _fileIndex; + newToken->_progressValue = _progressValue; if (this->next()) { newToken->next(this->next()); diff --git a/lib/token.h b/lib/token.h index c49f08ae7..c8bf3cf08 100644 --- a/lib/token.h +++ b/lib/token.h @@ -338,6 +338,23 @@ public: */ static void move(Token *srcStart, Token *srcEnd, Token *newLocation); + /** Get progressValue */ + unsigned char progressValue() const + { + return _progressValue; + } + + /** Calculate progress values for all tokens */ + void assignProgressValues() + { + unsigned int total_count = 0; + for (Token *tok = this; tok; tok = tok->next()) + ++total_count; + unsigned int count = 0; + for (Token *tok = this; tok; tok = tok->next()) + tok->_progressValue = count++ * 100 / total_count; + } + private: void next(Token *nextToken) { @@ -384,6 +401,7 @@ private: Token *_link; unsigned int _fileIndex; unsigned int _linenr; + unsigned char _progressValue; }; /// @} diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index f4c172bab..aa92a062d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -400,6 +400,7 @@ void Tokenizer::createTokens(std::istream &code) CurrentToken += ch; } addtoken(CurrentToken.c_str(), lineno, FileIndex, true); + _tokens->assignProgressValues(); } void Tokenizer::duplicateTypedefError(const Token *tok1, const Token *tok2, const std::string &type) @@ -650,8 +651,8 @@ void Tokenizer::simplifyTypedef() bool hasClass = false; for (Token *tok = _tokens; tok; tok = tok->next()) { - if (_errorLogger) - _errorLogger->ReportProgress("Tokenizer::simplifyTypedef"); + if (_errorLogger && !_files.empty()) + _errorLogger->reportProgress(_files[0], "Tokenize (typedef)", tok->progressValue()); if (Token::Match(tok, "class|struct|namespace %any%")) { @@ -1651,9 +1652,6 @@ void Tokenizer::simplifyTypedef() bool Tokenizer::tokenize(std::istream &code, const char FileName[], const std::string &configuration) { - if (_errorLogger) - _errorLogger->ReportProgress(0); - _configuration = configuration; // The "_files" vector remembers what files have been tokenized.. @@ -1829,10 +1827,9 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[], const std::s //updateClassList(); setVarId(); - if (!validate()) - return false; + _tokens->assignProgressValues(); - return true; + return validate(); } //--------------------------------------------------------------------------- @@ -2684,7 +2681,7 @@ void Tokenizer::setVarId() continue; if (_errorLogger) - _errorLogger->ReportProgress("Tokenizer::setVarId"); + _errorLogger->reportProgress(_files[0], "Tokenize (set variable id)", tok->progressValue()); // If pattern is "( %type% *|& %var% )" then check if it's a // variable declaration or a multiplication / mask @@ -3693,6 +3690,8 @@ bool Tokenizer::simplifyTokenList() _tokens->printOut(0, _files); } + _tokens->assignProgressValues(); + return validate(); } //---------------------------------------------------------------------------