cppcheck/gui/threadhandler.cpp

289 lines
7.2 KiB
C++
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
2016-01-01 14:34:45 +01:00
* Copyright (C) 2007-2016 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/>.
*/
2010-10-31 12:16:55 +01:00
#include <QObject>
#include <QFileInfo>
2010-10-31 12:16:55 +01:00
#include <QStringList>
#include <QDebug>
#include "common.h"
2010-10-31 12:16:55 +01:00
#include "settings.h"
#include "checkthread.h"
2010-10-31 12:16:55 +01:00
#include "threadhandler.h"
#include "resultsview.h"
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;
mAddons.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));
}
}
2017-07-28 12:39:28 +02:00
void ThreadHandler::setCheckFiles(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();
}
2011-10-13 20:53:06 +02:00
for (int i = 0; i < mRunningThreadCount; i++) {
mThreads[i]->setAddons(mAddons);
2017-08-11 07:45:29 +02:00
mThreads[i]->setSuppressions(mSuppressions);
mThreads[i]->setVsIncludePaths(mVsIncludePaths);
mThreads[i]->setClangPath(mClangPath);
mThreads[i]->setDataDir(mDataDir);
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;
mTime.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()
{
2011-10-13 20:53:06 +02:00
for (int i = 0; i < mThreads.size(); i++) {
mThreads[i]->terminate();
disconnect(mThreads[i], &CheckThread::done,
2017-08-01 14:33:12 +02:00
this, &ThreadHandler::threadDone);
disconnect(mThreads[i], &CheckThread::fileChecked,
2017-08-01 14:33:12 +02:00
&mResults, &ThreadResult::fileChecked);
delete mThreads[i];
}
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 = mTime.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;
2011-10-13 20:53:06 +02:00
for (int i = 0; i < mThreads.size(); i++) {
mThreads[i]->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);
2017-07-28 12:39:28 +02:00
connect(&mResults, SIGNAL(log(const QString &)),
2017-07-28 12:22:15 +02:00
parent(), SLOT(log(const QString &)));
2017-07-28 12:39:28 +02:00
connect(&mResults, SIGNAL(debugError(const ErrorItem &)),
2017-07-28 12:22:15 +02:00
parent(), SLOT(debugError(const ErrorItem &)));
}
2017-07-28 12:39:28 +02:00
void ThreadHandler::loadSettings(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 = checkStartTime;
}