Skip to content

Commit 1aafeeb

Browse files
committed
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
1 parent a0ba52c commit 1aafeeb

File tree

9 files changed

+255
-2
lines changed

9 files changed

+255
-2
lines changed

gui/gui.cppcheck

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
3+
<!-- cppcheck project file -->
4+
5+
<project version="1">
6+
<autodealloc>
7+
<class name="AboutDialog"/>
8+
<class name="FileViewDialog"/>
9+
<class name="ThreadHandler"/>
10+
</autodealloc>
11+
</project>

gui/gui.pro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
TEMPLATE = app
66
TARGET =
7+
QT += xml
78
DEPENDPATH += .
89
INCLUDEPATH += .
910
MOC_DIR = temp
@@ -27,6 +28,7 @@ HEADERS += mainwindow.h \
2728
aboutdialog.h \
2829
common.h \
2930
fileviewdialog.h \
31+
projectfile.h \
3032
../src/checkautovariables.h \
3133
../src/checkdangerousfunctions.h \
3234
../src/checkheaders.h \
@@ -62,6 +64,7 @@ SOURCES += main.cpp \
6264
applicationdialog.cpp \
6365
aboutdialog.cpp \
6466
fileviewdialog.cpp \
67+
projectfile.cpp \
6568
../src/checkautovariables.cpp \
6669
../src/checkdangerousfunctions.cpp \
6770
../src/checkmemoryleak.cpp \

gui/mainwindow.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "aboutdialog.h"
3131
#include "threadhandler.h"
3232
#include "fileviewdialog.h"
33+
#include "projectfile.h"
3334
#include "../src/filelister.h"
3435
#include "../src/cppcheckexecutor.h"
3536

@@ -230,14 +231,19 @@ void MainWindow::DoCheckFiles(QFileDialog::FileMode mode)
230231
selected = QFileDialog::getOpenFileNames(this,
231232
tr("Select files to check"),
232233
mSettings.value(tr("Check path"), "").toString());
234+
if (selected.isEmpty())
235+
mCurrentDirectory.clear();
233236
}
234237
else if (mode == QFileDialog::DirectoryOnly)
235238
{
236239
QString dir = QFileDialog::getExistingDirectory(this,
237240
tr("Select directory to check"),
238241
mSettings.value(tr("Check path"), "").toString());
239242
if (!dir.isEmpty())
243+
{
244+
mCurrentDirectory = dir;
240245
selected.append(dir);
246+
}
241247
}
242248

243249
if (selected.count() > 0)
@@ -275,7 +281,9 @@ void MainWindow::DoCheckFiles(QFileDialog::FileMode mode)
275281
EnableCheckButtons(false);
276282
mActionSettings.setEnabled(false);
277283
mResults.SetCheckDirectory(absDirectory);
278-
mThread->Check(GetCppcheckSettings(), false);
284+
285+
Settings checkSettings = GetCppcheckSettings();
286+
mThread->Check(checkSettings, false);
279287
}
280288
}
281289

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

292300
Settings MainWindow::GetCppcheckSettings()
293301
{
302+
ProjectFile pfile;
294303
Settings result;
304+
305+
if (!mCurrentDirectory.isEmpty())
306+
{
307+
// Format project filename (directory name + .cppcheck) and load
308+
// the project file if it is found.
309+
QStringList parts = mCurrentDirectory.split("/");
310+
QString projfile = parts[parts.count() - 1] + ".cppcheck";
311+
bool projectRead = false;
312+
if (QFile::exists(projfile))
313+
projectRead = pfile.Read(mCurrentDirectory + "/" + projfile);
314+
315+
if (projectRead)
316+
{
317+
QStringList classes = pfile.GetDeAllocatedClasses();
318+
QString classname;
319+
foreach(classname, classes)
320+
{
321+
result.addAutoAllocClass(classname.toStdString());
322+
}
323+
}
324+
}
325+
295326
result._debug = false;
296327
result._showAll = true;
297328
result._checkCodingStyle = true;

gui/mainwindow.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ protected slots:
197197
QStringList GetFilesRecursively(const QString &path);
198198

199199
/**
200-
* @brief Get our default cppcheck settings
200+
* @brief Get our default cppcheck settings and read project file.
201201
*
202202
* @return Default cppcheck settings
203203
*/
@@ -368,6 +368,11 @@ protected slots:
368368
ApplicationList mApplications;
369369

370370
private:
371+
372+
/**
373+
* @brief Current checked directory.
374+
*/
375+
QString mCurrentDirectory;
371376
};
372377

373378
#endif // MAINWINDOW_H

