2009-02-19 23:21:18 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2009-02-19 23:21:18 +01:00
|
|
|
*
|
|
|
|
* 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
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-02-19 23:21:18 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "threadexecutor.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2023-03-04 12:05:17 +01:00
|
|
|
#include "config.h"
|
2009-02-19 23:21:18 +01:00
|
|
|
#include "cppcheck.h"
|
2013-09-04 06:18:22 +02:00
|
|
|
#include "cppcheckexecutor.h"
|
2022-02-22 09:54:35 +01:00
|
|
|
#include "errorlogger.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "importproject.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <functional>
|
2021-08-26 19:36:31 +02:00
|
|
|
#include <future>
|
2022-07-08 16:42:57 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <list>
|
2021-08-26 19:36:31 +02:00
|
|
|
#include <numeric>
|
2022-07-08 16:42:57 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <system_error>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2009-02-19 23:21:18 +01:00
|
|
|
|
2023-04-08 16:08:47 +02:00
|
|
|
enum class Color;
|
|
|
|
|
2012-07-08 23:39:46 +02:00
|
|
|
ThreadExecutor::ThreadExecutor(const std::map<std::string, std::size_t> &files, Settings &settings, ErrorLogger &errorLogger)
|
2022-07-08 16:42:57 +02:00
|
|
|
: Executor(files, settings, errorLogger)
|
2022-02-22 09:54:35 +01:00
|
|
|
{}
|
2009-02-19 23:21:18 +01:00
|
|
|
|
|
|
|
ThreadExecutor::~ThreadExecutor()
|
2022-01-18 22:02:25 +01:00
|
|
|
{}
|
|
|
|
|
2023-03-09 20:08:39 +01:00
|
|
|
class SyncLogForwarder : public ErrorLogger
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit SyncLogForwarder(ThreadExecutor &threadExecutor, ErrorLogger &errorLogger)
|
|
|
|
: mThreadExecutor(threadExecutor), mErrorLogger(errorLogger) {}
|
|
|
|
|
|
|
|
void reportOut(const std::string &outmsg, Color c) override
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lg(mReportSync);
|
|
|
|
|
|
|
|
mErrorLogger.reportOut(outmsg, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reportErr(const ErrorMessage &msg) override {
|
|
|
|
if (!mThreadExecutor.hasToLog(msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lg(mReportSync);
|
|
|
|
mErrorLogger.reportErr(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reportStatus(std::size_t fileindex, std::size_t filecount, std::size_t sizedone, std::size_t sizetotal) {
|
|
|
|
std::lock_guard<std::mutex> lg(mReportSync);
|
2023-04-08 18:06:38 +02:00
|
|
|
mThreadExecutor.reportStatus(fileindex, filecount, sizedone, sizetotal);
|
2023-03-09 20:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex mReportSync;
|
|
|
|
ThreadExecutor &mThreadExecutor;
|
|
|
|
ErrorLogger &mErrorLogger;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadData
|
2022-02-22 09:54:35 +01:00
|
|
|
{
|
|
|
|
public:
|
2023-03-09 20:08:39 +01:00
|
|
|
ThreadData(ThreadExecutor &threadExecutor, ErrorLogger &errorLogger, const Settings &settings, const std::map<std::string, std::size_t> &files, const std::list<ImportProject::FileSettings> &fileSettings)
|
|
|
|
: mFiles(files), mFileSettings(fileSettings), mProcessedFiles(0), mProcessedSize(0), mSettings(settings), logForwarder(threadExecutor, errorLogger)
|
2023-03-04 12:05:17 +01:00
|
|
|
{
|
|
|
|
mItNextFile = mFiles.begin();
|
|
|
|
mItNextFileSettings = mFileSettings.begin();
|
2022-02-22 09:54:35 +01:00
|
|
|
|
2023-03-04 12:05:17 +01:00
|
|
|
mTotalFiles = mFiles.size() + mFileSettings.size();
|
|
|
|
mTotalFileSize = std::accumulate(mFiles.cbegin(), mFiles.cend(), std::size_t(0), [](std::size_t v, const std::pair<std::string, std::size_t>& p) {
|
2022-10-16 13:46:26 +02:00
|
|
|
return v + p.second;
|
|
|
|
});
|
2022-02-22 09:54:35 +01:00
|
|
|
}
|
|
|
|
|
2023-03-04 12:05:17 +01:00
|
|
|
bool next(const std::string *&file, const ImportProject::FileSettings *&fs, std::size_t &fileSize) {
|
|
|
|
std::lock_guard<std::mutex> l(mFileSync);
|
|
|
|
if (mItNextFile != mFiles.end()) {
|
|
|
|
file = &mItNextFile->first;
|
2023-03-09 20:08:39 +01:00
|
|
|
fs = nullptr;
|
2023-03-04 12:05:17 +01:00
|
|
|
fileSize = mItNextFile->second;
|
|
|
|
++mItNextFile;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (mItNextFileSettings != mFileSettings.end()) {
|
2023-03-09 20:08:39 +01:00
|
|
|
file = nullptr;
|
2023-03-04 12:05:17 +01:00
|
|
|
fs = &(*mItNextFileSettings);
|
|
|
|
fileSize = 0;
|
|
|
|
++mItNextFileSettings;
|
|
|
|
return true;
|
|
|
|
}
|
2022-02-22 09:54:35 +01:00
|
|
|
|
2023-03-04 12:05:17 +01:00
|
|
|
return false;
|
2022-02-22 09:54:35 +01:00
|
|
|
}
|
|
|
|
|
2023-03-09 20:08:39 +01:00
|
|
|
unsigned int check(ErrorLogger &errorLogger, const std::string *file, const ImportProject::FileSettings *fs) const {
|
|
|
|
CppCheck fileChecker(errorLogger, false, CppCheckExecutor::executeCommand);
|
|
|
|
fileChecker.settings() = mSettings; // this is a copy
|
|
|
|
|
|
|
|
unsigned int result;
|
|
|
|
if (fs) {
|
|
|
|
// file settings..
|
|
|
|
result = fileChecker.check(*fs);
|
|
|
|
if (fileChecker.settings().clangTidy)
|
|
|
|
fileChecker.analyseClangTidy(*fs);
|
|
|
|
} else {
|
|
|
|
// Read file from a file
|
|
|
|
result = fileChecker.check(*file);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void status(std::size_t fileSize) {
|
|
|
|
std::lock_guard<std::mutex> l(mFileSync);
|
|
|
|
mProcessedSize += fileSize;
|
|
|
|
mProcessedFiles++;
|
|
|
|
if (!mSettings.quiet)
|
|
|
|
logForwarder.reportStatus(mProcessedFiles, mTotalFiles, mProcessedSize, mTotalFileSize);
|
|
|
|
}
|
|
|
|
|
2023-03-04 12:05:17 +01:00
|
|
|
private:
|
|
|
|
const std::map<std::string, std::size_t> &mFiles;
|
2022-02-22 09:54:35 +01:00
|
|
|
std::map<std::string, std::size_t>::const_iterator mItNextFile;
|
2023-03-04 12:05:17 +01:00
|
|
|
const std::list<ImportProject::FileSettings> &mFileSettings;
|
2022-02-22 09:54:35 +01:00
|
|
|
std::list<ImportProject::FileSettings>::const_iterator mItNextFileSettings;
|
|
|
|
|
|
|
|
std::size_t mProcessedFiles;
|
|
|
|
std::size_t mTotalFiles;
|
|
|
|
std::size_t mProcessedSize;
|
|
|
|
std::size_t mTotalFileSize;
|
|
|
|
|
|
|
|
std::mutex mFileSync;
|
2023-03-09 20:08:39 +01:00
|
|
|
const Settings &mSettings;
|
2023-03-04 12:05:17 +01:00
|
|
|
|
|
|
|
public:
|
2023-03-09 20:08:39 +01:00
|
|
|
SyncLogForwarder logForwarder;
|
2023-03-04 12:05:17 +01:00
|
|
|
};
|
|
|
|
|
2023-03-09 20:08:39 +01:00
|
|
|
static unsigned int STDCALL threadProc(ThreadData *data)
|
2023-03-04 12:05:17 +01:00
|
|
|
{
|
|
|
|
unsigned int result = 0;
|
|
|
|
|
2023-03-09 20:08:39 +01:00
|
|
|
const std::string *file;
|
|
|
|
const ImportProject::FileSettings *fs;
|
|
|
|
std::size_t fileSize;
|
2023-03-04 12:05:17 +01:00
|
|
|
|
2023-03-09 20:08:39 +01:00
|
|
|
while (data->next(file, fs, fileSize)) {
|
|
|
|
result += data->check(data->logForwarder, file, fs);
|
2023-03-04 12:05:17 +01:00
|
|
|
|
2023-03-09 20:08:39 +01:00
|
|
|
data->status(fileSize);
|
2022-02-22 09:54:35 +01:00
|
|
|
}
|
2023-03-09 20:08:39 +01:00
|
|
|
|
2023-03-04 12:05:17 +01:00
|
|
|
return result;
|
|
|
|
}
|
2022-02-22 09:54:35 +01:00
|
|
|
|
2012-12-12 19:09:37 +01:00
|
|
|
unsigned int ThreadExecutor::check()
|
|
|
|
{
|
2021-08-24 20:39:43 +02:00
|
|
|
std::vector<std::future<unsigned int>> threadFutures;
|
|
|
|
threadFutures.reserve(mSettings.jobs);
|
2012-12-12 19:09:37 +01:00
|
|
|
|
2023-03-09 20:08:39 +01:00
|
|
|
ThreadData data(*this, mErrorLogger, mSettings, mFiles, mSettings.project.fileSettings);
|
2012-12-13 18:47:13 +01:00
|
|
|
|
2019-07-15 18:45:06 +02:00
|
|
|
for (unsigned int i = 0; i < mSettings.jobs; ++i) {
|
2021-08-24 20:39:43 +02:00
|
|
|
try {
|
2023-03-09 20:08:39 +01:00
|
|
|
threadFutures.emplace_back(std::async(std::launch::async, &threadProc, &data));
|
2012-12-12 19:09:37 +01:00
|
|
|
}
|
2021-08-24 20:39:43 +02:00
|
|
|
catch (const std::system_error &e) {
|
|
|
|
std::cerr << "#### ThreadExecutor::check exception :" << e.what() << std::endl;
|
2012-12-12 19:09:37 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 20:39:43 +02:00
|
|
|
return std::accumulate(threadFutures.begin(), threadFutures.end(), 0U, [](unsigned int v, std::future<unsigned int>& f) {
|
|
|
|
return v + f.get();
|
|
|
|
});
|
2012-12-12 19:09:37 +01:00
|
|
|
}
|