cppcheck/gui/threadhandler.cpp

299 lines
7.3 KiB
C++
Raw Permalink Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
2022-02-05 11:45:17 +01:00
* 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 "threadhandler.h"
#include "checkthread.h"
2022-02-02 16:17:28 +01:00
#include "common.h"
2010-10-31 12:16:55 +01:00
#include "resultsview.h"
2022-02-02 16:17:28 +01:00
#include "settings.h"
#include <list>
#include <string>
#include <utility>
2022-02-02 16:17:28 +01:00
#include <QDebug>
#include <QFileInfo>
#include <QSettings>
ThreadHandler::ThreadHandler(QObject *parent) :
2010-04-15 20:08:51 +02:00
QObject(parent),
mScanDuration(0),
2016-11-19 20:38:50 +01:00
mRunningThreadCount(0),
mAnalyseWholeProgram(false)
{
2017-07-28 12:39:28 +02:00
setThreadCount(1);
}
ThreadHandler::~ThreadHandler()
{
2017-07-28 13:13:10 +02:00
removeThreads();
}
2017-07-28 12:39:28 +02:00
void ThreadHandler::clearFiles()
{
mLastFiles.clear();
2017-07-28 12:39:28 +02:00
mResults.clearFiles();
2016-11-19 20:38:50 +01:00
mAnalyseWholeProgram = false;
mAddonsAndTools.clear();
2017-08-11 07:45:29 +02:00
mSuppressions.clear();
}
2017-07-28 12:39:28 +02:00
void ThreadHandler::setFiles(const QStringList &files)
{
2017-07-28 12:39:28 +02:00
mResults.setFiles(files);
mLastFiles = files;
}
2017-07-28 12:39:28 +02:00
void ThreadHandler::setProject(const ImportProject &prj)
2016-08-18 21:58:50 +02:00
{
2017-07-28 12:39:28 +02:00
mResults.setProject(prj);
2016-08-18 21:58:50 +02:00
mLastFiles.clear();
}
2017-07-28 12:39:28 +02:00
void ThreadHandler::setCheckFiles(bool all)
{
if (mRunningThreadCount == 0) {
2017-07-28 12:39:28 +02:00
mResults.setFiles(getReCheckFiles(all));
}
}
void ThreadHandler::setCheckFiles(const QStringList& files)
{
if (mRunningThreadCount == 0) {
2017-07-28 12:39:28 +02:00
mResults.setFiles(files);
}
}
void ThreadHandler::check(const Settings &settings)
{
2017-07-28 12:39:28 +02:00
if (mResults.getFileCount() == 0 || mRunningThreadCount > 0 || settings.jobs == 0) {
qDebug() << "Can't start checking if there's no files to check or if check is in progress.";
2017-07-28 12:39:28 +02:00
emit done();
return;
}
2017-07-28 12:39:28 +02:00
setThreadCount(settings.jobs);
mRunningThreadCount = mThreads.size();
2017-07-28 12:39:28 +02:00
if (mResults.getFileCount() < mRunningThreadCount) {
mRunningThreadCount = mResults.getFileCount();
}
QStringList addonsAndTools = mAddonsAndTools;
for (const std::string& addon: settings.addons) {
QString s = QString::fromStdString(addon);
if (!addonsAndTools.contains(s))
addonsAndTools << s;
}
2011-10-13 20:53:06 +02:00
for (int i = 0; i < mRunningThreadCount; i++) {
mThreads[i]->setAddonsAndTools(addonsAndTools);
2017-08-11 07:45:29 +02:00
mThreads[i]->setSuppressions(mSuppressions);
2017-08-12 12:04:42 +02:00
mThreads[i]->setClangIncludePaths(mClangIncludePaths);
2017-07-28 05:26:11 +02:00
mThreads[i]->check(settings);
}
// Date and time when checking starts..
mCheckStartTime = QDateTime::currentDateTime();
2016-11-19 20:38:50 +01:00
mAnalyseWholeProgram = true;
mTimer.start();
}
2017-07-28 12:39:28 +02:00
bool ThreadHandler::isChecking() const
{
return mRunningThreadCount > 0;
}
2017-07-28 12:39:28 +02:00
void ThreadHandler::setThreadCount(const int count)
{
if (mRunningThreadCount > 0 ||
count == mThreads.size() ||
2011-10-13 20:53:06 +02:00
count <= 0) {
return;
}
//Remove unused old threads
2017-07-28 13:13:10 +02:00
removeThreads();
//Create new threads
2011-10-13 20:53:06 +02:00
for (int i = mThreads.size(); i < count; i++) {
mThreads << new CheckThread(mResults);
connect(mThreads.last(), &CheckThread::done,
this, &ThreadHandler::threadDone);
connect(mThreads.last(), &CheckThread::fileChecked,
&mResults, &ThreadResult::fileChecked);
}
}
2017-07-28 13:13:10 +02:00
void ThreadHandler::removeThreads()
{
for (CheckThread* thread : mThreads) {
2020-09-21 19:48:04 +02:00
thread->terminate();
disconnect(thread, &CheckThread::done,
2017-08-01 14:33:12 +02:00
this, &ThreadHandler::threadDone);
disconnect(thread, &CheckThread::fileChecked,
2017-08-01 14:33:12 +02:00
&mResults, &ThreadResult::fileChecked);
delete thread;
}
mThreads.clear();
2016-11-19 20:38:50 +01:00
mAnalyseWholeProgram = false;
}
2017-07-28 12:39:28 +02:00
void ThreadHandler::threadDone()
{
2016-11-19 20:38:50 +01:00
if (mRunningThreadCount == 1 && mAnalyseWholeProgram) {
2017-07-28 05:26:11 +02:00
mThreads[0]->analyseWholeProgram(mLastFiles);
2016-11-19 20:38:50 +01:00
mAnalyseWholeProgram = false;
return;
}
mRunningThreadCount--;
2011-10-13 20:53:06 +02:00
if (mRunningThreadCount == 0) {
2017-07-28 12:39:28 +02:00
emit done();
mScanDuration = mTimer.elapsed();
// Set date/time used by the recheck
if (!mCheckStartTime.isNull()) {
mLastCheckTime = mCheckStartTime;
mCheckStartTime = QDateTime();
}
}
}
2017-07-28 12:39:28 +02:00
void ThreadHandler::stop()
{
mCheckStartTime = QDateTime();
2016-11-19 20:38:50 +01:00
mAnalyseWholeProgram = false;
for (CheckThread* thread : mThreads) {
2020-09-21 19:48:04 +02:00
thread->stop();
}
}
2017-07-28 12:39:28 +02:00
void ThreadHandler::initialize(ResultsView *view)
{
connect(&mResults, &ThreadResult::progress,
view, &ResultsView::progress);
connect(&mResults, &ThreadResult::error,
view, &ResultsView::error);
connect(&mResults, &ThreadResult::log,
this, &ThreadHandler::log);
connect(&mResults, &ThreadResult::debugError,
this, &ThreadHandler::debugError);
}
void ThreadHandler::loadSettings(const QSettings &settings)
{
2017-07-28 12:39:28 +02:00
setThreadCount(settings.value(SETTINGS_CHECK_THREADS, 1).toInt());
}
2017-07-28 12:39:28 +02:00
void ThreadHandler::saveSettings(QSettings &settings) const
{
settings.setValue(SETTINGS_CHECK_THREADS, mThreads.size());
}
2017-07-28 12:39:28 +02:00
bool ThreadHandler::hasPreviousFiles() const
{
return !mLastFiles.isEmpty();
}
2017-07-28 12:39:28 +02:00
int ThreadHandler::getPreviousFilesCount() const
{
return mLastFiles.size();
}
2017-07-28 12:39:28 +02:00
int ThreadHandler::getPreviousScanDuration() const
{
return mScanDuration;
}
2017-07-28 12:39:28 +02:00
QStringList ThreadHandler::getReCheckFiles(bool all) const
{
if (mLastCheckTime.isNull() || all)
return mLastFiles;
std::set<QString> modified;
std::set<QString> unmodified;
QStringList files;
for (int i = 0; i < mLastFiles.size(); ++i) {
2017-07-28 12:39:28 +02:00
if (needsReCheck(mLastFiles[i], modified, unmodified))
files.push_back(mLastFiles[i]);
}
return files;
}
2017-07-28 12:39:28 +02:00
bool ThreadHandler::needsReCheck(const QString &filename, std::set<QString> &modified, std::set<QString> &unmodified) const
{
if (modified.find(filename) != modified.end())
return true;
if (unmodified.find(filename) != unmodified.end())
return false;
if (QFileInfo(filename).lastModified() > mLastCheckTime) {
return true;
}
// Parse included files recursively
QFile f(filename);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
// prevent recursion..
unmodified.insert(filename);
QTextStream in(&f);
while (!in.atEnd()) {
QString line = in.readLine();
if (line.startsWith("#include \"")) {
line.remove(0,10);
int i = line.indexOf("\"");
if (i > 0) {
line.remove(i,line.length());
line = QFileInfo(filename).absolutePath() + "/" + line;
2017-07-28 12:39:28 +02:00
if (needsReCheck(line, modified, unmodified)) {
modified.insert(line);
return true;
}
}
}
}
return false;
}
2017-07-28 12:39:28 +02:00
QDateTime ThreadHandler::getCheckStartTime() const
{
return mCheckStartTime;
}
2017-07-28 12:39:28 +02:00
void ThreadHandler::setCheckStartTime(QDateTime checkStartTime)
{
mCheckStartTime = std::move(checkStartTime);
}