gui/projectfile.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/
17+
*/
18+
19+
#include <QObject>
20+
#include <QString>
21+
#include <QXmlStreamReader>
22+
#include <QFile>
23+
#include "projectfile.h"
24+
25+
static const char ProjectElementName[] = "project";
26+
static const char AllocElementName[] = "autodealloc";
27+
static const char ClassElementName[] = "class";
28+
static const char ClassNameAttrib[] = "name";
29+
30+
ProjectFile::ProjectFile(QObject *parent) :
31+
QObject(parent)
32+
{
33+
}
34+
35+
ProjectFile::ProjectFile(const QString &filename, QObject *parent) :
36+
QObject(parent),
37+
mFilename(filename)
38+
{
39+
}
40+
41+
bool ProjectFile::Read(const QString &filename)
42+
{
43+
if (!filename.isEmpty())
44+
mFilename = filename;
45+
46+
QFile file(mFilename);
47+
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
48+
return false;
49+
50+
QXmlStreamReader xmlReader(&file);
51+
bool insideProject = false;
52+
while (!xmlReader.atEnd())
53+
{
54+
switch (xmlReader.readNext())
55+
{
56+
case QXmlStreamReader::StartElement:
57+
if (xmlReader.name() == ProjectElementName)
58+
insideProject = true;
59+
if (insideProject && xmlReader.name() == AllocElementName)
60+
ReadAutoAllocClasses(xmlReader);
61+
break;
62+
63+
case QXmlStreamReader::EndElement:
64+
if (xmlReader.name() == ProjectElementName)
65+
insideProject = false;
66+
break;
67+
}
68+
}
69+
70+
file.close();
71+
return true;
72+
}
73+
74+
QStringList ProjectFile::GetDeAllocatedClasses() const
75+
{
76+
return mDeAllocatedClasses;
77+
}
78+
79+
void ProjectFile::ReadAutoAllocClasses(QXmlStreamReader &reader)
80+
{
81+
QXmlStreamReader::TokenType type;
82+
bool allRead = false;
83+
do
84+
{
85+
type = reader.readNext();
86+
switch (type)
87+
{
88+
case QXmlStreamReader::StartElement:
89+
if (reader.name().toString() == ClassElementName)
90+
{
91+
QXmlStreamAttributes attribs = reader.attributes();
92+
QString name = attribs.value("", ClassNameAttrib).toString();
93+
if (!name.isEmpty())
94+
mDeAllocatedClasses << name;
95+
}
96+
break;
97+
98+
case QXmlStreamReader::EndElement:
99+
if (reader.name().toString() == AllocElementName)
100+
allRead = true;
101+
break;
102+
}
103+
104+
}
105+
while (!allRead);
106+
}

gui/projectfile.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2009 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/
17+
*/
18+
19+
#include <QObject>
20+
#include <QString>
21+
#include <QStringList>
22+
#include <QXmlStreamReader>
23+
24+
/**
25+
* @brief A class that reads and writes (TODO) project files.
26+
* The project files contain project-specific settings for checking. For
27+
* example a list of automatically deallocated classes.
28+
*/
29+
class ProjectFile : public QObject
30+
{
31+
Q_OBJECT
32+
33+
public:
34+
ProjectFile(QObject *parent = 0);
35+
ProjectFile(const QString &filename, QObject *parent = 0);
36+
37+
/**
38+
* @brief Read the project file.
39+
* @param filename Filename (can be also given to constructor).
40+
*/
41+
bool Read(const QString &filename = QString());
42+
43+
/**
44+
* @brief Get list of automatically deallocated classes.
45+
* @return list of classes.
46+
*/
47+
QStringList GetDeAllocatedClasses() const;
48+
49+
protected:
50+
void ReadAutoAllocClasses(QXmlStreamReader &reader);
51+
52+
private:
53+
54+
/**
55+
* @brief Filename (+path) of the project file.
56+
*/
57+
QString mFilename;
58+
59+
/**
60+
* @brief List of automatically deallocated classes.
61+
*/
62+
QStringList mDeAllocatedClasses;
63+
};

gui/projectfile.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Project files
2+
-------------
3+
4+
cppcheck GUI handles per-project settings in project files instead of global
5+
program settings. This allows customizing cppcheck for each project's needs.
6+
7+
Currently there is no GUI to edit the project file. The cppcheck automatically
8+
loads a project file which has same name than selected directory and extension
9+
".cppcheck". For example in "gui"-directory cppcheck loads "gui.cppcheck"
10+
project file.
11+
12+
The project file is simple XML file easy to edit with your favourite editor
13+
program. The format is:
14+
15+
<?xml version="1.0"?>
16+
<project version="1">
17+
<autodealloc>
18+
<class name="MyClass"/>
19+
<class name="MyClass2"/>
20+
</autodealloc>
21+
<includedir>
22+
<dir name="c:/projects/framework/" />
23+
<dir name="c:/Program Files/Visual Studio 8/VC/include/" />
24+
</includedir>
25+
</project>
26+
27+
See also gui.cppcheck file in gui-directory of cppcheck sources.

src/settings.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ void Settings::autoDealloc(std::istream &istr)
5555
}
5656
}
5757

58+
void Settings::addAutoAllocClass(const std::string &name)
59+
{
60+
_autoDealloc.push_back(name);
61+
}
5862

5963
bool Settings::isAutoDealloc(const char classname[]) const
6064
{

src/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class Settings
6767
/** Fill list of automaticly deallocated classes */
6868
void autoDealloc(std::istream &istr);
6969

70+
/** Add class to list of automatically deallocated classes */
71+
void addAutoAllocClass(const std::string &name);
72+
7073
/** is a class automaticly deallocated? */
7174
bool isAutoDealloc(const char classname[]) const;
7275
};

0 commit comments

Comments
 (0)