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.
This commit is contained in:
Vesa Pikki 2009-03-02 19:56:51 +00:00
parent 20390ce655
commit e629db6818
12 changed files with 182 additions and 107 deletions

View File

@ -27,6 +27,9 @@
#include <QFileDialog> #include <QFileDialog>
#include <QDirIterator> #include <QDirIterator>
#include <QDebug> #include <QDebug>
#include <QFileInfo>
#include <QDirIterator>
#include "../src/filelister.h"
CheckDialog::CheckDialog(QSettings &programSettings) : CheckDialog::CheckDialog(QSettings &programSettings) :
mSettings(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<QString, int> hash;
QString str;
foreach(str, list)
{
hash[str] = 0;
}
return QStringList(hash.uniqueKeys());
}
QStringList CheckDialog::GetSelectedFiles() QStringList CheckDialog::GetSelectedFiles()
{ {
@ -124,11 +173,13 @@ QStringList CheckDialog::GetSelectedFiles()
{ {
if (!mModel.filePath(index).isEmpty()) if (!mModel.filePath(index).isEmpty())
{ {
list << mModel.filePath(index); list << GetFiles(index);
} }
} }
return list; QString str;
return RemoveUnacceptedFiles(RemoveDuplicates(list));
} }

View File

@ -33,9 +33,9 @@
#include <QDirModel> #include <QDirModel>
#include <QStandardItem> #include <QStandardItem>
/** /**
* @brief Dialog to select what and how to check * @brief Dialog to select what and how to check
* *
*/ */
class CheckDialog : public QDialog class CheckDialog : public QDialog
{ {
@ -44,38 +44,41 @@ public:
CheckDialog(QSettings &programSettings); CheckDialog(QSettings &programSettings);
virtual ~CheckDialog(); virtual ~CheckDialog();
/** /**
* @brief Get cppcheck settings based on user selections * @brief Get cppcheck settings based on user selections
* *
* @return cppcheck settings * @return cppcheck settings
*/ */
Settings GetSettings(); Settings GetSettings();
/** /**
* @brief Get the root path of current selection * @brief Get the root path of current selection
* *
* @return default path to use next time * @return default path to use next time
*/ */
QString GetDefaultPath(); QString GetDefaultPath();
/** /**
* @brief Get a list of selected files and directories * @brief Get a list of selected files and directories
* *
* @return list of selected files * @return list of selected files
*/ */
QStringList GetSelectedFiles(); QStringList GetSelectedFiles();
/** /**
* @brief Save all checkbox values * @brief Save all checkbox values
* *
*/ */
void SaveCheckboxValues(); void SaveCheckboxValues();
protected: 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. * Loads dialog size and column widths.
* *
*/ */
void SaveSettings(); void SaveSettings();
@ -86,58 +89,58 @@ protected:
*/ */
void LoadSettings(); void LoadSettings();
/** /**
* @brief Save a single checkboxes value * @brief Save a single checkboxes value
* *
* @param box checkbox to save * @param box checkbox to save
* @param name name for QSettings to store the value * @param name name for QSettings to store the value
*/ */
void SaveCheckboxValue(QCheckBox *box, const QString &name); void SaveCheckboxValue(QCheckBox *box, const QString &name);
/** /**
* @brief Add a new checkbox to layout * @brief Add a new checkbox to layout
* *
* @param layout layout to add to * @param layout layout to add to
* @param label label for the checkbox * @param label label for the checkbox
* @param settings QSettings name for default value * @param settings QSettings name for default value
* @return newly created QCheckBox * @return newly created QCheckBox
*/ */
QCheckBox* AddCheckbox(QVBoxLayout *layout, QCheckBox* AddCheckbox(QVBoxLayout *layout,
const QString &label, const QString &label,
const QString &settings, const QString &settings,
bool value); bool value);
/** /**
* @brief Convert bool to Qt::CheckState * @brief Convert bool to Qt::CheckState
* *
* @param yes value to convert * @param yes value to convert
* @return value converted to Qt::CheckState * @return value converted to Qt::CheckState
*/ */
Qt::CheckState BoolToCheckState(bool yes); Qt::CheckState BoolToCheckState(bool yes);
/** /**
* @brief Converts Qt::CheckState to bool * @brief Converts Qt::CheckState to bool
* *
* @param state Qt::CheckState to convert * @param state Qt::CheckState to convert
* @return converted value * @return converted value
*/ */
bool CheckStateToBool(Qt::CheckState state); bool CheckStateToBool(Qt::CheckState state);
/** /**
* @brief Item model for mFileTree * @brief Item model for mFileTree
* *
*/ */
QDirModel mModel; QDirModel mModel;
/** /**
* @brief Filetree to select files from * @brief Filetree to select files from
* *
*/ */
QTreeView *mFileTree; QTreeView *mFileTree;
/** /**
* @brief How many threads should cppcheck have * @brief How many threads should cppcheck have
* *
*/ */
QLineEdit *mJobs; QLineEdit *mJobs;

View File

@ -46,9 +46,12 @@ void CheckThread::run()
while (!file.isEmpty()) while (!file.isEmpty())
{ {
qDebug() << tr("Checking file") << file; qDebug() << "Checking file" << file;
mCppCheck.addFile(file.toStdString()); mCppCheck.addFile(file.toStdString());
mCppCheck.check(); mCppCheck.check();
mCppCheck.clearFiles();
emit FileChecked(file);
file = mResult.GetNextFile(); file = mResult.GetNextFile();
} }

View File

@ -57,6 +57,8 @@ signals:
* *
*/ */
void Done(); void Done();
void FileChecked(const QString &file);
protected: protected:
ThreadResult &mResult; ThreadResult &mResult;
/** /**

View File

@ -19,6 +19,7 @@
#include "resultstree.h" #include "resultstree.h"
#include <QDebug>
ResultsTree::ResultsTree(QSettings &settings) : ResultsTree::ResultsTree(QSettings &settings) :
mSettings(settings) mSettings(settings)
@ -61,6 +62,8 @@ void ResultsTree::AddErrorItem(const QString &file,
fileitem = CreateItem(file); fileitem = CreateItem(file);
} }
qDebug() << "Adding error for file" << file << ". Message is" << message;
QList<QStandardItem*> list; QList<QStandardItem*> list;
list << CreateItem(severity); list << CreateItem(severity);
list << CreateItem(message); list << CreateItem(message);

View File

@ -51,7 +51,7 @@ void ResultsView::Clear()
void ResultsView::Progress(int value, int max) void ResultsView::Progress(int value, int max)
{ {
qDebug() << tr("Progress:") << value << tr("/") << max; qDebug() << "Progress:" << value << "/" << max;
mProgress->setMaximum(max); mProgress->setMaximum(max);
mProgress->setValue(value); mProgress->setValue(value);
} }

View File

@ -27,9 +27,9 @@
#include "../src/errorlogger.h" #include "../src/errorlogger.h"
#include "resultstree.h" #include "resultstree.h"
/** /**
* @brief Widget to show cppcheck progressbar and result * @brief Widget to show cppcheck progressbar and result
* *
*/ */
class ResultsView : public QWidget class ResultsView : public QWidget
{ {
@ -38,9 +38,9 @@ public:
ResultsView(QSettings &settings); ResultsView(QSettings &settings);
virtual ~ResultsView(); virtual ~ResultsView();
/** /**
* @brief Clear results * @brief Clear results
* *
*/ */
void Clear(); void Clear();
public slots: public slots:
@ -54,15 +54,15 @@ public slots:
const QStringList &files, const QStringList &files,
const QList<int> &lines); const QList<int> &lines);
protected: protected:
/** /**
* @brief Tree to show cppcheck's results * @brief Tree to show cppcheck's results
* *
*/ */
ResultsTree *mTree; ResultsTree *mTree;
/** /**
* @brief Progressbar to show cppcheck's progress * @brief Progressbar to show cppcheck's progress
* *
*/ */
QProgressBar *mProgress; QProgressBar *mProgress;

View File

@ -21,21 +21,14 @@
#include "threadhandler.h" #include "threadhandler.h"
#include <QDebug> #include <QDebug>
ThreadHandler::ThreadHandler() : mThreadCount(1), mRunningThreadCount(0) ThreadHandler::ThreadHandler() : mRunningThreadCount(0)
{ {
SetThreadCount(1); SetThreadCount(1);
} }
ThreadHandler::~ThreadHandler() ThreadHandler::~ThreadHandler()
{ {
Stop(); RemoveThreads();
for (int i = 0;i < mThreads.size();i++)
{
delete mThreads[i];
}
mThreads.clear();
} }
void ThreadHandler::ClearFiles() void ThreadHandler::ClearFiles()
@ -50,23 +43,23 @@ void ThreadHandler::SetFiles(const QStringList &files)
void ThreadHandler::Check(Settings settings) 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; return;
} }
SetThreadCount(settings._jobs); SetThreadCount(settings._jobs);
mRunningThreadCount = mThreads.size();
mRunningThreadCount = mThreadCount;
if (mResults.GetFileCount() < mRunningThreadCount) if (mResults.GetFileCount() < mRunningThreadCount)
{ {
mRunningThreadCount = mResults.GetFileCount(); mRunningThreadCount = mResults.GetFileCount();
} }
qDebug() << tr("Starting") << mRunningThreadCount << tr("threads"); qDebug() << "Starting" << mRunningThreadCount << "threads";
qDebug() << mThreads.size();
for (int i = 0;i < mRunningThreadCount;i++) for (int i = 0;i < mRunningThreadCount;i++)
{ {
mThreads[i]->Check(settings); mThreads[i]->Check(settings);
@ -76,41 +69,51 @@ void ThreadHandler::Check(Settings settings)
void ThreadHandler::SetThreadCount(const int count) void ThreadHandler::SetThreadCount(const int count)
{ {
if (mRunningThreadCount > 0 || if (mRunningThreadCount > 0 ||
count == mThreadCount || count == mThreads.size() ||
count <= 0) count <= 0)
{ {
return; return;
} }
qDebug() << tr("Setting thead count to") << count; qDebug() << "Setting thead count to" << count;
mThreadCount = count;
//Remove unused old threads //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++) mThreads << new CheckThread(mResults);
{ connect(mThreads.last(), SIGNAL(Done()),
disconnect(mThreads.last(), SIGNAL(Done()), this, SLOT(ThreadDone()));
this, SLOT(ThreadDone())); connect(mThreads.last(), SIGNAL(FileChecked(const QString &)),
delete mThreads.takeLast(); &mResults, SLOT(FileChecked(const QString &)));
}
} }
else
}
void ThreadHandler::RemoveThreads()
{
for (int i = 0;i < mThreads.size();i++)
{ {
//Create new threads mThreads[i]->terminate();
for (int i = mThreads.size();i < count;i++) disconnect(mThreads.last(), SIGNAL(Done()),
{ this, SLOT(ThreadDone()));
mThreads << new CheckThread(mResults); disconnect(mThreads.last(), SIGNAL(FileChecked(const QString &)),
connect(mThreads.last(), SIGNAL(Done()), &mResults, SLOT(FileChecked(const QString &)));
this, SLOT(ThreadDone()));
} delete mThreads[i];
} }
mThreads.clear();
} }
void ThreadHandler::ThreadDone() void ThreadHandler::ThreadDone()
{ {
mRunningThreadCount--; mRunningThreadCount--;
qDebug() << "Thread done" << mRunningThreadCount << "threads left";
if (mRunningThreadCount == 0) if (mRunningThreadCount == 0)
{ {
emit Done(); emit Done();
@ -153,6 +156,6 @@ void ThreadHandler::LoadSettings(QSettings &settings)
void ThreadHandler::SaveSettings(QSettings &settings) void ThreadHandler::SaveSettings(QSettings &settings)
{ {
settings.setValue(tr("Check threads"), mThreadCount); settings.setValue(tr("Check threads"), mThreads.size());
} }

View File

@ -65,8 +65,8 @@ protected slots:
void Stop(); void Stop();
void ThreadDone(); void ThreadDone();
protected: protected:
void RemoveThreads();
ThreadResult mResults; ThreadResult mResults;
int mThreadCount;
QList<CheckThread *> mThreads; QList<CheckThread *> mThreads;
int mRunningThreadCount; int mRunningThreadCount;
private: private:

View File

@ -19,6 +19,7 @@
#include "threadresult.h" #include "threadresult.h"
#include <QDebug>
ThreadResult::ThreadResult() : mMaxProgress(0), mProgress(0) ThreadResult::ThreadResult() : mMaxProgress(0), mProgress(0)
{ {
@ -32,10 +33,16 @@ ThreadResult::~ThreadResult()
void ThreadResult::reportOut(const std::string &outmsg) void ThreadResult::reportOut(const std::string &outmsg)
{ {
//emit CurrentFile(QString(outmsg.c_str()));
Q_UNUSED(outmsg); 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) void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
{ {
QMutexLocker locker(&mutex); QMutexLocker locker(&mutex);
@ -50,6 +57,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
files << QString((*tok).file.c_str()); files << QString((*tok).file.c_str());
lines << (*tok).line; 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()), emit Error(QString(callStackToString(msg._callStack).c_str()),
QString(msg._severity.c_str()), QString(msg._severity.c_str()),
@ -57,9 +65,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
files, files,
lines); 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) void ThreadResult::SetFiles(const QStringList &files)
{ {
//TODO we should check which of the strings in files is actually a path QMutexLocker locker(&mutex);
//and add the path's contents
mFiles = files; mFiles = files;
mProgress = 0; mProgress = 0;
mMaxProgress = files.size(); mMaxProgress = files.size();
@ -92,11 +97,13 @@ void ThreadResult::SetFiles(const QStringList &files)
void ThreadResult::ClearFiles() void ThreadResult::ClearFiles()
{ {
QMutexLocker locker(&mutex);
mFiles.clear(); mFiles.clear();
} }
int ThreadResult::GetFileCount() int ThreadResult::GetFileCount()
{ {
QMutexLocker locker(&mutex);
return mFiles.size(); return mFiles.size();
} }

View File

@ -47,6 +47,8 @@ public:
void reportOut(const std::string &outmsg); void reportOut(const std::string &outmsg);
void reportErr(const ErrorLogger::ErrorMessage &msg); void reportErr(const ErrorLogger::ErrorMessage &msg);
void reportStatus(unsigned int index, unsigned int max); void reportStatus(unsigned int index, unsigned int max);
public slots:
void FileChecked(const QString &file);
signals: signals:
void Progress(int value, int max); void Progress(int value, int max);
void Error(const QString &file, void Error(const QString &file,

View File

@ -42,8 +42,9 @@ public:
static void RecursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive); static void RecursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive);
static std::string simplifyPath(const char *originalPath); static std::string simplifyPath(const char *originalPath);
static bool SameFileName(const char fname1[], const char fname2[]); static bool SameFileName(const char fname1[], const char fname2[]);
private:
static bool AcceptFile(const std::string &filename); static bool AcceptFile(const std::string &filename);
private:
}; };
#endif // #ifndef FILELISTER_H #endif // #ifndef FILELISTER_H