From 41b0037897acef70991b40266a59e6e8b5c1377d Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Tue, 21 Oct 2025 07:43:45 +0000 Subject: [PATCH 01/10] Fix #14206 --showtime does not account for addons --- lib/cppcheck.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 2048d6f8098..e6ce7a4df11 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -1505,7 +1505,9 @@ void CppCheck::executeAddons(const std::string& dumpFile, const FileWithDetails& { if (!dumpFile.empty()) { std::vector f{dumpFile}; - executeAddons(f, file.spath()); + Timer::run("CppCheck::executeAddons", mSettings.showtime, &s_timerResults, [&]() { + executeAddons(f, file.spath()); + }); } } From 4770995b7c26a46ebd53cb021884b3b247f817ff Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Wed, 22 Oct 2025 23:27:57 +0000 Subject: [PATCH 02/10] Add global timer --- Makefile | 2 +- cli/cppcheckexecutor.cpp | 5 +++++ lib/timer.cpp | 37 ++++++++++++++++++++++++++++++++++--- lib/timer.h | 17 +++++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 5f47959f60e..54019dba637 100644 --- a/Makefile +++ b/Makefile @@ -683,7 +683,7 @@ frontend/frontend.o: frontend/frontend.cpp frontend/frontend.h lib/addoninfo.h l cli/cmdlineparser.o: cli/cmdlineparser.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/filelister.h externals/tinyxml2/tinyxml2.h lib/addoninfo.h lib/check.h lib/checkers.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/pathmatch.h lib/platform.h lib/regex.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h lib/xml.h $(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cmdlineparser.cpp -cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h cli/sehwrapper.h cli/signalhandler.h cli/singleexecutor.h cli/threadexecutor.h externals/picojson/picojson.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/checkers.h lib/checkersreport.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h +cli/cppcheckexecutor.o: cli/cppcheckexecutor.cpp cli/cmdlinelogger.h cli/cmdlineparser.h cli/cppcheckexecutor.h cli/executor.h cli/processexecutor.h cli/sehwrapper.h cli/signalhandler.h cli/singleexecutor.h cli/threadexecutor.h externals/picojson/picojson.h lib/addoninfo.h lib/analyzerinfo.h lib/check.h lib/checkers.h lib/checkersreport.h lib/color.h lib/config.h lib/cppcheck.h lib/errorlogger.h lib/errortypes.h lib/filesettings.h lib/json.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/timer.h lib/utils.h $(CXX) ${INCLUDE_FOR_CLI} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ cli/cppcheckexecutor.cpp cli/executor.o: cli/executor.cpp cli/executor.h lib/addoninfo.h lib/checkers.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/utils.h diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 4b9809556b8..81606280339 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -37,6 +37,7 @@ #include "settings.h" #include "singleexecutor.h" #include "suppressions.h" +#include "timer.h" #include "utils.h" #if defined(HAS_THREADING_MODEL_THREAD) @@ -408,6 +409,7 @@ namespace { int CppCheckExecutor::check(int argc, const char* const argv[]) { + WholeProgramTimer timer; Settings settings; CmdLineLoggerStd logger; Suppressions supprs; @@ -419,6 +421,9 @@ int CppCheckExecutor::check(int argc, const char* const argv[]) return EXIT_SUCCESS; } + if (settings.showtime == SHOWTIME_MODES::SHOWTIME_NONE) + timer.cancell(); + settings.loadSummaries(); mFiles = parser.getFiles(); diff --git a/lib/timer.cpp b/lib/timer.cpp index 4b1f07d5de3..a445364f44b 100644 --- a/lib/timer.cpp +++ b/lib/timer.cpp @@ -83,9 +83,6 @@ void TimerResults::showResults(SHOWTIME_MODES mode) const } ++ordinal; } - - const double secOverall = overallData.seconds(); - std::cout << "Overall time: " << secOverall << "s" << std::endl; } void TimerResults::addResults(const std::string& str, std::clock_t clocks) @@ -142,3 +139,37 @@ void Timer::stop() mStopped = true; } + +WholeProgramTimer::~WholeProgramTimer() +{ + stop(); +} + +void WholeProgramTimer::stop() +{ + if (mCancelled) + return; + const auto end = std::chrono::high_resolution_clock::now(); + auto diff = std::chrono::duration_cast(end - mStart); + + // Extract hours + auto hours = std::chrono::duration_cast(diff); + diff -= hours; // Subtract the extracted hours + + // Extract minutes + auto minutes = std::chrono::duration_cast(diff); + diff -= minutes; // Subtract the extracted minutes + + // Extract seconds + auto seconds = static_cast(diff.count()) / std::chrono::microseconds::period::den; + + std::string ellapsedTime; + if (hours.count() > 0) + ellapsedTime += std::to_string(hours.count()) + "h "; + if (minutes.count() > 0) + ellapsedTime += std::to_string(minutes.count()) + "m "; + ellapsedTime += std::to_string(seconds) + "s "; + + std::lock_guard l(stdCoutLock); + std::cout << "Overall time: " << ellapsedTime << std::endl; +} diff --git a/lib/timer.h b/lib/timer.h index d2603d4eb55..7bec3ce6361 100644 --- a/lib/timer.h +++ b/lib/timer.h @@ -93,5 +93,22 @@ class CPPCHECKLIB Timer { const SHOWTIME_MODES mShowTimeMode = SHOWTIME_MODES::SHOWTIME_FILE_TOTAL; bool mStopped{}; }; + +class CPPCHECKLIB WholeProgramTimer { +public: + WholeProgramTimer() + : mStart(std::chrono::high_resolution_clock::now()) + {} + ~WholeProgramTimer(); + + void stop(); + + void cancell() { + mCancelled = true; + } +private: + std::chrono::system_clock::time_point mStart; + bool mCancelled{false}; +}; //--------------------------------------------------------------------------- #endif // timerH From 3ae57707294c3e6e728555531d837e1b5d1b08ff Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Fri, 24 Oct 2025 12:42:06 +0000 Subject: [PATCH 03/10] Rework + test fix --- cli/cppcheckexecutor.cpp | 8 ++++---- lib/timer.cpp | 12 ++---------- lib/timer.h | 22 ++++++---------------- test/testprocessexecutor.cpp | 2 +- 4 files changed, 13 insertions(+), 31 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 81606280339..ab8184c5f44 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -409,7 +409,7 @@ namespace { int CppCheckExecutor::check(int argc, const char* const argv[]) { - WholeProgramTimer timer; + auto startTime{Timer::now()}; Settings settings; CmdLineLoggerStd logger; Suppressions supprs; @@ -421,9 +421,6 @@ int CppCheckExecutor::check(int argc, const char* const argv[]) return EXIT_SUCCESS; } - if (settings.showtime == SHOWTIME_MODES::SHOWTIME_NONE) - timer.cancell(); - settings.loadSummaries(); mFiles = parser.getFiles(); @@ -431,6 +428,9 @@ int CppCheckExecutor::check(int argc, const char* const argv[]) const int ret = check_wrapper(settings, supprs); + if (settings.showtime != SHOWTIME_MODES::SHOWTIME_NONE) + Timer::calculateAndOutputTimeDiff(startTime, Timer::now()); + return ret; } diff --git a/lib/timer.cpp b/lib/timer.cpp index a445364f44b..efd59e97c8f 100644 --- a/lib/timer.cpp +++ b/lib/timer.cpp @@ -140,17 +140,9 @@ void Timer::stop() mStopped = true; } -WholeProgramTimer::~WholeProgramTimer() +void Timer::calculateAndOutputTimeDiff(const std::chrono::system_clock::time_point& start, const std::chrono::system_clock::time_point& end) { - stop(); -} - -void WholeProgramTimer::stop() -{ - if (mCancelled) - return; - const auto end = std::chrono::high_resolution_clock::now(); - auto diff = std::chrono::duration_cast(end - mStart); + auto diff = std::chrono::duration_cast(end - start); // Extract hours auto hours = std::chrono::duration_cast(diff); diff --git a/lib/timer.h b/lib/timer.h index 7bec3ce6361..c20437466c5 100644 --- a/lib/timer.h +++ b/lib/timer.h @@ -79,6 +79,12 @@ class CPPCHECKLIB Timer { Timer(const Timer&) = delete; Timer& operator=(const Timer&) = delete; + static std::chrono::system_clock::time_point now() { + return std::chrono::high_resolution_clock::now(); + } + + static void calculateAndOutputTimeDiff(const std::chrono::system_clock::time_point& start, const std::chrono::system_clock::time_point& end); + void stop(); static void run(std::string str, SHOWTIME_MODES showtimeMode, TimerResultsIntf* timerResults, const std::function& f) { @@ -94,21 +100,5 @@ class CPPCHECKLIB Timer { bool mStopped{}; }; -class CPPCHECKLIB WholeProgramTimer { -public: - WholeProgramTimer() - : mStart(std::chrono::high_resolution_clock::now()) - {} - ~WholeProgramTimer(); - - void stop(); - - void cancell() { - mCancelled = true; - } -private: - std::chrono::system_clock::time_point mStart; - bool mCancelled{false}; -}; //--------------------------------------------------------------------------- #endif // timerH diff --git a/test/testprocessexecutor.cpp b/test/testprocessexecutor.cpp index c0c19ae2b1f..031048fa84e 100644 --- a/test/testprocessexecutor.cpp +++ b/test/testprocessexecutor.cpp @@ -248,7 +248,7 @@ class TestProcessExecutorBase : public TestFixture { $.showtime = SHOWTIME_MODES::SHOWTIME_TOP5_SUMMARY)); const std::string output_s = GET_REDIRECT_OUTPUT; // once: top5 results + overall + empty line - TODO_ASSERT_EQUALS(5 + 1 + 1, 2, cppcheck::count_all_of(output_s, '\n')); + TODO_ASSERT_EQUALS(5 + 1 + 1, 1, cppcheck::count_all_of(output_s, '\n')); // should only report the top5 once ASSERT(output_s.find("1 result(s)") == std::string::npos); TODO_ASSERT(output_s.find("2 result(s)") != std::string::npos); From 2ba38c401d5ac80676d34b69fc037f123f98be7a Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Fri, 24 Oct 2025 14:58:40 +0000 Subject: [PATCH 04/10] Nits --- lib/timer.cpp | 2 +- lib/timer.h | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/timer.cpp b/lib/timer.cpp index efd59e97c8f..9e743a4a88a 100644 --- a/lib/timer.cpp +++ b/lib/timer.cpp @@ -140,7 +140,7 @@ void Timer::stop() mStopped = true; } -void Timer::calculateAndOutputTimeDiff(const std::chrono::system_clock::time_point& start, const std::chrono::system_clock::time_point& end) +void Timer::calculateAndOutputTimeDiff(const tp& start, const tp& end) { auto diff = std::chrono::duration_cast(end - start); diff --git a/lib/timer.h b/lib/timer.h index c20437466c5..c0b3dccda31 100644 --- a/lib/timer.h +++ b/lib/timer.h @@ -79,11 +79,13 @@ class CPPCHECKLIB Timer { Timer(const Timer&) = delete; Timer& operator=(const Timer&) = delete; - static std::chrono::system_clock::time_point now() { + using tp = std::chrono::time_point; + + static tp now() { return std::chrono::high_resolution_clock::now(); } - static void calculateAndOutputTimeDiff(const std::chrono::system_clock::time_point& start, const std::chrono::system_clock::time_point& end); + static void calculateAndOutputTimeDiff(const tp& start, const tp& end); void stop(); From b7e889a8638beb12d44f4e43fe67b0f3555eacf9 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Fri, 24 Oct 2025 15:23:46 +0000 Subject: [PATCH 05/10] Some test nits --- test/testsingleexecutor.cpp | 10 +++++----- test/testthreadexecutor.cpp | 11 +++++------ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/test/testsingleexecutor.cpp b/test/testsingleexecutor.cpp index 4f0665ff12e..1b995b8a489 100644 --- a/test/testsingleexecutor.cpp +++ b/test/testsingleexecutor.cpp @@ -242,8 +242,8 @@ class TestSingleExecutorBase : public TestFixture { dinit(CheckOptions, $.showtime = SHOWTIME_MODES::SHOWTIME_TOP5_FILE)); const std::string output_s = GET_REDIRECT_OUTPUT; - // for each file: top5 results + overall + empty line - ASSERT_EQUALS((5 + 1 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n')); + // for each file: top5 results + overall + ASSERT_EQUALS((5 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n')); } void showtime_top5_summary() { @@ -253,8 +253,8 @@ class TestSingleExecutorBase : public TestFixture { dinit(CheckOptions, $.showtime = SHOWTIME_MODES::SHOWTIME_TOP5_SUMMARY)); const std::string output_s = GET_REDIRECT_OUTPUT; - // once: top5 results + overall + empty line - ASSERT_EQUALS(5 + 1 + 1, cppcheck::count_all_of(output_s, '\n')); + // once: top5 results + overall + ASSERT_EQUALS(5 + 1, cppcheck::count_all_of(output_s, '\n')); // should only report the top5 once ASSERT(output_s.find("1 result(s)") == std::string::npos); ASSERT(output_s.find("2 result(s)") != std::string::npos); @@ -267,7 +267,7 @@ class TestSingleExecutorBase : public TestFixture { dinit(CheckOptions, $.showtime = SHOWTIME_MODES::SHOWTIME_FILE)); const std::string output_s = GET_REDIRECT_OUTPUT; - ASSERT_EQUALS(2, cppcheck::count_all_of(output_s, "Overall time:")); + ASSERT_EQUALS(1, cppcheck::count_all_of(output_s, "Overall time:")); } void showtime_summary() { diff --git a/test/testthreadexecutor.cpp b/test/testthreadexecutor.cpp index ad260727d01..252b8ffa957 100644 --- a/test/testthreadexecutor.cpp +++ b/test/testthreadexecutor.cpp @@ -233,10 +233,9 @@ class TestThreadExecutorBase : public TestFixture { "int main() {}", dinit(CheckOptions, $.showtime = SHOWTIME_MODES::SHOWTIME_TOP5_FILE)); - // for each file: top5 results + overall + empty line const std::string output_s = GET_REDIRECT_OUTPUT; - // for each file: top5 results + overall + empty line - ASSERT_EQUALS((5 + 1 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n')); + // for each file: top5 results + overall + ASSERT_EQUALS((5 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n')); } void showtime_top5_summary() { @@ -246,8 +245,8 @@ class TestThreadExecutorBase : public TestFixture { dinit(CheckOptions, $.showtime = SHOWTIME_MODES::SHOWTIME_TOP5_SUMMARY)); const std::string output_s = GET_REDIRECT_OUTPUT; - // once: top5 results + overall + empty line - ASSERT_EQUALS(5 + 1 + 1, cppcheck::count_all_of(output_s, '\n')); + // once: top5 results + overall + ASSERT_EQUALS(5 + 1, cppcheck::count_all_of(output_s, '\n')); // should only report the top5 once ASSERT(output_s.find("1 result(s)") == std::string::npos); ASSERT(output_s.find("2 result(s)") != std::string::npos); @@ -260,7 +259,7 @@ class TestThreadExecutorBase : public TestFixture { dinit(CheckOptions, $.showtime = SHOWTIME_MODES::SHOWTIME_FILE)); const std::string output_s = GET_REDIRECT_OUTPUT; - ASSERT_EQUALS(2, cppcheck::count_all_of(output_s, "Overall time:")); + ASSERT_EQUALS(1, cppcheck::count_all_of(output_s, "Overall time:")); } void showtime_summary() { From 170782ad20f5af7828eef5d4b611a86a86b9c2bb Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 27 Oct 2025 12:39:23 +0000 Subject: [PATCH 06/10] Test fix --- test/testsingleexecutor.cpp | 2 +- test/testthreadexecutor.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/testsingleexecutor.cpp b/test/testsingleexecutor.cpp index 1b995b8a489..9bcedd1bc92 100644 --- a/test/testsingleexecutor.cpp +++ b/test/testsingleexecutor.cpp @@ -267,7 +267,7 @@ class TestSingleExecutorBase : public TestFixture { dinit(CheckOptions, $.showtime = SHOWTIME_MODES::SHOWTIME_FILE)); const std::string output_s = GET_REDIRECT_OUTPUT; - ASSERT_EQUALS(1, cppcheck::count_all_of(output_s, "Overall time:")); + ASSERT_EQUALS(0, cppcheck::count_all_of(output_s, "Overall time:")); } void showtime_summary() { diff --git a/test/testthreadexecutor.cpp b/test/testthreadexecutor.cpp index 252b8ffa957..f783367c11b 100644 --- a/test/testthreadexecutor.cpp +++ b/test/testthreadexecutor.cpp @@ -259,7 +259,7 @@ class TestThreadExecutorBase : public TestFixture { dinit(CheckOptions, $.showtime = SHOWTIME_MODES::SHOWTIME_FILE)); const std::string output_s = GET_REDIRECT_OUTPUT; - ASSERT_EQUALS(1, cppcheck::count_all_of(output_s, "Overall time:")); + ASSERT_EQUALS(0, cppcheck::count_all_of(output_s, "Overall time:")); } void showtime_summary() { From 7006ea405bd872a961900b3f69f001569b42a2ed Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 27 Oct 2025 12:51:38 +0000 Subject: [PATCH 07/10] include --- lib/timer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/timer.h b/lib/timer.h index c0b3dccda31..be85c50c54c 100644 --- a/lib/timer.h +++ b/lib/timer.h @@ -22,6 +22,7 @@ #include "config.h" +#include #include #include #include From d8c18c31922c668449ee980b9a3acc88015a9bb8 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Mon, 27 Oct 2025 13:06:36 +0000 Subject: [PATCH 08/10] Small nit --- lib/valueflow.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 1f725673874..a039fea47f0 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -112,7 +112,6 @@ #include #include #include -#include #include #include #include From 6e587b51af881c00905197b41862ff4efc59bbd7 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Tue, 28 Oct 2025 10:30:26 +0000 Subject: [PATCH 09/10] Nits --- cli/cppcheckexecutor.cpp | 6 +++--- lib/cppcheck.cpp | 7 ++++--- lib/timer.cpp | 26 ++++++++++++++------------ lib/timer.h | 19 +++++++++++-------- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/cli/cppcheckexecutor.cpp b/cli/cppcheckexecutor.cpp index 742a9fc1c90..0c91672ea67 100644 --- a/cli/cppcheckexecutor.cpp +++ b/cli/cppcheckexecutor.cpp @@ -260,7 +260,7 @@ namespace { int CppCheckExecutor::check(int argc, const char* const argv[]) { - auto startTime{Timer::now()}; + Timer realTimeClock("", SHOWTIME_MODES::SHOWTIME_SUMMARY); Settings settings; CmdLineLoggerStd logger; Suppressions supprs; @@ -279,8 +279,8 @@ int CppCheckExecutor::check(int argc, const char* const argv[]) const int ret = check_wrapper(settings, supprs); - if (settings.showtime != SHOWTIME_MODES::SHOWTIME_NONE) - Timer::calculateAndOutputTimeDiff(startTime, Timer::now()); + if (settings.showtime == SHOWTIME_MODES::SHOWTIME_NONE) + realTimeClock.cancelRealTimeMeasurement(); return ret; } diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index e6ce7a4df11..fb1da1c5d9f 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -895,7 +895,9 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str if (Settings::terminated()) return mLogger->exitcode(); - const Timer fileTotalTimer(mSettings.showtime == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL, file.spath()); + Timer WholeFileTimer{file.spath()}; + if (mSettings.showtime != SHOWTIME_MODES::SHOWTIME_FILE_TOTAL) + WholeFileTimer.cancelRealTimeMeasurement(); if (!mSettings.quiet) { std::string fixedpath = Path::toNativeSeparators(file.spath()); @@ -1311,8 +1313,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str // TODO: clear earlier? mLogger->clear(); - if (mSettings.showtime == SHOWTIME_MODES::SHOWTIME_FILE || mSettings.showtime == SHOWTIME_MODES::SHOWTIME_TOP5_FILE) - printTimerResults(mSettings.showtime); + printTimerResults(mSettings.showtime); return mLogger->exitcode(); } diff --git a/lib/timer.cpp b/lib/timer.cpp index 9e743a4a88a..fc697dc0858 100644 --- a/lib/timer.cpp +++ b/lib/timer.cpp @@ -105,11 +105,13 @@ Timer::Timer(std::string str, SHOWTIME_MODES showtimeMode, TimerResultsIntf* tim , mStart(std::clock()) , mShowTimeMode(showtimeMode) , mStopped(showtimeMode == SHOWTIME_MODES::SHOWTIME_NONE || showtimeMode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL) + , mStartTimePoint(Clock::now()) {} -Timer::Timer(bool fileTotal, std::string filename) - : mStr(std::move(filename)) - , mStopped(!fileTotal) +Timer::Timer(std::string str) + : mStr(std::move(str)) + , mShowTimeMode(SHOWTIME_MODES::SHOWTIME_FILE_TOTAL) + , mStartTimePoint(Clock::now()) {} Timer::~Timer() @@ -127,22 +129,25 @@ void Timer::stop() const double sec = static_cast(diff) / CLOCKS_PER_SEC; std::lock_guard l(stdCoutLock); std::cout << mStr << ": " << sec << "s" << std::endl; - } else if (mShowTimeMode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL) { - const double sec = static_cast(diff) / CLOCKS_PER_SEC; + } else if (mShowTimeMode == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL && mStartTimePoint != TimePoint{}) { std::lock_guard l(stdCoutLock); - std::cout << "Check time: " << mStr << ": " << sec << "s" << std::endl; + std::cout << "Check time: " << mStr << ": " << getRealTimePassed() << std::endl; } else { if (mTimerResults) mTimerResults->addResults(mStr, diff); + else if (mStr.empty() && mStartTimePoint != TimePoint{}) { // Get real time + std::lock_guard l(stdCoutLock); + std::cout << "Overall time: " << getRealTimePassed() << std::endl; + } } } mStopped = true; } -void Timer::calculateAndOutputTimeDiff(const tp& start, const tp& end) +std::string Timer::getRealTimePassed() { - auto diff = std::chrono::duration_cast(end - start); + auto diff = std::chrono::duration_cast(Clock::now() - mStartTimePoint); // Extract hours auto hours = std::chrono::duration_cast(diff); @@ -160,8 +165,5 @@ void Timer::calculateAndOutputTimeDiff(const tp& start, const tp& end) ellapsedTime += std::to_string(hours.count()) + "h "; if (minutes.count() > 0) ellapsedTime += std::to_string(minutes.count()) + "m "; - ellapsedTime += std::to_string(seconds) + "s "; - - std::lock_guard l(stdCoutLock); - std::cout << "Overall time: " << ellapsedTime << std::endl; + return (ellapsedTime + std::to_string(seconds) + "s "); } diff --git a/lib/timer.h b/lib/timer.h index be85c50c54c..87962bb5234 100644 --- a/lib/timer.h +++ b/lib/timer.h @@ -74,33 +74,36 @@ class CPPCHECKLIB TimerResults : public TimerResultsIntf { class CPPCHECKLIB Timer { public: Timer(std::string str, SHOWTIME_MODES showtimeMode, TimerResultsIntf* timerResults = nullptr); - Timer(bool fileTotal, std::string filename); + Timer(std::string str); ~Timer(); Timer(const Timer&) = delete; Timer& operator=(const Timer&) = delete; - using tp = std::chrono::time_point; - - static tp now() { - return std::chrono::high_resolution_clock::now(); - } - - static void calculateAndOutputTimeDiff(const tp& start, const tp& end); + using Clock = std::chrono::high_resolution_clock; + using TimePoint = std::chrono::time_point; void stop(); + void cancelRealTimeMeasurement() { + mStartTimePoint = TimePoint{}; + } + static void run(std::string str, SHOWTIME_MODES showtimeMode, TimerResultsIntf* timerResults, const std::function& f) { Timer t(std::move(str), showtimeMode, timerResults); f(); } private: + + std::string getRealTimePassed(); + const std::string mStr; TimerResultsIntf* mTimerResults{}; std::clock_t mStart = std::clock(); const SHOWTIME_MODES mShowTimeMode = SHOWTIME_MODES::SHOWTIME_FILE_TOTAL; bool mStopped{}; + TimePoint mStartTimePoint{}; }; //--------------------------------------------------------------------------- From 2633020a00e15a3dca33b670e325297d4b9fcd5b Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Tue, 28 Oct 2025 10:45:09 +0000 Subject: [PATCH 10/10] Nits 2 --- lib/cppcheck.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index fb1da1c5d9f..0bbe92cbf03 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -1313,7 +1313,8 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str // TODO: clear earlier? mLogger->clear(); - printTimerResults(mSettings.showtime); + if (mSettings.showtime == SHOWTIME_MODES::SHOWTIME_FILE || mSettings.showtime == SHOWTIME_MODES::SHOWTIME_TOP5_FILE) + printTimerResults(mSettings.showtime); return mLogger->exitcode(); }