Skip to content

Commit 5e6cc10

Browse files
authored
small ErrorLogger usage cleanups (#4033)
1 parent fdca61a commit 5e6cc10

6 files changed

Lines changed: 87 additions & 61 deletions

File tree

cli/threadexecutor.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,10 @@ void ThreadExecutor::reportInternalChildErr(const std::string &childname, const
391391

392392
#elif defined(THREADING_MODEL_THREAD)
393393

394-
class ThreadExecutor::LogWriter : public ErrorLogger
394+
class ThreadExecutor::SyncLogForwarder : public ErrorLogger
395395
{
396396
public:
397-
LogWriter(ThreadExecutor &threadExecutor)
397+
SyncLogForwarder(ThreadExecutor &threadExecutor)
398398
: mThreadExecutor(threadExecutor), mProcessedFiles(0), mTotalFiles(0), mProcessedSize(0), mTotalFileSize(0) {
399399

400400
mItNextFile = threadExecutor.mFiles.begin();
@@ -475,11 +475,11 @@ unsigned int ThreadExecutor::check()
475475
std::vector<std::future<unsigned int>> threadFutures;
476476
threadFutures.reserve(mSettings.jobs);
477477

478-
LogWriter logwriter(*this);
478+
SyncLogForwarder logforwarder(*this);
479479

480480
for (unsigned int i = 0; i < mSettings.jobs; ++i) {
481481
try {
482-
threadFutures.emplace_back(std::async(std::launch::async, threadProc, &logwriter));
482+
threadFutures.emplace_back(std::async(std::launch::async, threadProc, &logforwarder));
483483
}
484484
catch (const std::system_error &e) {
485485
std::cerr << "#### ThreadExecutor::check exception :" << e.what() << std::endl;
@@ -492,51 +492,51 @@ unsigned int ThreadExecutor::check()
492492
});
493493
}
494494

495-
unsigned int STDCALL ThreadExecutor::threadProc(LogWriter* logWriter)
495+
unsigned int STDCALL ThreadExecutor::threadProc(SyncLogForwarder* logForwarder)
496496
{
497497
unsigned int result = 0;
498498

499-
std::map<std::string, std::size_t>::const_iterator &itFile = logWriter->mItNextFile;
500-
std::list<ImportProject::FileSettings>::const_iterator &itFileSettings = logWriter->mItNextFileSettings;
499+
std::map<std::string, std::size_t>::const_iterator &itFile = logForwarder->mItNextFile;
500+
std::list<ImportProject::FileSettings>::const_iterator &itFileSettings = logForwarder->mItNextFileSettings;
501501

502502
// guard static members of CppCheck against concurrent access
503-
logWriter->mFileSync.lock();
503+
logForwarder->mFileSync.lock();
504504

505505
for (;;) {
506-
if (itFile == logWriter->mThreadExecutor.mFiles.end() && itFileSettings == logWriter->mThreadExecutor.mSettings.project.fileSettings.end()) {
507-
logWriter->mFileSync.unlock();
506+
if (itFile == logForwarder->mThreadExecutor.mFiles.end() && itFileSettings == logForwarder->mThreadExecutor.mSettings.project.fileSettings.end()) {
507+
logForwarder->mFileSync.unlock();
508508
break;
509509
}
510510

511-
CppCheck fileChecker(*logWriter, false, CppCheckExecutor::executeCommand);
512-
fileChecker.settings() = logWriter->mThreadExecutor.mSettings;
511+
CppCheck fileChecker(*logForwarder, false, CppCheckExecutor::executeCommand);
512+
fileChecker.settings() = logForwarder->mThreadExecutor.mSettings;
513513

514514
std::size_t fileSize = 0;
515-
if (itFile != logWriter->mThreadExecutor.mFiles.end()) {
515+
if (itFile != logForwarder->mThreadExecutor.mFiles.end()) {
516516
const std::string &file = itFile->first;
517517
fileSize = itFile->second;
518518
++itFile;
519519

520-
logWriter->mFileSync.unlock();
520+
logForwarder->mFileSync.unlock();
521521

522522
// Read file from a file
523523
result += fileChecker.check(file);
524524
} else { // file settings..
525525
const ImportProject::FileSettings &fs = *itFileSettings;
526526
++itFileSettings;
527-
logWriter->mFileSync.unlock();
527+
logForwarder->mFileSync.unlock();
528528
result += fileChecker.check(fs);
529-
if (logWriter->mThreadExecutor.mSettings.clangTidy)
529+
if (logForwarder->mThreadExecutor.mSettings.clangTidy)
530530
fileChecker.analyseClangTidy(fs);
531531
}
532532

533-
logWriter->mFileSync.lock();
533+
logForwarder->mFileSync.lock();
534534

535-
logWriter->mProcessedSize += fileSize;
536-
logWriter->mProcessedFiles++;
537-
if (!logWriter->mThreadExecutor.mSettings.quiet) {
538-
std::lock_guard<std::mutex> lg(logWriter->mReportSync);
539-
CppCheckExecutor::reportStatus(logWriter->mProcessedFiles, logWriter->mTotalFiles, logWriter->mProcessedSize, logWriter->mTotalFileSize);
535+
logForwarder->mProcessedSize += fileSize;
536+
logForwarder->mProcessedFiles++;
537+
if (!logForwarder->mThreadExecutor.mSettings.quiet) {
538+
std::lock_guard<std::mutex> lg(logForwarder->mReportSync);
539+
CppCheckExecutor::reportStatus(logForwarder->mProcessedFiles, logForwarder->mTotalFiles, logForwarder->mProcessedSize, logForwarder->mTotalFileSize);
540540
}
541541
}
542542
return result;

cli/threadexecutor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class ThreadExecutor {
7575

7676
#elif defined(THREADING_MODEL_THREAD)
7777

78-
class LogWriter;
79-
static unsigned int STDCALL threadProc(LogWriter *threadExecutor);
78+
class SyncLogForwarder;
79+
static unsigned int STDCALL threadProc(SyncLogForwarder *logforwarder);
8080

8181
#endif
8282

oss-fuzz/main.cpp

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,30 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
#include "color.h"
2019
#include "cppcheck.h"
2120
#include "type2.h"
2221

22+
enum class Color;
2323

24-
class CppcheckExecutor : public ErrorLogger {
25-
private:
26-
CppCheck cppcheck;
27-
24+
class DummyErrorLogger : public ErrorLogger {
2825
public:
29-
CppcheckExecutor()
30-
: ErrorLogger()
31-
, cppcheck(*this, false, nullptr) {
32-
cppcheck.settings().addEnabled("all");
33-
cppcheck.settings().certainty.setEnabled(Certainty::inconclusive, true);
34-
}
35-
36-
void run(const std::string &code) {
37-
cppcheck.check("test.cpp", code);
38-
}
39-
40-
void reportOut(const std::string &outmsg, Color) override {
41-
(void)outmsg;
42-
}
43-
void reportErr(const ErrorMessage &msg) override {
44-
(void)msg;
45-
}
46-
void reportProgress(const std::string& filename,
47-
const char stage[],
48-
const std::size_t value) override {
49-
(void)filename;
50-
(void)stage;
51-
(void)value;
52-
}
26+
void reportOut(const std::string&, Color) override {}
27+
void reportErr(const ErrorMessage&) override {}
28+
void reportProgress(const std::string&,
29+
const char[],
30+
const std::size_t) override {}
5331
};
5432

55-
5633
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize)
5734
{
5835
if (dataSize < 10000) {
5936
const std::string code = generateCode2(data, dataSize);
60-
//std::ofstream fout("code.cpp");
61-
//fout << code;
62-
//fout.close();
6337

64-
CppcheckExecutor cppcheckExecutor;
65-
cppcheckExecutor.run(code);
38+
DummyErrorLogger errorLogger;
39+
CppCheck cppcheck(errorLogger, false, nullptr);
40+
cppcheck.settings().addEnabled("all");
41+
cppcheck.settings().certainty.setEnabled(Certainty::inconclusive, true);
42+
cppcheck.check("test.cpp", code);
6643
}
6744
return 0;
6845
}

oss-fuzz/translate.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2022 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
118

219
#include <iostream>
320
#include <fstream>

oss-fuzz/type2.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2022 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
118

219
#include <sstream>
320
#include "type2.h"
421

5-
6-
722
static int getValue(const uint8_t *data, size_t dataSize, uint8_t maxValue, bool *done = nullptr)
823
{
924
static size_t pos; // current "data" position

oss-fuzz/type2.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2022 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
118

219
#pragma once
320

0 commit comments

Comments
 (0)