2009-03-01 21:44:42 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2013-01-01 17:29:08 +01:00
|
|
|
* Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
|
2009-03-01 21:44:42 +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-03-01 21:44:42 +01:00
|
|
|
*/
|
|
|
|
|
2010-10-31 12:16:55 +01:00
|
|
|
#include <QObject>
|
2012-02-14 21:16:11 +01:00
|
|
|
#include <QFileInfo>
|
2010-10-31 12:16:55 +01:00
|
|
|
#include <QStringList>
|
2009-03-01 21:44:42 +01:00
|
|
|
#include <QDebug>
|
2012-10-07 12:25:34 +02:00
|
|
|
#include "common.h"
|
2010-10-31 12:16:55 +01:00
|
|
|
#include "settings.h"
|
2012-10-07 12:25:34 +02:00
|
|
|
#include "checkthread.h"
|
2010-10-31 12:16:55 +01:00
|
|
|
#include "threadhandler.h"
|
|
|
|
#include "resultsview.h"
|
2009-03-01 21:44:42 +01:00
|
|
|
|
2009-06-20 18:55:23 +02:00
|
|
|
ThreadHandler::ThreadHandler(QObject *parent) :
|
2010-04-15 20:08:51 +02:00
|
|
|
QObject(parent),
|
2010-09-01 08:12:24 +02:00
|
|
|
mScanDuration(0),
|
2010-04-15 20:08:51 +02:00
|
|
|
mRunningThreadCount(0)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
|
|
|
SetThreadCount(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadHandler::~ThreadHandler()
|
|
|
|
{
|
2009-03-02 20:56:51 +01:00
|
|
|
RemoveThreads();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::ClearFiles()
|
|
|
|
{
|
2009-03-22 16:42:00 +01:00
|
|
|
mLastFiles.clear();
|
2009-03-01 21:44:42 +01:00
|
|
|
mResults.ClearFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::SetFiles(const QStringList &files)
|
|
|
|
{
|
|
|
|
mResults.SetFiles(files);
|
2009-03-22 16:42:00 +01:00
|
|
|
mLastFiles = files;
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2010-12-08 13:55:56 +01:00
|
|
|
void ThreadHandler::Check(const Settings &settings, bool recheck)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
if (recheck && mRunningThreadCount == 0) {
|
2012-02-14 21:16:11 +01:00
|
|
|
// only recheck changed files
|
|
|
|
mResults.SetFiles(GetReCheckFiles());
|
2009-03-22 16:42:00 +01:00
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (mResults.GetFileCount() == 0 || mRunningThreadCount > 0 || settings._jobs <= 0) {
|
2009-03-02 20:56:51 +01:00
|
|
|
qDebug() << "Can't start checking if there's no files to check or if check is in progress.";
|
2009-03-22 13:32:07 +01:00
|
|
|
emit Done();
|
2009-03-01 21:44:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetThreadCount(settings._jobs);
|
|
|
|
|
2009-03-02 20:56:51 +01:00
|
|
|
mRunningThreadCount = mThreads.size();
|
2009-03-01 21:44:42 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
if (mResults.GetFileCount() < mRunningThreadCount) {
|
2009-03-01 21:44:42 +01:00
|
|
|
mRunningThreadCount = mResults.GetFileCount();
|
|
|
|
}
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
for (int i = 0; i < mRunningThreadCount; i++) {
|
2009-03-01 21:44:42 +01:00
|
|
|
mThreads[i]->Check(settings);
|
|
|
|
}
|
2010-09-01 08:12:24 +02:00
|
|
|
|
2012-02-14 21:16:11 +01:00
|
|
|
// Date and time when checking starts..
|
|
|
|
mCheckStartTime = QDateTime::currentDateTime();
|
|
|
|
|
2010-09-01 08:12:24 +02:00
|
|
|
mTime.start();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2009-06-04 16:02:35 +02:00
|
|
|
bool ThreadHandler::IsChecking() const
|
|
|
|
{
|
|
|
|
return mRunningThreadCount > 0;
|
|
|
|
}
|
|
|
|
|
2009-03-01 21:44:42 +01:00
|
|
|
void ThreadHandler::SetThreadCount(const int count)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (mRunningThreadCount > 0 ||
|
|
|
|
count == mThreads.size() ||
|
2011-10-13 20:53:06 +02:00
|
|
|
count <= 0) {
|
2009-03-01 21:44:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove unused old threads
|
2009-03-02 20:56:51 +01:00
|
|
|
RemoveThreads();
|
|
|
|
//Create new threads
|
2011-10-13 20:53:06 +02:00
|
|
|
for (int i = mThreads.size(); i < count; i++) {
|
2009-03-02 20:56:51 +01:00
|
|
|
mThreads << new CheckThread(mResults);
|
|
|
|
connect(mThreads.last(), SIGNAL(Done()),
|
|
|
|
this, SLOT(ThreadDone()));
|
|
|
|
connect(mThreads.last(), SIGNAL(FileChecked(const QString &)),
|
|
|
|
&mResults, SLOT(FileChecked(const QString &)));
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
2009-03-02 20:56:51 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ThreadHandler::RemoveThreads()
|
|
|
|
{
|
2011-10-13 20:53:06 +02:00
|
|
|
for (int i = 0; i < mThreads.size(); i++) {
|
2009-03-02 20:56:51 +01:00
|
|
|
mThreads[i]->terminate();
|
|
|
|
disconnect(mThreads.last(), SIGNAL(Done()),
|
|
|
|
this, SLOT(ThreadDone()));
|
|
|
|
disconnect(mThreads.last(), SIGNAL(FileChecked(const QString &)),
|
|
|
|
&mResults, SLOT(FileChecked(const QString &)));
|
|
|
|
|
|
|
|
delete mThreads[i];
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
2009-03-02 20:56:51 +01:00
|
|
|
|
|
|
|
mThreads.clear();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::ThreadDone()
|
|
|
|
{
|
|
|
|
mRunningThreadCount--;
|
2011-10-13 20:53:06 +02:00
|
|
|
if (mRunningThreadCount == 0) {
|
2009-03-01 21:44:42 +01:00
|
|
|
emit Done();
|
2010-09-01 08:12:24 +02:00
|
|
|
|
|
|
|
mScanDuration = mTime.elapsed();
|
2012-02-14 21:16:11 +01:00
|
|
|
|
|
|
|
// Set date/time used by the recheck
|
|
|
|
if (!mCheckStartTime.isNull()) {
|
|
|
|
mLastCheckTime = mCheckStartTime;
|
|
|
|
mCheckStartTime = QDateTime();
|
|
|
|
}
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::Stop()
|
|
|
|
{
|
2012-02-14 21:16:11 +01:00
|
|
|
mCheckStartTime = QDateTime();
|
2011-10-13 20:53:06 +02:00
|
|
|
for (int i = 0; i < mThreads.size(); i++) {
|
2009-06-02 01:01:53 +02:00
|
|
|
mThreads[i]->stop();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::Initialize(ResultsView *view)
|
|
|
|
{
|
2011-07-28 08:30:45 +02:00
|
|
|
connect(&mResults, SIGNAL(Progress(int, const QString&)),
|
|
|
|
view, SLOT(Progress(int, const QString&)));
|
2009-03-01 21:44:42 +01:00
|
|
|
|
2010-07-14 13:24:46 +02:00
|
|
|
connect(&mResults, SIGNAL(Error(const ErrorItem &)),
|
|
|
|
view, SLOT(Error(const ErrorItem &)));
|
2009-03-01 21:44:42 +01:00
|
|
|
|
2010-07-16 16:48:13 +02:00
|
|
|
connect(&mResults, SIGNAL(Log(const QString &)),
|
|
|
|
parent(), SLOT(Log(const QString &)));
|
2010-08-28 19:37:21 +02:00
|
|
|
|
2010-09-01 18:42:28 +02:00
|
|
|
connect(&mResults, SIGNAL(DebugError(const ErrorItem &)),
|
|
|
|
parent(), SLOT(DebugError(const ErrorItem &)));
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::LoadSettings(QSettings &settings)
|
|
|
|
{
|
2009-07-02 10:32:29 +02:00
|
|
|
SetThreadCount(settings.value(SETTINGS_CHECK_THREADS, 1).toInt());
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2012-10-27 12:10:32 +02:00
|
|
|
void ThreadHandler::SaveSettings(QSettings &settings) const
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2009-07-02 10:32:29 +02:00
|
|
|
settings.setValue(SETTINGS_CHECK_THREADS, mThreads.size());
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2009-10-13 10:19:00 +02:00
|
|
|
bool ThreadHandler::HasPreviousFiles() const
|
|
|
|
{
|
2013-01-16 15:37:07 +01:00
|
|
|
return !mLastFiles.isEmpty();
|
2009-10-13 10:19:00 +02:00
|
|
|
}
|
2010-07-07 11:25:47 +02:00
|
|
|
|
|
|
|
int ThreadHandler::GetPreviousFilesCount() const
|
|
|
|
{
|
|
|
|
return mLastFiles.size();
|
|
|
|
}
|
2010-09-01 08:12:24 +02:00
|
|
|
|
|
|
|
int ThreadHandler::GetPreviousScanDuration() const
|
|
|
|
{
|
|
|
|
return mScanDuration;
|
|
|
|
}
|
2012-02-14 21:16:11 +01:00
|
|
|
|
|
|
|
QStringList ThreadHandler::GetReCheckFiles() const
|
|
|
|
{
|
|
|
|
if (mLastCheckTime.isNull())
|
|
|
|
return mLastFiles;
|
|
|
|
|
|
|
|
std::set<QString> modified;
|
|
|
|
std::set<QString> unmodified;
|
|
|
|
|
|
|
|
QStringList files;
|
|
|
|
for (int i = 0; i < mLastFiles.size(); ++i) {
|
|
|
|
if (NeedsReCheck(mLastFiles[i], modified, unmodified))
|
|
|
|
files.push_back(mLastFiles[i]);
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (NeedsReCheck(line, modified, unmodified)) {
|
|
|
|
modified.insert(line);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|