GUI: Add support for project files.

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
This commit is contained in:
Kimmo Varis 2009-06-22 11:57:17 +03:00
parent a0ba52ccf1
commit 1aafeebe7b
9 changed files with 255 additions and 2 deletions

11
gui/gui.cppcheck Normal file
View File

@ -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>

View File

@ -4,6 +4,7 @@
TEMPLATE = app
TARGET =
QT += xml
DEPENDPATH += .
INCLUDEPATH += .
MOC_DIR = temp
@ -27,6 +28,7 @@ HEADERS += mainwindow.h \
aboutdialog.h \
common.h \
fileviewdialog.h \
projectfile.h \
../src/checkautovariables.h \
../src/checkdangerousfunctions.h \
../src/checkheaders.h \
@ -62,6 +64,7 @@ SOURCES += main.cpp \
applicationdialog.cpp \
aboutdialog.cpp \
fileviewdialog.cpp \
projectfile.cpp \
../src/checkautovariables.cpp \
../src/checkdangerousfunctions.cpp \
../src/checkmemoryleak.cpp \

View File

@ -30,6 +30,7 @@
#include "aboutdialog.h"
#include "threadhandler.h"
#include "fileviewdialog.h"
#include "projectfile.h"
#include "../src/filelister.h"
#include "../src/cppcheckexecutor.h"
@ -230,6 +231,8 @@ 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)
{
@ -237,7 +240,10 @@ void MainWindow::DoCheckFiles(QFileDialog::FileMode mode)
tr("Select directory to check"),
mSettings.value(tr("Check path"), "").toString());
if (!dir.isEmpty())
{
mCurrentDirectory = dir;
selected.append(dir);
}
}
if (selected.count() > 0)
@ -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);
}
}
@ -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;

View File

@ -197,7 +197,7 @@ protected:
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
*/
@ -368,6 +368,11 @@ protected:
ApplicationList mApplications;
private:
/**
* @brief Current checked directory.
*/
QString mCurrentDirectory;
};
#endif // MAINWINDOW_H

106
gui/projectfile.cpp Normal file
View File

@ -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
gui/projectfile.h Normal file
View File

@ -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
gui/projectfile.txt Normal file
View File

@ -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.

View File

@ -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
{

View File

@ -67,6 +67,9 @@ public:
/** 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;
};