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

View File

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

View File

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