Skip to content

Commit dbc5a82

Browse files
committed
Fix #14516 (Review progress messages)
1 parent 3dc52dc commit dbc5a82

5 files changed

Lines changed: 51 additions & 12 deletions

File tree

lib/cppcheck.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,8 @@ void CppCheck::internalError(const std::string &filename, const std::string &msg
13161316

13171317
void CppCheck::checkNormalTokens(const Tokenizer &tokenizer, AnalyzerInformation* analyzerInformation, const std::string& currentConfig)
13181318
{
1319+
ProgressReporter(mErrorLogger, mSettings.reportProgress >= 0, tokenizer.list.getSourceFilePath(), "Run checkers");
1320+
13191321
CheckUnusedFunctions unusedFunctionsChecker;
13201322

13211323
// TODO: this should actually be the behavior if only "--enable=unusedFunction" is specified - see #10648

lib/errorlogger.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <cstddef>
2828
#include <cstdint>
29+
#include <ctime>
2930
#include <list>
3031
#include <set>
3132
#include <string>
@@ -286,6 +287,41 @@ class CPPCHECKLIB ErrorLogger {
286287
static const std::set<std::string> mCriticalErrorIds;
287288
};
288289

290+
/// RAII class for reporting progress messages
291+
class CPPCHECKLIB ProgressReporter {
292+
public:
293+
ProgressReporter(ErrorLogger& e, bool reportProgress, const std::string& filename, const std::string& stage) :
294+
errorLogger(e),
295+
reportProgress(reportProgress),
296+
filename(filename),
297+
stage(stage) {
298+
if (reportProgress)
299+
errorLogger.reportProgress(filename, stage.c_str(), 0);
300+
}
301+
302+
~ProgressReporter() {
303+
if (reportProgress)
304+
errorLogger.reportProgress(filename, stage.c_str(), 100);
305+
}
306+
307+
void report(int value) {
308+
if (!reportProgress)
309+
return;
310+
const auto t = std::time(nullptr);
311+
if (t != lastTime) {
312+
errorLogger.reportProgress(filename, stage.c_str(), value);
313+
lastTime = t;
314+
}
315+
}
316+
317+
private:
318+
ErrorLogger& errorLogger;
319+
const bool reportProgress;
320+
const std::string filename;
321+
const std::string stage;
322+
std::time_t lastTime{0};
323+
};
324+
289325
/** Replace substring. Example replaceStr("1,NR,3", "NR", "2") => "1,2,3" */
290326
std::string replaceStr(std::string s, const std::string &from, const std::string &to);
291327

lib/symboldatabase.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
174174
// Store current access in each scope (depends on evaluation progress)
175175
std::map<const Scope*, AccessControl> access;
176176

177-
const bool doProgress = (mSettings.reportProgress != -1);
178-
179177
std::map<Scope *, std::set<std::string>> forwardDecls;
180178

181179
const std::function<Scope *(const Token *, Scope *)> findForwardDeclScope = [&](const Token *tok, Scope *startScope) {
@@ -200,13 +198,14 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
200198
return it->second.count(tok->str()) > 0 ? startScope : nullptr;
201199
};
202200

201+
ProgressReporter progressReporter(mErrorLogger, (mSettings.reportProgress >= 0), mTokenizer.list.getSourceFilePath(), "SymbolDatabase (find all scopes)");
202+
203203
// find all scopes
204204
for (const Token *tok = mTokenizer.tokens(); tok; tok = tok ? tok->next() : nullptr) {
205205
// #5593 suggested to add here:
206-
if (doProgress)
207-
mErrorLogger.reportProgress(mTokenizer.list.getSourceFilePath(),
208-
"SymbolDatabase",
209-
tok->progressValue());
206+
207+
progressReporter.report(tok->progressValue());
208+
210209
// Locate next class
211210
if ((tok->isCpp() && tok->isKeyword() &&
212211
((Token::Match(tok, "class|struct|union|namespace ::| %name% final| {|:|::|<") &&

lib/tokenize.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,11 +1167,10 @@ void Tokenizer::simplifyTypedefCpp()
11671167
std::vector<Space> spaceInfo(1);
11681168

11691169
const std::time_t maxTime = mSettings.typedefMaxTime > 0 ? std::time(nullptr) + mSettings.typedefMaxTime: 0;
1170-
const bool doProgress = (mSettings.reportProgress != -1) && !list.getFiles().empty();
1170+
ProgressReporter progressReporter(mErrorLogger, (mSettings.reportProgress >= 0), list.getSourceFilePath(), "Tokenize (typedef)");
11711171

11721172
for (Token *tok = list.front(); tok; tok = tok->next()) {
1173-
if (doProgress)
1174-
mErrorLogger.reportProgress(list.getFiles()[0], "Tokenize (typedef)", tok->progressValue());
1173+
progressReporter.report(tok->progressValue());
11751174

11761175
if (Settings::terminated())
11771176
return;
@@ -2932,11 +2931,10 @@ bool Tokenizer::simplifyUsing()
29322931
};
29332932
std::list<Using> usingList;
29342933

2935-
const bool doProgress = (mSettings.reportProgress != -1) && !list.getFiles().empty();
2934+
ProgressReporter progressReporter(mErrorLogger, (mSettings.reportProgress >= 0), list.getSourceFilePath(), "Tokenize (using)");
29362935

29372936
for (Token *tok = list.front(); tok; tok = tok->next()) {
2938-
if (doProgress)
2939-
mErrorLogger.reportProgress(list.getFiles()[0], "Tokenize (using)", tok->progressValue());
2937+
progressReporter.report(tok->progressValue());
29402938

29412939
if (Settings::terminated())
29422940
return substitute;

lib/valueflow.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7202,7 +7202,9 @@ struct ValueFlowPassRunner {
72027202
std::size_t n = state.settings.vfOptions.maxIterations;
72037203
while (n > 0 && values != getTotalValues()) {
72047204
values = getTotalValues();
7205+
const std::string passnum = std::to_string(state.settings.vfOptions.maxIterations - n + 1);
72057206
if (std::any_of(passes.begin(), passes.end(), [&](const ValuePtr<ValueFlowPass>& pass) {
7207+
ProgressReporter progressReporter(state.errorLogger, state.settings.reportProgress >= 0, state.tokenlist.getSourceFilePath(), std::string("ValueFlow::") + pass->name() + (' ' + passnum));
72067208
return run(pass);
72077209
}))
72087210
return true;
@@ -7346,6 +7348,8 @@ void ValueFlow::setValues(TokenList& tokenlist,
73467348
const Settings& settings,
73477349
TimerResultsIntf* timerResults)
73487350
{
7351+
ProgressReporter progressReporter(errorLogger, settings.reportProgress >= 0, tokenlist.getSourceFilePath(), "ValueFlow");
7352+
73497353
for (Token* tok = tokenlist.front(); tok; tok = tok->next())
73507354
tok->clearValueFlow();
73517355

0 commit comments

Comments
 (0)