GUI: Add suppressions to project file

This commit is contained in:
Daniel Marjamäki 2013-12-30 22:32:50 +01:00
parent 8da0d84905
commit 24a9da3771
7 changed files with 221 additions and 26 deletions

View File

@ -562,6 +562,11 @@ Settings MainWindow::GetCppcheckSettings()
QMessageBox::information(this, tr("Information"), tr("Failed to load the selected library %1").arg(filename));
}
QStringList suppressions = pfile->GetSuppressions();
foreach(QString suppression, suppressions) {
result.nomsg.addSuppressionLine(suppression.toStdString());
}
// Only check the given -D configuration
if (!defines.isEmpty())
result._maxConfigs = 1;

View File

@ -95,7 +95,10 @@ bool Project::Edit()
dlg.SetPaths(paths);
QStringList ignorepaths = mPFile->GetExcludedPaths();
dlg.SetExcludedPaths(ignorepaths);
dlg.SetLibraries(mPFile->GetLibraries());
QStringList libraries = mPFile->GetLibraries();
dlg.SetLibraries(libraries);
QStringList suppressions = mPFile->GetSuppressions();
dlg.SetSuppressions(suppressions);
int rv = dlg.exec();
if (rv == QDialog::Accepted) {
@ -109,7 +112,10 @@ bool Project::Edit()
mPFile->SetCheckPaths(paths);
QStringList excludedpaths = dlg.GetExcludedPaths();
mPFile->SetExcludedPaths(excludedpaths);
mPFile->SetLibraries(dlg.GetLibraries());
QStringList libraries = dlg.GetLibraries();
mPFile->SetLibraries(libraries);
QStringList suppressions = dlg.GetSuppressions();
mPFile->SetSuppressions(suppressions);
bool writeSuccess = mPFile->Write();
if (!writeSuccess) {

View File

@ -46,6 +46,8 @@ static const char ExcludePathName[] = "path";
static const char ExcludePathNameAttrib[] = "name";
static const char LibrariesElementName[] = "libraries";
static const char LibraryElementName[] = "library";
static const char SuppressionsElementName[] = "suppressions";
static const char SuppressionElementName[] = "suppression";
ProjectFile::ProjectFile(QObject *parent) :
QObject(parent)
@ -102,9 +104,13 @@ bool ProjectFile::Read(const QString &filename)
if (insideProject && xmlReader.name() == IgnoreElementName)
ReadExcludes(xmlReader);
// Find libraries list from insid project element
// Find libraries list from inside project element
if (insideProject && xmlReader.name() == LibrariesElementName)
ReadLibraries(xmlReader);
ReadStringList(mLibraries, xmlReader,LibraryElementName);
// Find suppressions list from inside project element
if (insideProject && xmlReader.name() == SuppressionsElementName)
ReadStringList(mSuppressions, xmlReader,SuppressionElementName);
break;
@ -175,6 +181,15 @@ QStringList ProjectFile::GetLibraries() const
return libraries;
}
QStringList ProjectFile::GetSuppressions() const
{
QStringList suppressions;
foreach(QString suppression, mSuppressions) {
suppressions << suppression;
}
return suppressions;
}
void ProjectFile::ReadRootPath(QXmlStreamReader &reader)
{
QXmlStreamAttributes attribs = reader.attributes();
@ -343,7 +358,7 @@ void ProjectFile::ReadExcludes(QXmlStreamReader &reader)
}
void ProjectFile::ReadLibraries(QXmlStreamReader &reader)
void ProjectFile::ReadStringList(QStringList &stringlist, QXmlStreamReader &reader, const char elementname[])
{
QXmlStreamReader::TokenType type;
bool allRead = false;
@ -352,17 +367,17 @@ void ProjectFile::ReadLibraries(QXmlStreamReader &reader)
switch (type) {
case QXmlStreamReader::StartElement:
// Read library-elements
if (reader.name().toString() == LibraryElementName) {
if (reader.name().toString() == elementname) {
type = reader.readNext();
if (type == QXmlStreamReader::Characters) {
QString library = reader.text().toString();
mLibraries << library;
QString text = reader.text().toString();
stringlist << text;
}
}
break;
case QXmlStreamReader::EndElement:
if (reader.name().toString() == LibrariesElementName)
if (reader.name().toString() != elementname)
allRead = true;
break;
@ -406,6 +421,11 @@ void ProjectFile::SetLibraries(const QStringList &libraries)
mLibraries = libraries;
}
void ProjectFile::SetSuppressions(const QStringList &suppressions)
{
mSuppressions = suppressions;
}
bool ProjectFile::Write(const QString &filename)
{
if (!filename.isEmpty())
@ -467,17 +487,31 @@ bool ProjectFile::Write(const QString &filename)
xmlWriter.writeEndElement();
}
if (!mLibraries.isEmpty()) {
xmlWriter.writeStartElement(LibrariesElementName);
foreach(QString library, mLibraries) {
xmlWriter.writeStartElement(LibraryElementName);
xmlWriter.writeCharacters(library);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}
WriteStringList(xmlWriter,
mLibraries,
LibrariesElementName,
LibraryElementName);
WriteStringList(xmlWriter,
mSuppressions,
SuppressionsElementName,
SuppressionElementName);
xmlWriter.writeEndDocument();
file.close();
return true;
}
void ProjectFile::WriteStringList(QXmlStreamWriter &xmlWriter, const QStringList &stringlist, const char startelementname[], const char stringelementname[])
{
if (stringlist.isEmpty())
return;
xmlWriter.writeStartElement(startelementname);
foreach(QString str, stringlist) {
xmlWriter.writeStartElement(stringelementname);
xmlWriter.writeCharacters(str);
xmlWriter.writeEndElement();
}
xmlWriter.writeEndElement();
}

View File

@ -84,6 +84,12 @@ public:
*/
QStringList GetLibraries() const;
/**
* @brief Get list suppressions.
* @return list of suppressions.
*/
QStringList GetSuppressions() const;
/**
* @brief Get filename for the project file.
* @return file name.
@ -126,16 +132,31 @@ public:
/**
* @brief Set list of libraries.
* @param paths List of libraries.
* @param libraries List of libraries.
*/
void SetLibraries(const QStringList &libraries);
/**
* @brief Set list of suppressions.
* @param suppressions List of suppressions.
*/
void SetSuppressions(const QStringList &suppressions);
/**
* @brief Write project file (to disk).
* @param filename Filename to use.
*/
bool Write(const QString &filename = QString());
/**
* @brief Write string list
* @param xmlWriter xml writer
* @param stringlist string list to write
* @param startelementname name of start element
* @param stringelementname name of each string element
*/
static void WriteStringList(QXmlStreamWriter &xmlWriter, const QStringList &stringlist, const char startelementname[], const char stringelementname[]);
/**
* @brief Set filename for the project file.
* @param filename Filename to use.
@ -177,10 +198,12 @@ protected:
void ReadExcludes(QXmlStreamReader &reader);
/**
* @brief Read list of libraries.
* @param reader XML stream reader.
*/
void ReadLibraries(QXmlStreamReader &reader);
* @brief Read string list
* @param stringlist destination string list
* @param reader XML stream reader
* @param elementname elementname for each string
*/
void ReadStringList(QStringList &stringlist, QXmlStreamReader &reader, const char elementname[]);
private:
@ -221,6 +244,11 @@ private:
* @brief List of libraries.
*/
QStringList mLibraries;
/**
* @brief List of suppressions.
*/
QStringList mSuppressions;
};
/// @}
#endif // PROJECT_FILE_H

View File

@ -276,6 +276,54 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_4">
<attribute name="title">
<string>Suppressions</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>Suppression list:</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="mListSuppressions"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="mBtnAddSuppression">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="mBtnRemoveSuppression">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
</item>
<item>

View File

@ -22,11 +22,14 @@
#include <QStringList>
#include <QFileInfo>
#include <QFileDialog>
#include <QInputDialog>
#include <QDir>
#include <QSettings>
#include "common.h"
#include "projectfiledialog.h"
#include "library.h"
#include "cppcheck.h"
#include "errorlogger.h"
ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent)
: QDialog(parent)
@ -83,6 +86,8 @@ ProjectFileDialog::ProjectFileDialog(const QString &path, QWidget *parent)
connect(mUI.mBtnRemoveIgnorePath, SIGNAL(clicked()), this, SLOT(RemoveExcludePath()));
connect(mUI.mBtnIncludeUp, SIGNAL(clicked()), this, SLOT(MoveIncludePathUp()));
connect(mUI.mBtnIncludeDown, SIGNAL(clicked()), this, SLOT(MoveIncludePathDown()));
connect(mUI.mBtnAddSuppression, SIGNAL(clicked()), this, SLOT(AddSuppression()));
connect(mUI.mBtnRemoveSuppression, SIGNAL(clicked()), this, SLOT(RemoveSuppression()));
}
ProjectFileDialog::~ProjectFileDialog()
@ -195,13 +200,24 @@ QStringList ProjectFileDialog::GetExcludedPaths() const
QStringList ProjectFileDialog::GetLibraries() const
{
QStringList libraries;
for (int i = 0; i < mLibraryCheckboxes.size(); i++) {
if (mLibraryCheckboxes[i]->isChecked())
libraries << mLibraryCheckboxes[i]->text();
foreach(const QCheckBox *checkbox, mLibraryCheckboxes) {
if (checkbox->isChecked())
libraries << checkbox->text();
}
return libraries;
}
QStringList ProjectFileDialog::GetSuppressions() const
{
QStringList suppressions;
const int count = mUI.mListSuppressions->count();
for (int i = 0; i < count; i++) {
QListWidgetItem *item = mUI.mListSuppressions->item(i);
suppressions << item->text();
}
return suppressions;
}
void ProjectFileDialog::SetRootPath(const QString &root)
{
QString newroot = QDir::toNativeSeparators(root);
@ -251,6 +267,12 @@ void ProjectFileDialog::SetLibraries(const QStringList &libraries)
}
}
void ProjectFileDialog::SetSuppressions(const QStringList &suppressions)
{
mUI.mListSuppressions->clear();
mUI.mListSuppressions->addItems(suppressions);
}
void ProjectFileDialog::AddIncludeDir()
{
const QFileInfo inf(mFilePath);
@ -360,3 +382,33 @@ void ProjectFileDialog::MoveIncludePathDown()
mUI.mListIncludeDirs->insertItem(row, item);
mUI.mListIncludeDirs->setCurrentItem(item);
}
void ProjectFileDialog::AddSuppression()
{
class QErrorLogger : public ErrorLogger {
public:
virtual void reportOut(const std::string &outmsg) {}
virtual void reportErr(const ErrorLogger::ErrorMessage &msg) {
errorIds << QString::fromStdString(msg._id);
}
QStringList errorIds;
};
QErrorLogger errorLogger;
CppCheck cppcheck(errorLogger,false);
cppcheck.getErrorMessages();
errorLogger.errorIds.sort();
bool ok;
QString item = QInputDialog::getItem(this, tr("Add Suppression"),
tr("Select error id suppress:"), errorLogger.errorIds, 0, false, &ok);
if (ok && !item.isEmpty())
mUI.mListSuppressions->addItem(item);
}
void ProjectFileDialog::RemoveSuppression()
{
const int row = mUI.mListSuppressions->currentRow();
QListWidgetItem *item = mUI.mListSuppressions->takeItem(row);
delete item;
}

View File

@ -77,6 +77,12 @@ public:
*/
QStringList GetLibraries() const;
/**
* @brief Return suppressions from the dialog control.
* @return List of suppressions.
*/
QStringList GetSuppressions() const;
/**
* @brief Set project root path to dialog control.
* @param root Project root path to set to dialog control.
@ -113,6 +119,12 @@ public:
*/
void SetLibraries(const QStringList &libraries);
/**
* @brief Set suppressions to dialog control.
* @param paths List of suppressions to set to dialog control.
*/
void SetSuppressions(const QStringList &suppressions);
protected slots:
/**
* @brief Browse for include directory.
@ -170,6 +182,16 @@ protected slots:
*/
void MoveIncludePathDown();
/**
* @brief Add suppression to the list
*/
void AddSuppression();
/**
* @brief Remove selected suppression from the list
*/
void RemoveSuppression();
protected:
/**