Skip to content

Commit fd857cf

Browse files
committed
refs #14599 - moved ShowTime logic out of Timer
1 parent d2bb637 commit fd857cf

7 files changed

Lines changed: 35 additions & 41 deletions

File tree

cli/cppcheckexecutor.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,9 @@ bool CppCheckExecutor::reportUnmatchedSuppressions(const Settings &settings, con
420420
int CppCheckExecutor::check_internal(const Settings& settings, Suppressions& supprs) const
421421
{
422422
StdLogger stdLogger(settings);
423-
TimerResults timerResults;
423+
std::unique_ptr<TimerResults> timerResults;
424+
if (settings.showtime != ShowTime::NONE)
425+
timerResults.reset(new TimerResults);
424426

425427
if (settings.reportProgress >= 0)
426428
stdLogger.resetLatestProgressOutputTime();
@@ -441,33 +443,35 @@ int CppCheckExecutor::check_internal(const Settings& settings, Suppressions& sup
441443
if (!settings.checkersReportFilename.empty())
442444
std::remove(settings.checkersReportFilename.c_str());
443445

444-
CppCheck cppcheck(settings, supprs, stdLogger, &timerResults, true, executeCommand);
446+
CppCheck cppcheck(settings, supprs, stdLogger, timerResults.get(), true, executeCommand);
445447

446448
unsigned int returnValue = 0;
447449
if (settings.useSingleJob()) {
448450
// Single process
449-
SingleExecutor executor(cppcheck, mFiles, mFileSettings, settings, supprs, stdLogger, &timerResults);
451+
SingleExecutor executor(cppcheck, mFiles, mFileSettings, settings, supprs, stdLogger, timerResults.get());
450452
returnValue = executor.check();
451453
} else {
452454
#if defined(HAS_THREADING_MODEL_THREAD)
453455
if (settings.executor == Settings::ExecutorType::Thread) {
454-
ThreadExecutor executor(mFiles, mFileSettings, settings, supprs, stdLogger, &timerResults, CppCheckExecutor::executeCommand);
456+
ThreadExecutor executor(mFiles, mFileSettings, settings, supprs, stdLogger, timerResults.get(), CppCheckExecutor::executeCommand);
455457
returnValue = executor.check();
456458
}
457459
#endif
458460
#if defined(HAS_THREADING_MODEL_FORK)
459461
if (settings.executor == Settings::ExecutorType::Process) {
460-
ProcessExecutor executor(mFiles, mFileSettings, settings, supprs, stdLogger, &timerResults, CppCheckExecutor::executeCommand);
462+
ProcessExecutor executor(mFiles, mFileSettings, settings, supprs, stdLogger, timerResults.get(), CppCheckExecutor::executeCommand);
461463
returnValue = executor.check();
462464
}
463465
#endif
464466
}
465467

466468
// TODO: show time *after* the whole program analysis
467-
if (settings.showtime == ShowTime::SUMMARY)
468-
timerResults.showResults();
469-
else if (settings.showtime == ShowTime::TOP5_SUMMARY)
470-
timerResults.showResults(5);
469+
if (timerResults) {
470+
if (settings.showtime == ShowTime::SUMMARY)
471+
timerResults->showResults();
472+
else if (settings.showtime == ShowTime::TOP5_SUMMARY)
473+
timerResults->showResults(5);
474+
}
471475

472476
// TODO: is this run again instead of using previously cached results?
473477
returnValue |= cppcheck.analyseWholeProgram(settings.buildDir, mFiles, mFileSettings, stdLogger.getCtuInfo());

lib/cppcheck.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
10451045
// Get configurations..
10461046
std::set<std::string> configurations;
10471047
if (maxConfigs > 1) {
1048-
Timer::run("Preprocessor::getConfigs", mSettings.showtime, mTimerResults, [&]() {
1048+
Timer::run("Preprocessor::getConfigs", mTimerResults, [&]() {
10491049
configurations = preprocessor.getConfigs();
10501050
});
10511051
} else {
@@ -1130,7 +1130,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
11301130

11311131
if (mSettings.preprocessOnly) {
11321132
std::string codeWithoutCfg;
1133-
Timer::run("Preprocessor::getcode", mSettings.showtime, mTimerResults, [&]() {
1133+
Timer::run("Preprocessor::getcode", mTimerResults, [&]() {
11341134
codeWithoutCfg = preprocessor.getcode(currentConfig, files, true);
11351135
});
11361136

@@ -1154,7 +1154,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
11541154
{
11551155
bool skipCfg = false;
11561156
// Create tokens, skip rest of iteration if failed
1157-
Timer::run("Tokenizer::createTokens", mSettings.showtime, mTimerResults, [&]() {
1157+
Timer::run("Tokenizer::createTokens", mTimerResults, [&]() {
11581158
simplecpp::OutputList outputList_cfg;
11591159
simplecpp::TokenList tokensP = preprocessor.preprocess(currentConfig, files, outputList_cfg);
11601160
const simplecpp::Output* o = preprocessor.handleErrors(outputList_cfg);
@@ -1358,7 +1358,7 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer, AnalyzerInformation
13581358
return;
13591359
}
13601360

1361-
Timer::run(check->name() + "::runChecks", mSettings.showtime, mTimerResults, [&]() {
1361+
Timer::run(check->name() + "::runChecks", mTimerResults, [&]() {
13621362
check->runChecks(tokenizer, &mErrorLogger);
13631363
});
13641364
}
@@ -1493,7 +1493,7 @@ void CppCheck::executeAddons(const std::string& dumpFile, const FileWithDetails&
14931493
{
14941494
if (!dumpFile.empty()) {
14951495
std::vector<std::string> f{dumpFile};
1496-
Timer::run("CppCheck::executeAddons", mSettings.showtime, mTimerResults, [&]() {
1496+
Timer::run("CppCheck::executeAddons", mTimerResults, [&]() {
14971497
executeAddons(f, file.spath());
14981498
});
14991499
}

lib/timer.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,10 @@ void TimerResults::reset()
8282
mResults.clear();
8383
}
8484

85-
Timer::Timer(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults)
85+
Timer::Timer(std::string str, TimerResultsIntf* timerResults)
8686
: mName(std::move(str))
8787
, mResults(timerResults)
8888
{
89-
if (showtimeMode == ShowTime::NONE)
90-
return;
9189
if (!mResults)
9290
return;
9391
mStart = Clock::now();
@@ -152,5 +150,5 @@ OneShotTimer::OneShotTimer(std::string name, ShowTime showtime)
152150
};
153151

154152
mResults.reset(new MyResults);
155-
mTimer.reset(new Timer(std::move(name), showtime, mResults.get()));
153+
mTimer.reset(new Timer(std::move(name), mResults.get()));
156154
}

lib/timer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class CPPCHECKLIB Timer {
8282
using Clock = std::chrono::high_resolution_clock;
8383
using TimePoint = std::chrono::time_point<Clock>;
8484

85-
Timer(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults = nullptr);
85+
Timer(std::string str, TimerResultsIntf* timerResults = nullptr);
8686
~Timer();
8787

8888
Timer(const Timer&) = delete;
@@ -91,8 +91,8 @@ class CPPCHECKLIB Timer {
9191
void stop();
9292

9393
template<class TFunc>
94-
static void run(std::string str, ShowTime showtimeMode, TimerResultsIntf* timerResults, const TFunc& f) {
95-
Timer t(std::move(str), showtimeMode, timerResults);
94+
static void run(std::string str, TimerResultsIntf* timerResults, const TFunc& f) {
95+
Timer t(std::move(str), timerResults);
9696
f();
9797
}
9898

lib/tokenize.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3508,26 +3508,24 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration, int fileIndex)
35083508
mConfiguration = configuration;
35093509

35103510
if (mTimerResults) {
3511-
Timer t("Tokenizer::simplifyTokens1::simplifyTokenList1", mSettings.showtime, mTimerResults);
3511+
Timer t("Tokenizer::simplifyTokens1::simplifyTokenList1", mTimerResults);
35123512
if (!simplifyTokenList1(list.getFiles().front().c_str()))
35133513
return false;
35143514
} else {
35153515
if (!simplifyTokenList1(list.getFiles().front().c_str()))
35163516
return false;
35173517
}
35183518

3519-
const ShowTime showTime = mTimerResults ? mSettings.showtime : ShowTime::NONE;
3520-
3521-
Timer::run("Tokenizer::simplifyTokens1::createAst", showTime, mTimerResults, [&]() {
3519+
Timer::run("Tokenizer::simplifyTokens1::createAst", mTimerResults, [&]() {
35223520
list.createAst();
35233521
list.validateAst(mSettings.debugnormal);
35243522
});
35253523

3526-
Timer::run("Tokenizer::simplifyTokens1::createSymbolDatabase", showTime, mTimerResults, [&]() {
3524+
Timer::run("Tokenizer::simplifyTokens1::createSymbolDatabase", mTimerResults, [&]() {
35273525
createSymbolDatabase();
35283526
});
35293527

3530-
Timer::run("Tokenizer::simplifyTokens1::setValueType", showTime, mTimerResults, [&]() {
3528+
Timer::run("Tokenizer::simplifyTokens1::setValueType", mTimerResults, [&]() {
35313529
mSymbolDatabase->setValueTypeInTokenList(false);
35323530
mSymbolDatabase->setValueTypeInTokenList(true);
35333531
});
@@ -3542,7 +3540,7 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration, int fileIndex)
35423540
const bool doValueFlow = !disableValueflowEnv || (std::strcmp(disableValueflowEnv, "1") != 0);
35433541

35443542
if (doValueFlow) {
3545-
Timer::run("Tokenizer::simplifyTokens1::ValueFlow", showTime, mTimerResults, [&]() {
3543+
Timer::run("Tokenizer::simplifyTokens1::ValueFlow", mTimerResults, [&]() {
35463544
ValueFlow::setValues(list, *mSymbolDatabase, mErrorLogger, mSettings, mTimerResults);
35473545
});
35483546

@@ -5753,10 +5751,8 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
57535751

57545752
validate();
57555753

5756-
const ShowTime showTime = mTimerResults ? mSettings.showtime : ShowTime::NONE;
5757-
57585754
// Bail out if code is garbage
5759-
Timer::run("Tokenizer::simplifyTokens1::simplifyTokenList1::findGarbageCode", showTime, mTimerResults, [&]() {
5755+
Timer::run("Tokenizer::simplifyTokens1::simplifyTokenList1::findGarbageCode", mTimerResults, [&]() {
57605756
findGarbageCode();
57615757
});
57625758

@@ -5893,7 +5889,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
58935889
simplifyTypedefLHS();
58945890

58955891
// typedef..
5896-
Timer::run("Tokenizer::simplifyTokens1::simplifyTokenList1::simplifyTypedef", showTime, mTimerResults, [&]() {
5892+
Timer::run("Tokenizer::simplifyTokens1::simplifyTokenList1::simplifyTypedef", mTimerResults, [&]() {
58975893
simplifyTypedef();
58985894
});
58995895

@@ -5987,7 +5983,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
59875983
simplifyTypeIntrinsics();
59885984

59895985
// Handle templates..
5990-
Timer::run("Tokenizer::simplifyTokens1::simplifyTokenList1::simplifyTemplates", showTime, mTimerResults, [&]() {
5986+
Timer::run("Tokenizer::simplifyTokens1::simplifyTokenList1::simplifyTemplates", mTimerResults, [&]() {
59915987
simplifyTemplates();
59925988
});
59935989

@@ -6014,7 +6010,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
60146010

60156011
validate(); // #6772 "segmentation fault (invalid code) in Tokenizer::setVarId"
60166012

6017-
Timer::run("Tokenizer::simplifyTokens1::simplifyTokenList1::setVarId", showTime, mTimerResults, [&](){
6013+
Timer::run("Tokenizer::simplifyTokens1::simplifyTokenList1::setVarId", mTimerResults, [&](){
60186014
setVarId();
60196015
});
60206016

lib/valueflow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7266,7 +7266,7 @@ struct ValueFlowPassRunner {
72667266
name += ' ';
72677267
name += std::to_string(it);
72687268
}
7269-
Timer t(name, state.settings.showtime, timerResults);
7269+
Timer t(name, timerResults);
72707270
pass->run(state);
72717271
} else {
72727272
pass->run(state);

test/fixture.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ bool TestFixture::prepareTest(const char testname[])
116116
std::cout << fullTestName << std::endl;
117117
}
118118
if (timer_results)
119-
mTimer.reset(new Timer(fullTestName, ShowTime::TOP5_SUMMARY, timer_results));
119+
mTimer.reset(new Timer(fullTestName, timer_results));
120120
return !dry_run;
121121
}
122122

@@ -423,11 +423,7 @@ std::size_t TestFixture::runTests(const options& args)
423423
const auto f = [&](){
424424
fixture = test->create();
425425
};
426-
// TODO: Timer::run() needs proper handling if no results should be collected
427-
if (args.timer_results())
428-
Timer::run(test->classname + " - create", ShowTime::TOP5_SUMMARY, args.timer_results(), f);
429-
else
430-
f();
426+
Timer::run(test->classname + " - create", args.timer_results(), f);
431427
fixture->processOptions(args);
432428
fixture->run(tests);
433429
}

0 commit comments

Comments
 (0)