2009-02-19 23:21:18 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2022-02-05 11:45:17 +01:00
|
|
|
* Copyright (C) 2007-2022 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
|
|
|
|
2021-07-08 21:21:35 +02:00
|
|
|
#include "color.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 "suppressions.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
|
|
|
|
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
|
|
|
{}
|
|
|
|
|
2022-04-21 21:30:22 +02:00
|
|
|
class ThreadExecutor::SyncLogForwarder : public ErrorLogger
|
2022-02-22 09:54:35 +01:00
|
|
|
{
|
|
|
|
public:
|
2022-07-08 16:42:57 +02:00
|
|
|
explicit SyncLogForwarder(ThreadExecutor &threadExecutor)
|
2022-10-16 13:46:26 +02:00
|
|
|
: mThreadExecutor(threadExecutor), mProcessedFiles(0), mTotalFiles(0), mProcessedSize(0) {
|
2022-02-22 09:54:35 +01:00
|
|
|
|
2022-10-16 13:46:26 +02:00
|
|
|
const std::map<std::string, std::size_t>& files = mThreadExecutor.mFiles;
|
|
|
|
mItNextFile = files.begin();
|
2022-07-08 16:42:57 +02:00
|
|
|
mItNextFileSettings = mThreadExecutor.mSettings.project.fileSettings.begin();
|
2022-02-22 09:54:35 +01:00
|
|
|
|
2022-10-16 13:46:26 +02:00
|
|
|
mTotalFiles = files.size() + mThreadExecutor.mSettings.project.fileSettings.size();
|
|
|
|
mTotalFileSize = std::accumulate(files.begin(), files.end(), std::size_t(0), [](std::size_t v, const std::pair<std::string, std::size_t>& p) {
|
|
|
|
return v + p.second;
|
|
|
|
});
|
2022-02-22 09:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void reportOut(const std::string &outmsg, Color c) override
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lg(mReportSync);
|
|
|
|
|
|
|
|
mThreadExecutor.mErrorLogger.reportOut(outmsg, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reportErr(const ErrorMessage &msg) override {
|
|
|
|
report(msg, MessageType::REPORT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reportInfo(const ErrorMessage &msg) override {
|
|
|
|
report(msg, MessageType::REPORT_INFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadExecutor &mThreadExecutor;
|
|
|
|
|
|
|
|
std::map<std::string, std::size_t>::const_iterator mItNextFile;
|
|
|
|
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;
|
|
|
|
std::mutex mErrorSync;
|
|
|
|
std::mutex mReportSync;
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum class MessageType {REPORT_ERROR, REPORT_INFO};
|
|
|
|
|
|
|
|
void report(const ErrorMessage &msg, MessageType msgType)
|
|
|
|
{
|
|
|
|
if (mThreadExecutor.mSettings.nomsg.isSuppressed(msg.toSuppressionsErrorMessage()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Alert only about unique errors
|
|
|
|
bool reportError = false;
|
|
|
|
|
|
|
|
{
|
2022-09-10 11:25:15 +02:00
|
|
|
std::string errmsg = msg.toString(mThreadExecutor.mSettings.verbose);
|
|
|
|
|
2022-02-22 09:54:35 +01:00
|
|
|
std::lock_guard<std::mutex> lg(mErrorSync);
|
|
|
|
if (std::find(mThreadExecutor.mErrorList.begin(), mThreadExecutor.mErrorList.end(), errmsg) == mThreadExecutor.mErrorList.end()) {
|
2022-09-10 11:25:15 +02:00
|
|
|
mThreadExecutor.mErrorList.emplace_back(std::move(errmsg));
|
2022-02-22 09:54:35 +01:00
|
|
|
reportError = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reportError) {
|
|
|
|
std::lock_guard<std::mutex> lg(mReportSync);
|
|
|
|
|
|
|
|
switch (msgType) {
|
|
|
|
case MessageType::REPORT_ERROR:
|
|
|
|
mThreadExecutor.mErrorLogger.reportErr(msg);
|
|
|
|
break;
|
|
|
|
case MessageType::REPORT_INFO:
|
|
|
|
mThreadExecutor.mErrorLogger.reportInfo(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2022-04-21 21:30:22 +02:00
|
|
|
SyncLogForwarder logforwarder(*this);
|
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 {
|
2022-04-21 21:30:22 +02:00
|
|
|
threadFutures.emplace_back(std::async(std::launch::async, threadProc, &logforwarder));
|
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
|
|
|
}
|
|
|
|
|
2022-04-21 21:30:22 +02:00
|
|
|
unsigned int STDCALL ThreadExecutor::threadProc(SyncLogForwarder* logForwarder)
|
2012-12-12 19:09:37 +01:00
|
|
|
{
|
|
|
|
unsigned int result = 0;
|
|
|
|
|
2022-04-21 21:30:22 +02:00
|
|
|
std::map<std::string, std::size_t>::const_iterator &itFile = logForwarder->mItNextFile;
|
|
|
|
std::list<ImportProject::FileSettings>::const_iterator &itFileSettings = logForwarder->mItNextFileSettings;
|
2012-12-12 19:09:37 +01:00
|
|
|
|
|
|
|
// guard static members of CppCheck against concurrent access
|
2022-04-21 21:30:22 +02:00
|
|
|
logForwarder->mFileSync.lock();
|
2012-12-12 19:09:37 +01:00
|
|
|
|
|
|
|
for (;;) {
|
2022-04-21 21:30:22 +02:00
|
|
|
if (itFile == logForwarder->mThreadExecutor.mFiles.end() && itFileSettings == logForwarder->mThreadExecutor.mSettings.project.fileSettings.end()) {
|
|
|
|
logForwarder->mFileSync.unlock();
|
2015-01-21 18:46:33 +01:00
|
|
|
break;
|
2016-08-07 17:10:37 +02:00
|
|
|
}
|
2012-12-12 19:09:37 +01:00
|
|
|
|
2022-04-21 21:30:22 +02:00
|
|
|
CppCheck fileChecker(*logForwarder, false, CppCheckExecutor::executeCommand);
|
|
|
|
fileChecker.settings() = logForwarder->mThreadExecutor.mSettings;
|
2021-08-17 20:51:31 +02:00
|
|
|
|
2016-08-13 10:50:03 +02:00
|
|
|
std::size_t fileSize = 0;
|
2022-04-21 21:30:22 +02:00
|
|
|
if (itFile != logForwarder->mThreadExecutor.mFiles.end()) {
|
2016-08-13 10:50:03 +02:00
|
|
|
const std::string &file = itFile->first;
|
|
|
|
fileSize = itFile->second;
|
|
|
|
++itFile;
|
2016-08-07 14:30:09 +02:00
|
|
|
|
2022-04-21 21:30:22 +02:00
|
|
|
logForwarder->mFileSync.unlock();
|
2016-08-13 10:50:03 +02:00
|
|
|
|
2022-03-02 11:10:29 +01:00
|
|
|
// Read file from a file
|
|
|
|
result += fileChecker.check(file);
|
2016-08-13 10:50:03 +02:00
|
|
|
} else { // file settings..
|
|
|
|
const ImportProject::FileSettings &fs = *itFileSettings;
|
|
|
|
++itFileSettings;
|
2022-04-21 21:30:22 +02:00
|
|
|
logForwarder->mFileSync.unlock();
|
2016-08-13 10:50:03 +02:00
|
|
|
result += fileChecker.check(fs);
|
2022-04-21 21:30:22 +02:00
|
|
|
if (logForwarder->mThreadExecutor.mSettings.clangTidy)
|
2020-03-15 11:09:35 +01:00
|
|
|
fileChecker.analyseClangTidy(fs);
|
2012-12-12 19:09:37 +01:00
|
|
|
}
|
|
|
|
|
2022-04-21 21:30:22 +02:00
|
|
|
logForwarder->mFileSync.lock();
|
2012-12-13 18:47:13 +01:00
|
|
|
|
2022-04-21 21:30:22 +02:00
|
|
|
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);
|
2012-12-13 18:47:13 +01:00
|
|
|
}
|
2015-01-21 18:46:33 +01:00
|
|
|
}
|
2012-12-12 19:09:37 +01:00
|
|
|
return result;
|
|
|
|
}
|