GUI: Add libraryeditor

This commit is contained in:
Daniel Marjamäki 2015-08-16 17:36:10 +02:00
parent c26ec86508
commit 4946f772ef
7 changed files with 456 additions and 5 deletions

View File

@ -5,7 +5,7 @@ DEPENDPATH += . \
../lib ../lib
INCLUDEPATH += . \ INCLUDEPATH += . \
../lib ../lib
QT += xml
greaterThan(QT_MAJOR_VERSION, 4) { greaterThan(QT_MAJOR_VERSION, 4) {
QT += widgets # In Qt 5 widgets are in separate module QT += widgets # In Qt 5 widgets are in separate module
QT += printsupport # In Qt 5 QPrinter/QPrintDialog are in separate module QT += printsupport # In Qt 5 QPrinter/QPrintDialog are in separate module
@ -50,7 +50,8 @@ FORMS = about.ui \
resultsview.ui \ resultsview.ui \
scratchpad.ui \ scratchpad.ui \
settings.ui \ settings.ui \
stats.ui stats.ui \
librarydialog.ui
TRANSLATIONS = cppcheck_de.ts \ TRANSLATIONS = cppcheck_de.ts \
cppcheck_es.ts \ cppcheck_es.ts \
@ -105,7 +106,8 @@ HEADERS += aboutdialog.h \
txtreport.h \ txtreport.h \
xmlreport.h \ xmlreport.h \
xmlreportv1.h \ xmlreportv1.h \
xmlreportv2.h xmlreportv2.h \
librarydialog.h
SOURCES += aboutdialog.cpp \ SOURCES += aboutdialog.cpp \
application.cpp \ application.cpp \
@ -139,7 +141,8 @@ SOURCES += aboutdialog.cpp \
txtreport.cpp \ txtreport.cpp \
xmlreport.cpp \ xmlreport.cpp \
xmlreportv1.cpp \ xmlreportv1.cpp \
xmlreportv2.cpp xmlreportv2.cpp \
librarydialog.cpp
win32 { win32 {
DEFINES += _CRT_SECURE_NO_WARNINGS DEFINES += _CRT_SECURE_NO_WARNINGS

182
gui/librarydialog.cpp Normal file
View File

@ -0,0 +1,182 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2015 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 "librarydialog.h"
#include "ui_librarydialog.h"
#include <QFile>
#include <QDomDocument>
#include <QDomNode>
#include <QDomNodeList>
#include <QSettings>
#include <QFileDialog>
const unsigned int LibraryDialog::Function::Arg::ANY = ~0U;
// TODO: get/compare functions from header
LibraryDialog::LibraryDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LibraryDialog)
{
ui->setupUi(this);
}
LibraryDialog::~LibraryDialog()
{
delete ui;
}
void LibraryDialog::updateui()
{
ui->functions->clear();
for (const struct Function &function : functions) {
ui->functions->addItem(function.name);
}
}
QStringList getFunctions(QFile &file)
{
QStringList ret;
QDomDocument doc;
if (!doc.setContent(&file))
return ret;
return ret;
}
static LibraryDialog::Function::Arg loadFunctionArg(const QDomElement &functionArgElement)
{
LibraryDialog::Function::Arg arg;
if (functionArgElement.attribute("nr") == "any")
arg.nr = LibraryDialog::Function::Arg::ANY;
else
arg.nr = functionArgElement.attribute("nr").toUInt();
for (QDomElement childElement = functionArgElement.firstChildElement(); !childElement.isNull(); childElement = childElement.nextSiblingElement()) {
if (childElement.tagName() == "not-bool")
arg.notbool = true;
else if (childElement.tagName() == "not-null")
arg.notnull = true;
else if (childElement.tagName() == "not-uninit")
arg.notuninit = true;
else if (childElement.tagName() == "strz")
arg.strz = true;
else if (childElement.tagName() == "formatstr")
arg.formatstr = true;
else if (childElement.tagName() == "valid")
arg.valid = childElement.text();
else if (childElement.tagName() == "minsize") {
arg.minsize.type = childElement.attribute("type");
arg.minsize.arg = childElement.attribute("arg");
arg.minsize.arg2 = childElement.attribute("arg2");
}
}
return arg;
}
static LibraryDialog::Function loadFunction(const QDomElement &functionElement)
{
LibraryDialog::Function function;
function.name = functionElement.attribute("name");
for (QDomElement childElement = functionElement.firstChildElement(); !childElement.isNull(); childElement = childElement.nextSiblingElement()) {
const QString tagName = childElement.tagName();
if (childElement.tagName() == "noreturn")
function.noreturn = (childElement.text() == "true");
else if (childElement.tagName() == "pure")
function.gccPure = true;
else if (childElement.tagName() == "const")
function.gccConst = true;
else if (childElement.tagName() == "leak-ignore")
function.leakignore = true;
else if (childElement.tagName() == "use-retval")
function.useretval = true;
else if (childElement.tagName() == "formatstr") {
function.formatstr.scan = childElement.attribute("scan");
function.formatstr.secure = childElement.attribute("secure");
} else if (childElement.tagName() == "arg") {
const LibraryDialog::Function::Arg fa = loadFunctionArg(childElement);
function.args.append(fa);
} else {
int x = 123;
x++;
}
}
return function;
}
bool LibraryDialog::loadFile(QFile &file)
{
QDomDocument doc;
if (!doc.setContent(&file))
return false;
QDomElement rootElement = doc.firstChildElement("def");
for (QDomElement functionElement = rootElement.firstChildElement("function"); !functionElement.isNull(); functionElement = functionElement.nextSiblingElement("function")) {
functions.append(loadFunction(functionElement));
}
return true;
}
void LibraryDialog::openCfg()
{
const QSettings settings;
const QString datadir = settings.value("DATADIR",QString()).toString();
QString selectedFilter;
const QString filter(tr("Library files (*.cfg)"));
const QString selectedFile = QFileDialog::getOpenFileName(this,
tr("Open library file"),
datadir,
filter,
&selectedFilter);
if (!selectedFile.isEmpty()) {
QFile file(selectedFile);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
loadFile(file);
updateui();
}
}
}
void LibraryDialog::selectFunction(int row)
{
const Function &function = functions[row];
ui->functionreturn->setChecked(!function.noreturn);
ui->useretval->setChecked(function.useretval);
ui->leakignore->setChecked(function.leakignore);
ui->arguments->clear();
for (const Function::Arg &arg : function.args) {
QString s("arg");
if (arg.nr != Function::Arg::ANY)
s += QString::number(arg.nr);
if (arg.formatstr)
s += " formatstr";
else if (!arg.strz)
s += " strz";
else if (!arg.valid.isNull())
s += " " + arg.valid;
ui->arguments->addItem(s);
}
}

78
gui/librarydialog.h Normal file
View File

@ -0,0 +1,78 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2015 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/>.
*/
#ifndef LIBRARYDIALOG_H
#define LIBRARYDIALOG_H
#include <QDialog>
#include <QFile>
namespace Ui {
class LibraryDialog;
}
class LibraryDialog : public QDialog {
Q_OBJECT
public:
explicit LibraryDialog(QWidget *parent = 0);
~LibraryDialog();
struct Function {
QString name;
bool noreturn = false;
bool gccPure = false;
bool gccConst = false;
bool leakignore = false;
bool useretval = false;
struct {
QString scan;
QString secure;
} formatstr;
struct Arg {
QString name;
unsigned int nr = 0;
static const unsigned int ANY;
bool notbool = false;
bool notnull = false;
bool notuninit = false;
bool formatstr = false;
bool strz = false;
QString valid;
struct {
QString type;
QString arg;
QString arg2;
} minsize;
};
QList<struct Arg> args;
};
private slots:
void openCfg();
void selectFunction(int row);
private:
Ui::LibraryDialog *ui;
void updateui();
bool loadFile(QFile &file);
QList<struct Function> functions;
};
#endif // LIBRARYDIALOG_H

165
gui/librarydialog.ui Normal file
View File

@ -0,0 +1,165 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>LibraryDialog</class>
<widget class="QDialog" name="LibraryDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>547</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Library Editor</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="label">
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="styleSheet">
<string notr="true">QLabel { background-color : red; color : blue; }</string>
</property>
<property name="text">
<string>EXPERIMENTAL</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Open</string>
</property>
</widget>
</item>
<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>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Functions</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="functions">
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="functionreturn">
<property name="text">
<string>function always return</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="useretval">
<property name="text">
<string>return value must be used</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="leakignore">
<property name="text">
<string>ignore function in leaks checking</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Arguments</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="arguments"/>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>functions</sender>
<signal>currentRowChanged(int)</signal>
<receiver>LibraryDialog</receiver>
<slot>selectFunction(int)</slot>
<hints>
<hint type="sourcelabel">
<x>155</x>
<y>218</y>
</hint>
<hint type="destinationlabel">
<x>327</x>
<y>299</y>
</hint>
</hints>
</connection>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<receiver>LibraryDialog</receiver>
<slot>openCfg()</slot>
<hints>
<hint type="sourcelabel">
<x>56</x>
<y>36</y>
</hint>
<hint type="destinationlabel">
<x>72</x>
<y>21</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>selectFunction(int)</slot>
<slot>openCfg()</slot>
</slots>
</ui>

View File

@ -62,7 +62,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>640</width> <width>640</width>
<height>21</height> <height>20</height>
</rect> </rect>
</property> </property>
<widget class="QMenu" name="mMenuFile"> <widget class="QMenu" name="mMenuFile">
@ -115,6 +115,7 @@
<addaction name="mActionShowScratchpad"/> <addaction name="mActionShowScratchpad"/>
<addaction name="mActionViewLog"/> <addaction name="mActionViewLog"/>
<addaction name="mActionViewStats"/> <addaction name="mActionViewStats"/>
<addaction name="mActionLibraryEditor"/>
</widget> </widget>
<widget class="QMenu" name="mMenuHelp"> <widget class="QMenu" name="mMenuHelp">
<property name="title"> <property name="title">
@ -702,6 +703,14 @@
<string>Open a Print Preview Dialog for the Current Results</string> <string>Open a Print Preview Dialog for the Current Results</string>
</property> </property>
</action> </action>
<action name="mActionLibraryEditor">
<property name="text">
<string>LibraryEditor...</string>
</property>
<property name="toolTip">
<string>Open library editor</string>
</property>
</action>
</widget> </widget>
<customwidgets> <customwidgets>
<customwidget> <customwidget>

View File

@ -44,6 +44,7 @@
#include "logview.h" #include "logview.h"
#include "filelist.h" #include "filelist.h"
#include "showtypes.h" #include "showtypes.h"
#include "librarydialog.h"
static const QString OnlineHelpURL("http://cppcheck.sourceforge.net/manual.html"); static const QString OnlineHelpURL("http://cppcheck.sourceforge.net/manual.html");
@ -99,6 +100,7 @@ MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
connect(mUI.mActionShowHidden, SIGNAL(triggered()), mUI.mResults, SLOT(ShowHiddenResults())); connect(mUI.mActionShowHidden, SIGNAL(triggered()), mUI.mResults, SLOT(ShowHiddenResults()));
connect(mUI.mActionViewLog, SIGNAL(triggered()), this, SLOT(ShowLogView())); connect(mUI.mActionViewLog, SIGNAL(triggered()), this, SLOT(ShowLogView()));
connect(mUI.mActionViewStats, SIGNAL(triggered()), this, SLOT(ShowStatistics())); connect(mUI.mActionViewStats, SIGNAL(triggered()), this, SLOT(ShowStatistics()));
connect(mUI.mActionLibraryEditor, SIGNAL(triggered()), this, SLOT(ShowLibraryEditor()));
connect(mUI.mActionRecheck, SIGNAL(triggered()), this, SLOT(ReCheck())); connect(mUI.mActionRecheck, SIGNAL(triggered()), this, SLOT(ReCheck()));
@ -1215,6 +1217,12 @@ void MainWindow::ShowStatistics()
statsDialog.exec(); statsDialog.exec();
} }
void MainWindow::ShowLibraryEditor()
{
LibraryDialog libraryDialog(this);
libraryDialog.exec();
}
void MainWindow::Log(const QString &logline) void MainWindow::Log(const QString &logline)
{ {
if (mLogView) { if (mLogView) {

View File

@ -220,6 +220,12 @@ public slots:
*/ */
void ShowStatistics(); void ShowStatistics();
/**
* @brief Slot for showing the library editor
*
*/
void ShowLibraryEditor();
protected slots: protected slots:
/** /**