Skip to content

Commit

Permalink
GUI: Add support for project files.
Browse files Browse the repository at this point in the history
GUI reads per-project settings (automatically deallocated classes) from XML project file. The project file format is described in projectfile.txt. Example project file is added for gui.

See also forum discussion at:
https://sourceforge.net/apps/phpbb/cppcheck/viewtopic.php?f=3&t=46
  • Loading branch information
kimmov committed Jun 22, 2009
1 parent a0ba52c commit 1aafeeb
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 2 deletions.
11 changes: 11 additions & 0 deletions gui/gui.cppcheck
@@ -0,0 +1,11 @@
<?xml version="1.0"?>

<!-- cppcheck project file -->

<project version="1">
<autodealloc>
<class name="AboutDialog"/>
<class name="FileViewDialog"/>
<class name="ThreadHandler"/>
</autodealloc>
</project>
3 changes: 3 additions & 0 deletions gui/gui.pro
Expand Up @@ -4,6 +4,7 @@

TEMPLATE = app
TARGET =
QT += xml
DEPENDPATH += .
INCLUDEPATH += .
MOC_DIR = temp
Expand All @@ -27,6 +28,7 @@ HEADERS += mainwindow.h \
aboutdialog.h \
common.h \
fileviewdialog.h \
projectfile.h \
../src/checkautovariables.h \
../src/checkdangerousfunctions.h \
../src/checkheaders.h \
Expand Down Expand Up @@ -62,6 +64,7 @@ SOURCES += main.cpp \
applicationdialog.cpp \
aboutdialog.cpp \
fileviewdialog.cpp \
projectfile.cpp \
../src/checkautovariables.cpp \
../src/checkdangerousfunctions.cpp \
../src/checkmemoryleak.cpp \
Expand Down
33 changes: 32 additions & 1 deletion gui/mainwindow.cpp
Expand Up @@ -30,6 +30,7 @@
#include "aboutdialog.h"
#include "threadhandler.h"
#include "fileviewdialog.h"
#include "projectfile.h"
#include "../src/filelister.h"
#include "../src/cppcheckexecutor.h"

Expand Down Expand Up @@ -230,14 +231,19 @@ void MainWindow::DoCheckFiles(QFileDialog::FileMode mode)
selected = QFileDialog::getOpenFileNames(this,
tr("Select files to check"),
mSettings.value(tr("Check path"), "").toString());
if (selected.isEmpty())
mCurrentDirectory.clear();
}
else if (mode == QFileDialog::DirectoryOnly)
{
QString dir = QFileDialog::getExistingDirectory(this,
tr("Select directory to check"),
mSettings.value(tr("Check path"), "").toString());
if (!dir.isEmpty())
{
mCurrentDirectory = dir;
selected.append(dir);
}
}

if (selected.count() > 0)
Expand Down Expand Up @@ -275,7 +281,9 @@ void MainWindow::DoCheckFiles(QFileDialog::FileMode mode)
EnableCheckButtons(false);
mActionSettings.setEnabled(false);
mResults.SetCheckDirectory(absDirectory);
mThread->Check(GetCppcheckSettings(), false);

Settings checkSettings = GetCppcheckSettings();
mThread->Check(checkSettings, false);
}
}

Expand All @@ -291,7 +299,30 @@ void MainWindow::CheckDirectory()

Settings MainWindow::GetCppcheckSettings()
{
ProjectFile pfile;
Settings result;

if (!mCurrentDirectory.isEmpty())
{
// Format project filename (directory name + .cppcheck) and load
// the project file if it is found.
QStringList parts = mCurrentDirectory.split("/");
QString projfile = parts[parts.count() - 1] + ".cppcheck";
bool projectRead = false;
if (QFile::exists(projfile))
projectRead = pfile.Read(mCurrentDirectory + "/" + projfile);

if (projectRead)
{
QStringList classes = pfile.GetDeAllocatedClasses();
QString classname;
foreach(classname, classes)
{
result.addAutoAllocClass(classname.toStdString());
}
}
}

result._debug = false;
result._showAll = true;
result._checkCodingStyle = true;
Expand Down
7 changes: 6 additions & 1 deletion gui/mainwindow.h
Expand Up @@ -197,7 +197,7 @@ protected slots:
QStringList GetFilesRecursively(const QString &path);

/**
* @brief Get our default cppcheck settings
* @brief Get our default cppcheck settings and read project file.
*
* @return Default cppcheck settings
*/
Expand Down Expand Up @@ -368,6 +368,11 @@ protected slots:
ApplicationList mApplications;

private:

/**
* @brief Current checked directory.
*/
QString mCurrentDirectory;
};

#endif // MAINWINDOW_H
106 changes: 106 additions & 0 deletions gui/projectfile.cpp
@@ -0,0 +1,106 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2009 Daniel Marjamäki and 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/
*/

