small `ErrorLogger` usage cleanups (#4033)

This commit is contained in:
Oliver Stöneberg 2022-04-21 21:30:22 +02:00 committed by GitHub
parent fdca61add9
commit 5e6cc1053a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 87 additions and 61 deletions

View File

@ -391,10 +391,10 @@ void ThreadExecutor::reportInternalChildErr(const std::string &childname, const
#elif defined(THREADING_MODEL_THREAD)
class ThreadExecutor::LogWriter : public ErrorLogger
class ThreadExecutor::SyncLogForwarder : public ErrorLogger
{
public:
LogWriter(ThreadExecutor &threadExecutor)
SyncLogForwarder(ThreadExecutor &threadExecutor)
: mThreadExecutor(threadExecutor), mProcessedFiles(0), mTotalFiles(0), mProcessedSize(0), mTotalFileSize(0) {
mItNextFile = threadExecutor.mFiles.begin();
@ -475,11 +475,11 @@ unsigned int ThreadExecutor::check()
std::vector<std::future<unsigned int>> threadFutures;
threadFutures.reserve(mSettings.jobs);
LogWriter logwriter(*this);
SyncLogForwarder logforwarder(*this);
for (unsigned int i = 0; i < mSettings.jobs; ++i) {
try {
threadFutures.emplace_back(std::async(std::launch::async, threadProc, &logwriter));
threadFutures.emplace_back(std::async(std::launch::async, threadProc, &logforwarder));
}
catch (const std::system_error &e) {
std::cerr << "#### ThreadExecutor::check exception :" << e.what() << std::endl;
@ -492,51 +492,51 @@ unsigned int ThreadExecutor::check()
});
}
unsigned int STDCALL ThreadExecutor::threadProc(LogWriter* logWriter)
unsigned int STDCALL ThreadExecutor::threadProc(SyncLogForwarder* logForwarder)
{
unsigned int result = 0;
std::map<std::string, std::size_t>::const_iterator &itFile = logWriter->mItNextFile;
std::list<ImportProject::FileSettings>::const_iterator &itFileSettings = logWriter->mItNextFileSettings;
std::map<std::string, std::size_t>::const_iterator &itFile = logForwarder->mItNextFile;
std::list<ImportProject::FileSettings>::const_iterator &itFileSettings = logForwarder->mItNextFileSettings;
// guard static members of CppCheck against concurrent access
logWriter->mFileSync.lock();
logForwarder->mFileSync.lock();
for (;;) {
if (itFile == logWriter->mThreadExecutor.mFiles.end() && itFileSettings == logWriter->mThreadExecutor.mSettings.project.fileSettings.end()) {
logWriter->mFileSync.unlock();
if (itFile == logForwarder->mThreadExecutor.mFiles.end() && itFileSettings == logForwarder->mThreadExecutor.mSettings.project.fileSettings.end()) {
logForwarder->mFileSync.unlock();
break;
}
CppCheck fileChecker(*logWriter, false, CppCheckExecutor::executeCommand);
fileChecker.settings() = logWriter->mThreadExecutor.mSettings;
CppCheck fileChecker(*logForwarder, false, CppCheckExecutor::executeCommand);
fileChecker.settings() = logForwarder->mThreadExecutor.mSettings;
std::size_t fileSize = 0;
if (itFile != logWriter->mThreadExecutor.mFiles.end()) {
if (itFile != logForwarder->mThreadExecutor.mFiles.end()) {
const std::string &file = itFile->first;
fileSize = itFile->second;
++itFile;
logWriter->mFileSync.unlock();
logForwarder->mFileSync.unlock();
// Read file from a file
result += fileChecker.check(file);
} else { // file settings..
const ImportProject::FileSettings &fs = *itFileSettings;
++itFileSettings;
logWriter->mFileSync.unlock();
logForwarder->mFileSync.unlock();
result += fileChecker.check(fs);
if (logWriter->mThreadExecutor.mSettings.clangTidy)
if (logForwarder->mThreadExecutor.mSettings.clangTidy)
fileChecker.analyseClangTidy(fs);
}
logWriter->mFileSync.lock();
logForwarder->mFileSync.lock();
logWriter->mProcessedSize += fileSize;
logWriter->mProcessedFiles++;
if (!logWriter->mThreadExecutor.mSettings.quiet) {
std::lock_guard<std::mutex> lg(logWriter->mReportSync);
CppCheckExecutor::reportStatus(logWriter->mProcessedFiles, logWriter->mTotalFiles, logWriter->mProcessedSize, logWriter->mTotalFileSize);
logForwarder->mProcessedSize += fileSize;
logForwarder->mProcessedFiles++;
if (!logForwarder->mThreadExecutor.mSettings.quiet) {
std::lock_guard<std::mutex> lg(logForwarder->mReportSync);
CppCheckExecutor::reportStatus(logForwarder->mProcessedFiles, logForwarder->mTotalFiles, logForwarder->mProcessedSize, logForwarder->mTotalFileSize);
}
}
return result;

View File

@ -75,8 +75,8 @@ private:
#elif defined(THREADING_MODEL_THREAD)
class LogWriter;
static unsigned int STDCALL threadProc(LogWriter *threadExecutor);
class SyncLogForwarder;
static unsigned int STDCALL threadProc(SyncLogForwarder *logforwarder);
#endif

View File

@ -16,53 +16,30 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "color.h"
#include "cppcheck.h"
#include "type2.h"
enum class Color;
class CppcheckExecutor : public ErrorLogger {
private:
CppCheck cppcheck;
class DummyErrorLogger : public ErrorLogger {
public:
CppcheckExecutor()
: ErrorLogger()
, cppcheck(*this, false, nullptr) {
cppcheck.settings().addEnabled("all");
cppcheck.settings().certainty.setEnabled(Certainty::inconclusive, true);
}
void run(const std::string &code) {
cppcheck.check("test.cpp", code);
}
void reportOut(const std::string &outmsg, Color) override {
(void)outmsg;
}
void reportErr(const ErrorMessage &msg) override {
(void)msg;
}
void reportProgress(const std::string& filename,
const char stage[],
const std::size_t value) override {
(void)filename;
(void)stage;
(void)value;
}
void reportOut(const std::string&, Color) override {}
void reportErr(const ErrorMessage&) override {}
void reportProgress(const std::string&,
const char[],
const std::size_t) override {}
};
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataSize)
{
if (dataSize < 10000) {
const std::string code = generateCode2(data, dataSize);
//std::ofstream fout("code.cpp");
//fout << code;
//fout.close();
CppcheckExecutor cppcheckExecutor;
cppcheckExecutor.run(code);
DummyErrorLogger errorLogger;
CppCheck cppcheck(errorLogger, false, nullptr);
cppcheck.settings().addEnabled("all");
cppcheck.settings().certainty.setEnabled(Certainty::inconclusive, true);
cppcheck.check("test.cpp", code);
}
return 0;
}

View File

@ -1,3 +1,20 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2022 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <fstream>

View File

@ -1,9 +1,24 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2022 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include "type2.h"
static int getValue(const uint8_t *data, size_t dataSize, uint8_t maxValue, bool *done = nullptr)
{
static size_t pos; // current "data" position

View File

@ -1,3 +1,20 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2022 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once