Skip to content

Commit

Permalink
added and used Timer::run() helper (#4533)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Aug 7, 2024
1 parent 89434e1 commit bf6ae27
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 52 deletions.
22 changes: 12 additions & 10 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,9 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
// Get configurations..
std::set<std::string> configurations;
if ((mSettings.checkAllConfigurations && mSettings.userDefines.empty()) || mSettings.force) {
Timer t("Preprocessor::getConfigs", mSettings.showtime, &s_timerResults);
configurations = preprocessor.getConfigs(tokens1);
Timer::run("Preprocessor::getConfigs", mSettings.showtime, &s_timerResults, [&]() {
configurations = preprocessor.getConfigs(tokens1);
});
} else {
configurations.insert(mSettings.userDefines);
}
Expand Down Expand Up @@ -843,9 +844,10 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
}

if (mSettings.preprocessOnly) {
Timer t("Preprocessor::getcode", mSettings.showtime, &s_timerResults);
std::string codeWithoutCfg = preprocessor.getcode(tokens1, mCurrentConfig, files, true);
t.stop();
std::string codeWithoutCfg;
Timer::run("Preprocessor::getcode", mSettings.showtime, &s_timerResults, [&]() {
codeWithoutCfg = preprocessor.getcode(tokens1, mCurrentConfig, files, true);
});

if (startsWith(codeWithoutCfg,"#file"))
codeWithoutCfg.insert(0U, "//");
Expand All @@ -869,11 +871,10 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string

try {
// Create tokens, skip rest of iteration if failed
{
Timer timer("Tokenizer::createTokens", mSettings.showtime, &s_timerResults);
Timer::run("Tokenizer::createTokens", mSettings.showtime, &s_timerResults, [&]() {
simplecpp::TokenList tokensP = preprocessor.preprocess(tokens1, mCurrentConfig, files, true);
tokenizer.list.createTokens(std::move(tokensP));
}
});
hasValidConfig = true;

// locations macros
Expand Down Expand Up @@ -1075,8 +1076,9 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
return;
}

Timer timerRunChecks(check->name() + "::runChecks", mSettings.showtime, &s_timerResults);
check->runChecks(tokenizer, this);
Timer::run(check->name() + "::runChecks", mSettings.showtime, &s_timerResults, [&]() {
check->runChecks(tokenizer, this);
});
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <cstdint>
#include <ctime>
#include <functional>
#include <map>
#include <mutex>
#include <string>
Expand Down Expand Up @@ -79,6 +80,11 @@ class CPPCHECKLIB Timer {

void stop();

static void run(std::string str, SHOWTIME_MODES showtimeMode, TimerResultsIntf* timerResults, const std::function<void()>& f) {
Timer t(std::move(str), showtimeMode, timerResults);
f();
}

private:
const std::string mStr;
TimerResultsIntf* mTimerResults{};
Expand Down
62 changes: 20 additions & 42 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3407,30 +3407,21 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration)
return false;
}

if (mTimerResults) {
Timer t("Tokenizer::simplifyTokens1::createAst", mSettings.showtime, mTimerResults);
list.createAst();
list.validateAst(mSettings.debugnormal);
} else {
const SHOWTIME_MODES showTime = mTimerResults ? mSettings.showtime : SHOWTIME_MODES::SHOWTIME_NONE;

Timer::run("Tokenizer::simplifyTokens1::createAst", showTime, mTimerResults, [&]() {
list.createAst();
list.validateAst(mSettings.debugnormal);
}
});

if (mTimerResults) {
Timer t("Tokenizer::simplifyTokens1::createSymbolDatabase", mSettings.showtime, mTimerResults);
Timer::run("Tokenizer::simplifyTokens1::createSymbolDatabase", showTime, mTimerResults, [&]() {
createSymbolDatabase();
} else {
createSymbolDatabase();
}
});

if (mTimerResults) {
Timer t("Tokenizer::simplifyTokens1::setValueType", mSettings.showtime, mTimerResults);
mSymbolDatabase->setValueTypeInTokenList(false);
mSymbolDatabase->setValueTypeInTokenList(true);
} else {
Timer::run("Tokenizer::simplifyTokens1::setValueType", showTime, mTimerResults, [&]() {
mSymbolDatabase->setValueTypeInTokenList(false);
mSymbolDatabase->setValueTypeInTokenList(true);
}
});

if (!mSettings.buildDir.empty())
Summaries::create(*this, configuration);
Expand All @@ -3441,12 +3432,9 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration)
const bool doValueFlow = !disableValueflowEnv || (std::strcmp(disableValueflowEnv, "1") != 0);

if (doValueFlow) {
if (mTimerResults) {
Timer t("Tokenizer::simplifyTokens1::ValueFlow", mSettings.showtime, mTimerResults);
Timer::run("Tokenizer::simplifyTokens1::ValueFlow", showTime, mTimerResults, [&]() {
ValueFlow::setValues(list, *mSymbolDatabase, mErrorLogger, mSettings, mTimerResults);
} else {
ValueFlow::setValues(list, *mSymbolDatabase, mErrorLogger, mSettings, mTimerResults);
}
});

arraySizeAfterValueFlow();
}
Expand Down Expand Up @@ -5557,13 +5545,12 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])

validate();

const SHOWTIME_MODES showTime = mTimerResults ? mSettings.showtime : SHOWTIME_MODES::SHOWTIME_NONE;

// Bail out if code is garbage
if (mTimerResults) {
Timer t("Tokenizer::simplifyTokens1::simplifyTokenList1::findGarbageCode", mSettings.showtime, mTimerResults);
Timer::run("Tokenizer::simplifyTokens1::simplifyTokenList1::findGarbageCode", showTime, mTimerResults, [&]() {
findGarbageCode();
} else {
findGarbageCode();
}
});

checkConfiguration();

Expand Down Expand Up @@ -5718,12 +5705,9 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
simplifyTypedefLHS();

// typedef..
if (mTimerResults) {
Timer t("Tokenizer::simplifyTokens1::simplifyTokenList1::simplifyTypedef", mSettings.showtime, mTimerResults);
Timer::run("Tokenizer::simplifyTokens1::simplifyTokenList1::simplifyTypedef", showTime, mTimerResults, [&]() {
simplifyTypedef();
} else {
simplifyTypedef();
}
});

// using A = B;
while (simplifyUsing())
Expand Down Expand Up @@ -5815,12 +5799,9 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
simplifyTypeIntrinsics();

// Handle templates..
if (mTimerResults) {
Timer t("Tokenizer::simplifyTokens1::simplifyTokenList1::simplifyTemplates", mSettings.showtime, mTimerResults);
simplifyTemplates();
} else {
Timer::run("Tokenizer::simplifyTokens1::simplifyTokenList1::simplifyTemplates", showTime, mTimerResults, [&]() {
simplifyTemplates();
}
});

// The simplifyTemplates have inner loops
if (Settings::terminated())
Expand All @@ -5845,12 +5826,9 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])

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

if (mTimerResults) {
Timer t("Tokenizer::simplifyTokens1::simplifyTokenList1::setVarId", mSettings.showtime, mTimerResults);
setVarId();
} else {
Timer::run("Tokenizer::simplifyTokens1::simplifyTokenList1::setVarId", showTime, mTimerResults, [&](){
setVarId();
}
});

// Link < with >
createLinks2();
Expand Down

0 comments on commit bf6ae27

Please sign in to comment.