#include <QObject>
#include <QString>
#include <QXmlStreamReader>
#include <QFile>
#include "projectfile.h"

static const char ProjectElementName[] = "project";
static const char AllocElementName[] = "autodealloc";
static const char ClassElementName[] = "class";
static const char ClassNameAttrib[] = "name";

ProjectFile::ProjectFile(QObject *parent) :
QObject(parent)
{
}

ProjectFile::ProjectFile(const QString &filename, QObject *parent) :
QObject(parent),
mFilename(filename)
{
}

bool ProjectFile::Read(const QString &filename)
{
if (!filename.isEmpty())
mFilename = filename;

QFile file(mFilename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;

QXmlStreamReader xmlReader(&file);
bool insideProject = false;
while (!xmlReader.atEnd())
{
switch (xmlReader.readNext())
{
case QXmlStreamReader::StartElement:
if (xmlReader.name() == ProjectElementName)
insideProject = true;
if (insideProject && xmlReader.name() == AllocElementName)
ReadAutoAllocClasses(xmlReader);
break;

case QXmlStreamReader::EndElement:
if (xmlReader.name() == ProjectElementName)
insideProject = false;
break;
}
}

file.close();
return true;
}

QStringList ProjectFile::GetDeAllocatedClasses() const
{
return mDeAllocatedClasses;
}

void ProjectFile::ReadAutoAllocClasses(QXmlStreamReader &reader)
{
QXmlStreamReader::TokenType type;
bool allRead = false;
do
{
type = reader.readNext();
switch (type)
{
case QXmlStreamReader::StartElement:
if (reader.name().toString() == ClassElementName)
{
QXmlStreamAttributes attribs = reader.attributes();
QString name = attribs.value("", ClassNameAttrib).toString();
if (!name.isEmpty())
mDeAllocatedClasses << name;
}
break;

case QXmlStreamReader::EndElement:
if (reader.name().toString() == AllocElementName)
allRead = true;
break;
}

}
while (!allRead);
}
63 changes: 63 additions & 0 deletions gui/projectfile.h
@@ -0,0 +1,63 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2009 Daniel Marjamäki and 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/
*/

#include <QObject>
#include <QString>
#include <QStringList>
#include <QXmlStreamReader>

/**
* @brief A class that reads and writes (TODO) project files.
* The project files contain project-specific settings for checking. For
* example a list of automatically deallocated classes.
*/
class ProjectFile : public QObject
{
Q_OBJECT

public:
ProjectFile(QObject *parent = 0);
ProjectFile(const QString &filename, QObject *parent = 0);

/**
* @brief Read the project file.
* @param filename Filename (can be also given to constructor).
*/
bool Read(const QString &filename = QString());

/**
* @brief Get list of automatically deallocated classes.
* @return list of classes.
*/
QStringList GetDeAllocatedClasses() const;

protected:
void ReadAutoAllocClasses(QXmlStreamReader &reader);

private:

/**
* @brief Filename (+path) of the project file.
*/
QString mFilename;

/**
* @brief List of automatically deallocated classes.
*/
QStringList mDeAllocatedClasses;
};
27 changes: 27 additions & 0 deletions gui/projectfile.txt
@@ -0,0 +1,27 @@
Project files
-------------

cppcheck GUI handles per-project settings in project files instead of global
program settings. This allows customizing cppcheck for each project's needs.

Currently there is no GUI to edit the project file. The cppcheck automatically
loads a project file which has same name than selected directory and extension
".cppcheck". For example in "gui"-directory cppcheck loads "gui.cppcheck"
project file.

The project file is simple XML file easy to edit with your favourite editor
program. The format is:

<?xml version="1.0"?>
<project version="1">
<autodealloc>
<class name="MyClass"/>
<class name="MyClass2"/>
</autodealloc>
<includedir>
<dir name="c:/projects/framework/" />
<dir name="c:/Program Files/Visual Studio 8/VC/include/" />
</includedir>
</project>

See also gui.cppcheck file in gui-directory of cppcheck sources.
4 changes: 4 additions & 0 deletions src/settings.cpp
Expand Up @@ -55,6 +55,10 @@ void Settings::autoDealloc(std::istream &istr)
}
}

void Settings::addAutoAllocClass(const std::string &name)
{
_autoDealloc.push_back(name);
}

bool Settings::isAutoDealloc(const char classname[]) const
{
Expand Down
3 changes: 3 additions & 0 deletions src/settings.h
Expand Up @@ -67,6 +67,9 @@ class Settings
/** Fill list of automaticly deallocated classes */
void autoDealloc(std::istream &istr);

/** Add class to list of automatically deallocated classes */
void addAutoAllocClass(const std::string &name);

/** is a class automaticly deallocated? */
bool isAutoDealloc(const char classname[]) const;
};
Expand Down

0 comments on commit 1aafeeb

Please sign in to comment.