Skip to content

Commit d83f797

Browse files
committed
SOme more changes
1 parent ed23668 commit d83f797

6 files changed

Lines changed: 42 additions & 33 deletions

File tree

cli/cppcheckexecutor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ int CppCheckExecutor::check(int argc, const char* const argv[])
271271
return EXIT_SUCCESS;
272272
}
273273

274-
Timer realTimeClock(Timer::OVERALL, settings.showtime);
274+
Timer realTimeClock("", settings.showtime, nullptr, Timer::Type::OVERALL);
275275

276276
settings.loadSummaries();
277277

lib/cppcheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
895895
if (Settings::terminated())
896896
return mLogger->exitcode();
897897

898-
const Timer fileTotalTimer{file.spath(), mSettings.showtime};
898+
const Timer fileTotalTimer{file.spath(), mSettings.showtime, nullptr, Timer::Type::FILE};
899899

900900
if (!mSettings.quiet) {
901901
std::string fixedpath = Path::toNativeSeparators(file.spath());

lib/timer.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include <utility>
2626
#include <vector>
2727

28-
constexpr char Timer::OVERALL[];
29-
3028
namespace {
3129
using dataElementType = std::pair<std::string, TimerResultsData>;
3230
bool more_second_sec(const dataElementType& lhs, const dataElementType& rhs)
@@ -84,11 +82,12 @@ void TimerResults::reset()
8482
mResults.clear();
8583
}
8684

87-
Timer::Timer(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults)
88-
: mStr(std::move(str))
89-
, mTimerResults(timerResults)
90-
, mShowTimeMode(showtimeMode)
91-
, mStartTimePoint(Clock::now())
85+
Timer::Timer(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults, Type type)
86+
: mName(std::move(str))
87+
, mMode(showtimeMode)
88+
, mType(type)
89+
, mStart(Clock::now())
90+
, mResults(timerResults)
9291
{}
9392

9493
Timer::~Timer()
@@ -98,22 +97,26 @@ Timer::~Timer()
9897

9998
void Timer::stop()
10099
{
101-
if ((mShowTimeMode != ShowTime::NONE) && mStartTimePoint != TimePoint{}) {
102-
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - mStartTimePoint);
103-
if (!mTimerResults) {
104-
if (mStr == OVERALL
105-
&& (mShowTimeMode != ShowTime::TOP5_SUMMARY && mShowTimeMode != ShowTime::TOP5_FILE && mShowTimeMode != ShowTime::SUMMARY))
106-
return;
107-
if (mStr != OVERALL
108-
&& (mShowTimeMode != ShowTime::FILE && mShowTimeMode != ShowTime::FILE_TOTAL))
109-
return;
100+
if (mMode == ShowTime::NONE)
101+
return;
102+
if (mType == Type::OVERALL && mMode != ShowTime::TOP5_SUMMARY && mMode != ShowTime::SUMMARY) {
103+
mMode = ShowTime::NONE;
104+
return;
105+
}
106+
if (mType == Type::FILE && mMode != ShowTime::TOP5_FILE && mMode != ShowTime::FILE && mMode != ShowTime::FILE_TOTAL) {
107+
mMode = ShowTime::NONE;
108+
return;
109+
}
110+
if (mStart != TimePoint{}) {
111+
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - mStart);
112+
if (!mResults) {
110113
std::lock_guard<std::mutex> l(stdCoutLock);
111-
std::cout << (mStr == OVERALL ? "Overall time: " : "Check time: " + mStr + ": ")<< TimerResultsData::durationToString(diff) << std::endl;
114+
std::cout << (mType == Type::OVERALL ? "Overall time: " : "Check time: " + mName + ": ") << TimerResultsData::durationToString(diff) << std::endl;
112115
} else {
113-
mTimerResults->addResults(mStr, diff);
116+
mResults->addResults(mName, diff);
114117
}
115118
}
116-
mShowTimeMode = ShowTime::NONE; // prevent multiple stops
119+
mMode = ShowTime::NONE; // prevent multiple stops
117120
}
118121

119122
std::string TimerResultsData::durationToString(std::chrono::milliseconds duration)

lib/timer.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ class CPPCHECKLIB TimerResults : public TimerResultsIntf {
7474

7575
class CPPCHECKLIB Timer {
7676
public:
77-
static constexpr char OVERALL[] = "Summary";
7877
using Clock = std::chrono::high_resolution_clock;
7978
using TimePoint = std::chrono::time_point<Clock>;
8079

81-
Timer(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults = nullptr);
80+
enum class Type : std::uint8_t {
81+
FILE,
82+
OVERALL,
83+
OTHER
84+
};
85+
86+
Timer(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults = nullptr, Type type = Type::OTHER);
8287
~Timer();
8388

8489
Timer(const Timer&) = delete;
@@ -92,10 +97,11 @@ class CPPCHECKLIB Timer {
9297
}
9398

9499
private:
95-
const std::string mStr;
96-
TimerResultsIntf* mTimerResults{};
97-
ShowTime mShowTimeMode = ShowTime::FILE_TOTAL;
98-
TimePoint mStartTimePoint;
100+
const std::string mName;
101+
ShowTime mMode{};
102+
Type mType{};
103+
TimePoint mStart;
104+
TimerResultsIntf* mResults{};
99105
};
100106

101107
//---------------------------------------------------------------------------

test/testsingleexecutor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ class TestSingleExecutorBase : public TestFixture {
242242
dinit(CheckOptions,
243243
$.showtime = ShowTime::TOP5_FILE));
244244
const std::string output_s = GET_REDIRECT_OUTPUT;
245-
// for each file: top5 results + overall
246-
ASSERT_EQUALS((5 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
245+
// for each file: top5 results + overall + total
246+
ASSERT_EQUALS((5 + 1 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
247247
}
248248

249249
void showtime_top5_summary() {
@@ -253,7 +253,7 @@ class TestSingleExecutorBase : public TestFixture {
253253
dinit(CheckOptions,
254254
$.showtime = ShowTime::TOP5_SUMMARY));
255255
const std::string output_s = GET_REDIRECT_OUTPUT;
256-
// once: top5 results + overall
256+
// once: top5 results + newline
257257
ASSERT_EQUALS(5 + 1, cppcheck::count_all_of(output_s, '\n'));
258258
// should only report the top5 once
259259
ASSERT(output_s.find("1 result(s)") == std::string::npos);

test/testthreadexecutor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ class TestThreadExecutorBase : public TestFixture {
234234
dinit(CheckOptions,
235235
$.showtime = ShowTime::TOP5_FILE));
236236
const std::string output_s = GET_REDIRECT_OUTPUT;
237-
// for each file: top5 results + overall
238-
ASSERT_EQUALS((5 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
237+
// for each file: top5 results + newline + overall
238+
ASSERT_EQUALS((5 + 1 + 1) * 2LL, cppcheck::count_all_of(output_s, '\n'));
239239
}
240240

241241
void showtime_top5_summary() {
@@ -245,7 +245,7 @@ class TestThreadExecutorBase : public TestFixture {
245245
dinit(CheckOptions,
246246
$.showtime = ShowTime::TOP5_SUMMARY));
247247
const std::string output_s = GET_REDIRECT_OUTPUT;
248-
// once: top5 results + overall
248+
// once: top5 results + newline
249249
ASSERT_EQUALS(5 + 1, cppcheck::count_all_of(output_s, '\n'));
250250
// should only report the top5 once
251251
ASSERT(output_s.find("1 result(s)") == std::string::npos);

0 commit comments

Comments
 (0)