From e1d5d9988d399e3863dd63ba699a82fdb1b1af9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 11 May 2023 14:04:22 +0200 Subject: [PATCH] Fixed #11715 (Add --showtime=file-total option to show checking time of each file) (#5049) --- cli/cmdlineparser.cpp | 4 +++- lib/cppcheck.cpp | 2 ++ lib/timer.cpp | 22 +++++++++++++++------- lib/timer.h | 4 +++- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index b59fd1f39..e74eaa6ab 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -830,6 +830,8 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) const std::string showtimeMode = argv[i] + 11; if (showtimeMode == "file") mSettings.showtime = SHOWTIME_MODES::SHOWTIME_FILE; + else if (showtimeMode == "file-total") + mSettings.showtime = SHOWTIME_MODES::SHOWTIME_FILE_TOTAL; else if (showtimeMode == "summary") mSettings.showtime = SHOWTIME_MODES::SHOWTIME_SUMMARY; else if (showtimeMode == "top5") @@ -837,7 +839,7 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[]) else if (showtimeMode.empty()) mSettings.showtime = SHOWTIME_MODES::SHOWTIME_NONE; else { - printError("unrecognized showtime mode: \"" + showtimeMode + "\". Supported modes: file, summary, top5."); + printError("unrecognized showtime mode: \"" + showtimeMode + "\". Supported modes: file, file-total, summary, top5."); return false; } } diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 11c4a5465..eccb673ff 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -638,6 +638,8 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string if (Settings::terminated()) return mExitCode; + const Timer fileTotalTimer(mSettings.showtime == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL, filename); + if (!mSettings.quiet) { std::string fixedpath = Path::simplifyPath(filename); fixedpath = Path::toNativeSeparators(fixedpath); diff --git a/lib/timer.cpp b/lib/timer.cpp index 06571b257..1b1603a6f 100644 --- a/lib/timer.cpp +++ b/lib/timer.cpp @@ -40,7 +40,7 @@ namespace { void TimerResults::showResults(SHOWTIME_MODES mode) const { - if (mode == SHOWTIME_MODES::SHOWTIME_NONE) + if (mode == SHOWTIME_MODES::SHOWTIME_NONE || mode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL) return; std::cout << std::endl; @@ -90,13 +90,18 @@ void TimerResults::addResults(const std::string& str, std::clock_t clocks) Timer::Timer(std::string str, SHOWTIME_MODES showtimeMode, TimerResultsIntf* timerResults) : mStr(std::move(str)) , mTimerResults(timerResults) - , mStart(0) + , mStart(std::clock()) , mShowTimeMode(showtimeMode) - , mStopped(false) -{ - if (showtimeMode != SHOWTIME_MODES::SHOWTIME_NONE) - mStart = std::clock(); -} + , mStopped(showtimeMode == SHOWTIME_MODES::SHOWTIME_NONE || showtimeMode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL) +{} + +Timer::Timer(bool fileTotal, std::string filename) + : mStr(std::move(filename)) + , mTimerResults(nullptr) + , mStart(std::clock()) + , mShowTimeMode(SHOWTIME_MODES::SHOWTIME_FILE_TOTAL) + , mStopped(!fileTotal) +{} Timer::~Timer() { @@ -112,6 +117,9 @@ void Timer::stop() if (mShowTimeMode == SHOWTIME_MODES::SHOWTIME_FILE) { const double sec = (double)diff / CLOCKS_PER_SEC; std::cout << mStr << ": " << sec << "s" << std::endl; + } else if (mShowTimeMode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL) { + const double sec = (double)diff / CLOCKS_PER_SEC; + std::cout << "Check time: " << mStr << ": " << sec << "s" << std::endl; } else { if (mTimerResults) mTimerResults->addResults(mStr, diff); diff --git a/lib/timer.h b/lib/timer.h index d24e1244d..7fa8ffad8 100644 --- a/lib/timer.h +++ b/lib/timer.h @@ -28,8 +28,9 @@ #include enum class SHOWTIME_MODES { - SHOWTIME_NONE = 0, + SHOWTIME_NONE, SHOWTIME_FILE, + SHOWTIME_FILE_TOTAL, SHOWTIME_SUMMARY, SHOWTIME_TOP5 }; @@ -70,6 +71,7 @@ private: class CPPCHECKLIB Timer { public: Timer(std::string str, SHOWTIME_MODES showtimeMode, TimerResultsIntf* timerResults = nullptr); + Timer(bool fileTotal, std::string filename); ~Timer(); Timer(const Timer&) = delete;