From e629db68186994119559374e1457176594e9ead1 Mon Sep 17 00:00:00 2001 From: Vesa Pikki Date: Mon, 2 Mar 2009 19:56:51 +0000 Subject: [PATCH] Now adds directory contents to list of files to check. Only adds proper (.cpp,.c,.cpp,...) files to the list of files to check. Modified checkthread to clear results after each file. --- gui/checkdialog.cpp | 55 +++++++++++++++++++++- gui/checkdialog.h | 105 ++++++++++++++++++++++-------------------- gui/checkthread.cpp | 5 +- gui/checkthread.h | 2 + gui/resultstree.cpp | 3 ++ gui/resultsview.cpp | 2 +- gui/resultsview.h | 24 +++++----- gui/threadhandler.cpp | 69 ++++++++++++++------------- gui/threadhandler.h | 2 +- gui/threadresult.cpp | 17 +++++-- gui/threadresult.h | 2 + src/filelister.h | 3 +- 12 files changed, 182 insertions(+), 107 deletions(-) diff --git a/gui/checkdialog.cpp b/gui/checkdialog.cpp index 164fbcfc5..565c65cda 100644 --- a/gui/checkdialog.cpp +++ b/gui/checkdialog.cpp @@ -27,6 +27,9 @@ #include #include #include +#include +#include +#include "../src/filelister.h" CheckDialog::CheckDialog(QSettings &programSettings) : mSettings(programSettings) @@ -111,8 +114,54 @@ CheckDialog::~CheckDialog() } +QStringList CheckDialog::RemoveUnacceptedFiles(const QStringList &list) +{ + QStringList result; + QString str; + foreach(str, list) + { + if (FileLister::AcceptFile(str.toStdString())) + { + result << str; + } + } + return result; +} +QStringList CheckDialog::GetFiles(QModelIndex index) +{ + QFileInfo info(mModel.filePath(index)); + QStringList list; + + if (info.isDir()) + { + QDirIterator it(mModel.filePath(index), QDirIterator::Subdirectories); + + while (it.hasNext()) + { + list << it.next(); + } + } + else + { + list << mModel.filePath(index); + } + + return list; +} + +QStringList CheckDialog::RemoveDuplicates(const QStringList &list) +{ + QHash hash; + QString str; + foreach(str, list) + { + hash[str] = 0; + } + + return QStringList(hash.uniqueKeys()); +} QStringList CheckDialog::GetSelectedFiles() { @@ -124,11 +173,13 @@ QStringList CheckDialog::GetSelectedFiles() { if (!mModel.filePath(index).isEmpty()) { - list << mModel.filePath(index); + list << GetFiles(index); } } - return list; + QString str; + + return RemoveUnacceptedFiles(RemoveDuplicates(list)); } diff --git a/gui/checkdialog.h b/gui/checkdialog.h index 10c4847b1..3bf34fa1e 100644 --- a/gui/checkdialog.h +++ b/gui/checkdialog.h @@ -33,9 +33,9 @@ #include #include -/** -* @brief Dialog to select what and how to check -* +/** +* @brief Dialog to select what and how to check +* */ class CheckDialog : public QDialog { @@ -44,38 +44,41 @@ public: CheckDialog(QSettings &programSettings); virtual ~CheckDialog(); - /** - * @brief Get cppcheck settings based on user selections - * - * @return cppcheck settings + /** + * @brief Get cppcheck settings based on user selections + * + * @return cppcheck settings */ Settings GetSettings(); - /** - * @brief Get the root path of current selection - * - * @return default path to use next time + /** + * @brief Get the root path of current selection + * + * @return default path to use next time */ QString GetDefaultPath(); - /** - * @brief Get a list of selected files and directories - * - * @return list of selected files + /** + * @brief Get a list of selected files and directories + * + * @return list of selected files */ QStringList GetSelectedFiles(); - /** - * @brief Save all checkbox values - * + /** + * @brief Save all checkbox values + * */ void SaveCheckboxValues(); protected: + QStringList RemoveUnacceptedFiles(const QStringList &list); + QStringList RemoveDuplicates(const QStringList &list); + QStringList GetFiles(QModelIndex index); - /** - * @brief Load saved values + /** + * @brief Load saved values * Loads dialog size and column widths. - * + * */ void SaveSettings(); @@ -86,58 +89,58 @@ protected: */ void LoadSettings(); - /** - * @brief Save a single checkboxes value - * - * @param box checkbox to save - * @param name name for QSettings to store the value + /** + * @brief Save a single checkboxes value + * + * @param box checkbox to save + * @param name name for QSettings to store the value */ void SaveCheckboxValue(QCheckBox *box, const QString &name); - /** - * @brief Add a new checkbox to layout - * - * @param layout layout to add to + /** + * @brief Add a new checkbox to layout + * + * @param layout layout to add to * @param label label for the checkbox - * @param settings QSettings name for default value - * @return newly created QCheckBox + * @param settings QSettings name for default value + * @return newly created QCheckBox */ QCheckBox* AddCheckbox(QVBoxLayout *layout, const QString &label, const QString &settings, bool value); - /** - * @brief Convert bool to Qt::CheckState - * - * @param yes value to convert - * @return value converted to Qt::CheckState + /** + * @brief Convert bool to Qt::CheckState + * + * @param yes value to convert + * @return value converted to Qt::CheckState */ Qt::CheckState BoolToCheckState(bool yes); - /** - * @brief Converts Qt::CheckState to bool - * - * @param state Qt::CheckState to convert - * @return converted value + /** + * @brief Converts Qt::CheckState to bool + * + * @param state Qt::CheckState to convert + * @return converted value */ bool CheckStateToBool(Qt::CheckState state); - /** - * @brief Item model for mFileTree - * + /** + * @brief Item model for mFileTree + * */ QDirModel mModel; - /** - * @brief Filetree to select files from - * + /** + * @brief Filetree to select files from + * */ QTreeView *mFileTree; - /** - * @brief How many threads should cppcheck have - * + /** + * @brief How many threads should cppcheck have + * */ QLineEdit *mJobs; diff --git a/gui/checkthread.cpp b/gui/checkthread.cpp index 6e4536d59..1a4ecb5db 100644 --- a/gui/checkthread.cpp +++ b/gui/checkthread.cpp @@ -46,9 +46,12 @@ void CheckThread::run() while (!file.isEmpty()) { - qDebug() << tr("Checking file") << file; + qDebug() << "Checking file" << file; mCppCheck.addFile(file.toStdString()); mCppCheck.check(); + mCppCheck.clearFiles(); + emit FileChecked(file); + file = mResult.GetNextFile(); } diff --git a/gui/checkthread.h b/gui/checkthread.h index 0c69ec78a..6e702027a 100644 --- a/gui/checkthread.h +++ b/gui/checkthread.h @@ -57,6 +57,8 @@ signals: * */ void Done(); + + void FileChecked(const QString &file); protected: ThreadResult &mResult; /** diff --git a/gui/resultstree.cpp b/gui/resultstree.cpp index fcc56109f..f0528b204 100644 --- a/gui/resultstree.cpp +++ b/gui/resultstree.cpp @@ -19,6 +19,7 @@ #include "resultstree.h" +#include ResultsTree::ResultsTree(QSettings &settings) : mSettings(settings) @@ -61,6 +62,8 @@ void ResultsTree::AddErrorItem(const QString &file, fileitem = CreateItem(file); } + qDebug() << "Adding error for file" << file << ". Message is" << message; + QList list; list << CreateItem(severity); list << CreateItem(message); diff --git a/gui/resultsview.cpp b/gui/resultsview.cpp index d1582161e..aaea3be37 100644 --- a/gui/resultsview.cpp +++ b/gui/resultsview.cpp @@ -51,7 +51,7 @@ void ResultsView::Clear() void ResultsView::Progress(int value, int max) { - qDebug() << tr("Progress:") << value << tr("/") << max; + qDebug() << "Progress:" << value << "/" << max; mProgress->setMaximum(max); mProgress->setValue(value); } diff --git a/gui/resultsview.h b/gui/resultsview.h index e444a4e71..bd09d6d5f 100644 --- a/gui/resultsview.h +++ b/gui/resultsview.h @@ -27,9 +27,9 @@ #include "../src/errorlogger.h" #include "resultstree.h" -/** -* @brief Widget to show cppcheck progressbar and result -* +/** +* @brief Widget to show cppcheck progressbar and result +* */ class ResultsView : public QWidget { @@ -38,9 +38,9 @@ public: ResultsView(QSettings &settings); virtual ~ResultsView(); - /** - * @brief Clear results - * + /** + * @brief Clear results + * */ void Clear(); public slots: @@ -54,15 +54,15 @@ public slots: const QStringList &files, const QList &lines); protected: - /** - * @brief Tree to show cppcheck's results - * + /** + * @brief Tree to show cppcheck's results + * */ ResultsTree *mTree; - /** - * @brief Progressbar to show cppcheck's progress - * + /** + * @brief Progressbar to show cppcheck's progress + * */ QProgressBar *mProgress; diff --git a/gui/threadhandler.cpp b/gui/threadhandler.cpp index 3a7ed2007..ddb9def37 100644 --- a/gui/threadhandler.cpp +++ b/gui/threadhandler.cpp @@ -21,21 +21,14 @@ #include "threadhandler.h" #include -ThreadHandler::ThreadHandler() : mThreadCount(1), mRunningThreadCount(0) +ThreadHandler::ThreadHandler() : mRunningThreadCount(0) { SetThreadCount(1); } ThreadHandler::~ThreadHandler() { - Stop(); - - for (int i = 0;i < mThreads.size();i++) - { - delete mThreads[i]; - } - - mThreads.clear(); + RemoveThreads(); } void ThreadHandler::ClearFiles() @@ -50,23 +43,23 @@ void ThreadHandler::SetFiles(const QStringList &files) void ThreadHandler::Check(Settings settings) { - if (mResults.GetFileCount() == 0 || mRunningThreadCount > 0) + if (mResults.GetFileCount() == 0 || mRunningThreadCount > 0 || settings._jobs <= 0) { - qDebug() << tr("Can't start checking if there's no files to check or if check is in progress."); + qDebug() << "Can't start checking if there's no files to check or if check is in progress."; return; } SetThreadCount(settings._jobs); - - mRunningThreadCount = mThreadCount; + mRunningThreadCount = mThreads.size(); if (mResults.GetFileCount() < mRunningThreadCount) { mRunningThreadCount = mResults.GetFileCount(); } - qDebug() << tr("Starting") << mRunningThreadCount << tr("threads"); + qDebug() << "Starting" << mRunningThreadCount << "threads"; + qDebug() << mThreads.size(); for (int i = 0;i < mRunningThreadCount;i++) { mThreads[i]->Check(settings); @@ -76,41 +69,51 @@ void ThreadHandler::Check(Settings settings) void ThreadHandler::SetThreadCount(const int count) { if (mRunningThreadCount > 0 || - count == mThreadCount || + count == mThreads.size() || count <= 0) { return; } - qDebug() << tr("Setting thead count to") << count; + qDebug() << "Setting thead count to" << count; - mThreadCount = count; //Remove unused old threads - if (mThreads.size() > count) + RemoveThreads(); + //Create new threads + for (int i = mThreads.size();i < count;i++) { - for (int i = count;i < mThreads.size();i++) - { - disconnect(mThreads.last(), SIGNAL(Done()), - this, SLOT(ThreadDone())); - delete mThreads.takeLast(); - } + mThreads << new CheckThread(mResults); + connect(mThreads.last(), SIGNAL(Done()), + this, SLOT(ThreadDone())); + connect(mThreads.last(), SIGNAL(FileChecked(const QString &)), + &mResults, SLOT(FileChecked(const QString &))); } - else + +} + + +void ThreadHandler::RemoveThreads() +{ + for (int i = 0;i < mThreads.size();i++) { - //Create new threads - for (int i = mThreads.size();i < count;i++) - { - mThreads << new CheckThread(mResults); - connect(mThreads.last(), SIGNAL(Done()), - this, SLOT(ThreadDone())); - } + 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]; } + + mThreads.clear(); } void ThreadHandler::ThreadDone() { mRunningThreadCount--; + qDebug() << "Thread done" << mRunningThreadCount << "threads left"; + if (mRunningThreadCount == 0) { emit Done(); @@ -153,6 +156,6 @@ void ThreadHandler::LoadSettings(QSettings &settings) void ThreadHandler::SaveSettings(QSettings &settings) { - settings.setValue(tr("Check threads"), mThreadCount); + settings.setValue(tr("Check threads"), mThreads.size()); } diff --git a/gui/threadhandler.h b/gui/threadhandler.h index 503f4b474..338048ff5 100644 --- a/gui/threadhandler.h +++ b/gui/threadhandler.h @@ -65,8 +65,8 @@ protected slots: void Stop(); void ThreadDone(); protected: + void RemoveThreads(); ThreadResult mResults; - int mThreadCount; QList mThreads; int mRunningThreadCount; private: diff --git a/gui/threadresult.cpp b/gui/threadresult.cpp index 09700a3e7..0d5ffd017 100644 --- a/gui/threadresult.cpp +++ b/gui/threadresult.cpp @@ -19,6 +19,7 @@ #include "threadresult.h" +#include ThreadResult::ThreadResult() : mMaxProgress(0), mProgress(0) { @@ -32,10 +33,16 @@ ThreadResult::~ThreadResult() void ThreadResult::reportOut(const std::string &outmsg) { - //emit CurrentFile(QString(outmsg.c_str())); Q_UNUSED(outmsg); } +void ThreadResult::FileChecked(const QString &file) +{ + Q_UNUSED(file); //For later use maybe? + mProgress++; + emit Progress(mProgress, mMaxProgress); +} + void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg) { QMutexLocker locker(&mutex); @@ -50,6 +57,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg) files << QString((*tok).file.c_str()); lines << (*tok).line; } + qDebug() << "Got error for file" << QString(callStackToString(msg._callStack).c_str()) << QString(msg._msg.c_str()); emit Error(QString(callStackToString(msg._callStack).c_str()), QString(msg._severity.c_str()), @@ -57,9 +65,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg) files, lines); - mProgress++; - emit Progress(mProgress, mMaxProgress); } @@ -83,8 +89,7 @@ void ThreadResult::reportStatus(unsigned int index, unsigned int max) void ThreadResult::SetFiles(const QStringList &files) { - //TODO we should check which of the strings in files is actually a path - //and add the path's contents + QMutexLocker locker(&mutex); mFiles = files; mProgress = 0; mMaxProgress = files.size(); @@ -92,11 +97,13 @@ void ThreadResult::SetFiles(const QStringList &files) void ThreadResult::ClearFiles() { + QMutexLocker locker(&mutex); mFiles.clear(); } int ThreadResult::GetFileCount() { + QMutexLocker locker(&mutex); return mFiles.size(); } diff --git a/gui/threadresult.h b/gui/threadresult.h index c2b4662e7..0472d7c5d 100644 --- a/gui/threadresult.h +++ b/gui/threadresult.h @@ -47,6 +47,8 @@ public: void reportOut(const std::string &outmsg); void reportErr(const ErrorLogger::ErrorMessage &msg); void reportStatus(unsigned int index, unsigned int max); +public slots: + void FileChecked(const QString &file); signals: void Progress(int value, int max); void Error(const QString &file, diff --git a/src/filelister.h b/src/filelister.h index 4c5f8c03a..2d3f0048d 100644 --- a/src/filelister.h +++ b/src/filelister.h @@ -42,8 +42,9 @@ public: static void RecursiveAddFiles(std::vector &filenames, const std::string &path, bool recursive); static std::string simplifyPath(const char *originalPath); static bool SameFileName(const char fname1[], const char fname2[]); -private: static bool AcceptFile(const std::string &filename); +private: + }; #endif // #ifndef FILELISTER_H