Skip to content

Commit 0b5b8f6

Browse files
committed
refs #14599 - do not implicitly print in TimerResults::stop()
1 parent 77cbab3 commit 0b5b8f6

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

cli/cppcheckexecutor.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
269269
return EXIT_SUCCESS;
270270
}
271271

272-
Timer realTimeClock("", settings.showtime, nullptr, Timer::Type::OVERALL);
272+
TimerResults overallTimerResults;
273+
Timer realTimeClock("Overall time", settings.showtime, &overallTimerResults, Timer::Type::OVERALL);
273274

274275
settings.loadSummaries();
275276

@@ -278,6 +279,9 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
278279

279280
const int ret = check_wrapper(settings, supprs);
280281

282+
realTimeClock.stop();
283+
overallTimerResults.showResults(settings.showtime, false);
284+
281285
return ret;
282286
}
283287

lib/cppcheck.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,8 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
923923
if (Settings::terminated())
924924
return mLogger->exitcode();
925925

926-
const Timer fileTotalTimer{file.spath(), mSettings.showtime, nullptr, Timer::Type::FILE};
926+
TimerResults checkTimeResults;
927+
Timer fileTotalTimer{"Check time: " + file.spath(), mSettings.showtime, &checkTimeResults, Timer::Type::FILE};
927928

928929
if (!mSettings.quiet) {
929930
std::string fixedpath = Path::toNativeSeparators(file.spath());
@@ -1295,6 +1296,9 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
12951296
if (mTimerResults && (mSettings.showtime == ShowTime::FILE || mSettings.showtime == ShowTime::TOP5_FILE))
12961297
mTimerResults->showResults(mSettings.showtime);
12971298

1299+
fileTotalTimer.stop();
1300+
checkTimeResults.showResults(mSettings.showtime, false);
1301+
12981302
return mLogger->exitcode();
12991303
}
13001304

lib/timer.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "timer.h"
2020

2121
#include <algorithm>
22+
#include <cassert>
2223
#include <cstddef>
2324
#include <iostream>
2425
#include <utility>
@@ -37,9 +38,9 @@ namespace {
3738

3839
// TODO: this does not include any file context when SHOWTIME_FILE thus rendering it useless - should we include the logging with the progress logging?
3940
// that could also get rid of the broader locking
40-
void TimerResults::showResults(ShowTime mode) const
41+
void TimerResults::showResults(ShowTime mode, bool metrics) const
4142
{
42-
if (mode == ShowTime::NONE || mode == ShowTime::FILE_TOTAL)
43+
if (mode == ShowTime::NONE)
4344
return;
4445
std::vector<dataElementType> data;
4546

@@ -59,7 +60,10 @@ void TimerResults::showResults(ShowTime mode) const
5960
const double sec = iter->second.getSeconds().count();
6061
const double secAverage = sec / static_cast<double>(iter->second.mNumberOfResults);
6162
if ((mode != ShowTime::TOP5_FILE && mode != ShowTime::TOP5_SUMMARY) || (ordinal<=5)) {
62-
std::cout << iter->first << ": " << sec << "s (avg. " << secAverage << "s - " << iter->second.mNumberOfResults << " result(s))" << std::endl;
63+
std::cout << iter->first << ": " << sec << "s";
64+
if (metrics)
65+
std::cout << " (avg. " << secAverage << "s - " << iter->second.mNumberOfResults << " result(s))";
66+
std::cout << std::endl;
6367
}
6468
++ordinal;
6569
}
@@ -105,12 +109,11 @@ void Timer::stop()
105109
return;
106110
}
107111
if (mStart != TimePoint{}) {
108-
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - mStart);
109112
if (!mResults) {
110-
// TODO: do not print implicitly
111-
std::lock_guard<std::mutex> l(stdCoutLock);
112-
std::cout << (mType == Type::OVERALL ? "Overall time: " : "Check time: " + mName + ": ") << TimerResultsData::durationToString(diff) << std::endl;
113-
} else {
113+
assert(false);
114+
}
115+
else {
116+
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - mStart);
114117
mResults->addResults(mName, diff);
115118
}
116119
}

lib/timer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class CPPCHECKLIB WARN_UNUSED TimerResults : public TimerResultsIntf {
6060
public:
6161
TimerResults() = default;
6262

63-
void showResults(ShowTime mode) const;
63+
void showResults(ShowTime mode, bool metrics = true) const;
6464
void addResults(const std::string& str, std::chrono::milliseconds duration) override;
6565

6666
void reset();

test/cli/other_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,7 @@ def __test_showtime(tmp_path, showtime, exp_res, exp_last, extra_args=None):
990990
for i in range(1, exp_res):
991991
assert 'avg.' in lines[i]
992992
assert lines[exp_len-1].startswith(exp_last)
993+
assert not 'avg.' in lines[exp_len-1]
993994
assert stderr == ''
994995

995996

0 commit comments

Comments
 (0)