2015-08-16 17:36:10 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2015-11-18 20:04:50 +01:00
|
|
|
* Copyright (C) 2007-2015 Cppcheck team.
|
2015-08-16 17:36:10 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2015-08-30 10:24:44 +02:00
|
|
|
#include "libraryaddfunctiondialog.h"
|
2015-08-30 13:33:43 +02:00
|
|
|
#include "libraryeditargdialog.h"
|
2015-08-16 17:36:10 +02:00
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QFileDialog>
|
2015-08-23 12:49:51 +02:00
|
|
|
#include <QTextStream>
|
2015-08-29 18:50:08 +02:00
|
|
|
#include <QInputDialog>
|
2015-08-16 17:36:10 +02:00
|
|
|
|
|
|
|
// TODO: get/compare functions from header
|
|
|
|
|
2015-09-08 15:38:02 +02:00
|
|
|
class FunctionListItem : public QListWidgetItem {
|
|
|
|
public:
|
|
|
|
FunctionListItem(QListWidget *view,
|
|
|
|
CppcheckLibraryData::Function *function,
|
|
|
|
bool selected)
|
2015-09-08 15:43:22 +02:00
|
|
|
: QListWidgetItem(view), function(function) {
|
2015-09-08 15:38:02 +02:00
|
|
|
setText(function->name);
|
|
|
|
setFlags(flags() | Qt::ItemIsEditable);
|
|
|
|
setSelected(selected);
|
|
|
|
}
|
|
|
|
CppcheckLibraryData::Function *function;
|
|
|
|
};
|
|
|
|
|
2015-08-16 17:36:10 +02:00
|
|
|
LibraryDialog::LibraryDialog(QWidget *parent) :
|
|
|
|
QDialog(parent),
|
2015-08-29 17:24:56 +02:00
|
|
|
ui(new Ui::LibraryDialog),
|
|
|
|
ignoreChanges(false)
|
2015-08-16 17:36:10 +02:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2015-08-23 12:49:51 +02:00
|
|
|
ui->buttonSave->setEnabled(false);
|
2015-09-06 14:54:21 +02:00
|
|
|
ui->sortFunctions->setEnabled(false);
|
|
|
|
ui->filter->setEnabled(false);
|
2015-11-05 16:34:08 +01:00
|
|
|
ui->addFunction->setEnabled(false);
|
|
|
|
|
|
|
|
//As no function selected, this disables function editing widgets
|
|
|
|
selectFunction();
|
2015-08-16 17:36:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LibraryDialog::~LibraryDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
2015-08-29 17:24:56 +02:00
|
|
|
|
2015-09-06 14:20:27 +02:00
|
|
|
CppcheckLibraryData::Function *LibraryDialog::currentFunction()
|
|
|
|
{
|
|
|
|
QList<QListWidgetItem *> selitems = ui->functions->selectedItems();
|
|
|
|
if (selitems.count() != 1)
|
|
|
|
return nullptr;
|
2015-09-08 15:38:02 +02:00
|
|
|
return dynamic_cast<FunctionListItem *>(selitems.first())->function;
|
2015-09-06 14:20:27 +02:00
|
|
|
}
|
|
|
|
|
2015-08-16 17:36:10 +02:00
|
|
|
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()) {
|
2015-08-23 16:03:24 +02:00
|
|
|
mFileName.clear();
|
2015-08-16 17:36:10 +02:00
|
|
|
QFile file(selectedFile);
|
|
|
|
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2015-08-29 18:50:08 +02:00
|
|
|
ignoreChanges = true;
|
2015-08-23 16:03:24 +02:00
|
|
|
data.open(file);
|
2015-08-23 12:49:51 +02:00
|
|
|
mFileName = selectedFile;
|
|
|
|
ui->buttonSave->setEnabled(false);
|
2015-09-06 14:20:27 +02:00
|
|
|
ui->filter->clear();
|
2015-08-23 12:49:51 +02:00
|
|
|
ui->functions->clear();
|
2015-09-08 15:38:02 +02:00
|
|
|
for (struct CppcheckLibraryData::Function &function : data.functions) {
|
|
|
|
ui->functions->addItem(new FunctionListItem(ui->functions,
|
2015-09-08 15:43:22 +02:00
|
|
|
&function,
|
|
|
|
false));
|
2015-08-29 18:50:08 +02:00
|
|
|
}
|
2015-09-06 14:54:21 +02:00
|
|
|
ui->sortFunctions->setEnabled(!data.functions.empty());
|
|
|
|
ui->filter->setEnabled(!data.functions.empty());
|
2015-11-05 16:34:08 +01:00
|
|
|
ui->addFunction->setEnabled(true);
|
2015-08-29 18:50:08 +02:00
|
|
|
ignoreChanges = false;
|
2015-08-16 17:36:10 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-23 12:49:51 +02:00
|
|
|
}
|
2015-08-16 17:36:10 +02:00
|
|
|
|
2015-08-23 12:49:51 +02:00
|
|
|
void LibraryDialog::saveCfg()
|
|
|
|
{
|
2015-08-23 16:03:24 +02:00
|
|
|
if (mFileName.isNull())
|
|
|
|
return;
|
2015-08-23 12:49:51 +02:00
|
|
|
QFile file(mFileName);
|
|
|
|
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
2015-08-23 13:34:50 +02:00
|
|
|
QTextStream ts(&file);
|
2015-09-06 09:10:45 +02:00
|
|
|
ts << data.toString() << '\n';
|
2015-08-23 12:49:51 +02:00
|
|
|
ui->buttonSave->setEnabled(false);
|
|
|
|
}
|
2015-08-16 17:36:10 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 18:50:08 +02:00
|
|
|
void LibraryDialog::addFunction()
|
|
|
|
{
|
2015-08-30 10:24:44 +02:00
|
|
|
LibraryAddFunctionDialog *d = new LibraryAddFunctionDialog;
|
2015-08-30 13:33:43 +02:00
|
|
|
if (d->exec() == QDialog::Accepted && !d->functionName().isEmpty()) {
|
2015-08-30 10:24:44 +02:00
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::Function f;
|
2015-08-30 13:33:43 +02:00
|
|
|
f.name = d->functionName();
|
|
|
|
int args = d->numberOfArguments();
|
2015-08-30 10:24:44 +02:00
|
|
|
|
2015-08-30 13:33:43 +02:00
|
|
|
for (int i = 1; i <= args; i++) {
|
2015-08-30 13:41:57 +02:00
|
|
|
CppcheckLibraryData::Function::Arg arg;
|
2015-08-30 13:33:43 +02:00
|
|
|
arg.nr = i;
|
|
|
|
f.args.append(arg);
|
|
|
|
}
|
|
|
|
data.functions.append(f);
|
2015-09-08 15:38:02 +02:00
|
|
|
ui->functions->addItem(new FunctionListItem(ui->functions, &data.functions.back(), false));
|
2015-08-30 13:33:43 +02:00
|
|
|
ui->buttonSave->setEnabled(true);
|
2015-09-06 14:54:21 +02:00
|
|
|
ui->sortFunctions->setEnabled(!data.functions.empty());
|
|
|
|
ui->filter->setEnabled(!data.functions.empty());
|
2015-08-29 18:50:08 +02:00
|
|
|
}
|
2015-08-30 13:33:43 +02:00
|
|
|
delete d;
|
2015-08-29 18:50:08 +02:00
|
|
|
}
|
|
|
|
|
2015-09-08 15:38:02 +02:00
|
|
|
void LibraryDialog::editFunctionName(QListWidgetItem* item)
|
|
|
|
{
|
|
|
|
if (ignoreChanges)
|
|
|
|
return;
|
|
|
|
QString functionName = item->text();
|
|
|
|
CppcheckLibraryData::Function * const function = dynamic_cast<FunctionListItem*>(item)->function;
|
|
|
|
if (functionName != function->name) {
|
|
|
|
if (QRegExp(NAMES).exactMatch(functionName)) {
|
|
|
|
function->name = functionName;
|
|
|
|
ui->buttonSave->setEnabled(true);
|
|
|
|
} else {
|
|
|
|
ignoreChanges = true;
|
|
|
|
item->setText(function->name);
|
|
|
|
ignoreChanges = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 14:20:27 +02:00
|
|
|
void LibraryDialog::selectFunction()
|
2015-08-16 17:36:10 +02:00
|
|
|
{
|
2015-09-06 14:20:27 +02:00
|
|
|
const CppcheckLibraryData::Function * const function = currentFunction();
|
|
|
|
|
|
|
|
if (function == nullptr) {
|
2015-11-05 16:34:08 +01:00
|
|
|
ui->comments->clear();
|
|
|
|
ui->comments->setEnabled(false);
|
|
|
|
|
2015-09-03 20:36:26 +02:00
|
|
|
ui->noreturn->setCurrentIndex(0);
|
2015-11-05 16:34:08 +01:00
|
|
|
ui->noreturn->setEnabled(false);
|
|
|
|
|
2015-08-30 02:49:17 +02:00
|
|
|
ui->useretval->setChecked(false);
|
2015-11-05 16:34:08 +01:00
|
|
|
ui->useretval->setEnabled(false);
|
|
|
|
|
2015-08-30 02:49:17 +02:00
|
|
|
ui->leakignore->setChecked(false);
|
2015-11-05 16:34:08 +01:00
|
|
|
ui->leakignore->setEnabled(false);
|
|
|
|
|
2015-08-30 02:49:17 +02:00
|
|
|
ui->arguments->clear();
|
2015-11-05 16:34:08 +01:00
|
|
|
ui->arguments->setEnabled(false);
|
|
|
|
|
|
|
|
ui->editArgButton->setEnabled(false);
|
2015-08-30 02:49:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-29 17:24:56 +02:00
|
|
|
ignoreChanges = true;
|
2015-09-10 20:11:05 +02:00
|
|
|
ui->comments->setPlainText(function->comments);
|
2015-11-05 16:34:08 +01:00
|
|
|
ui->comments->setEnabled(true);
|
|
|
|
|
2015-09-06 14:20:27 +02:00
|
|
|
ui->noreturn->setCurrentIndex(function->noreturn);
|
2015-11-05 16:34:08 +01:00
|
|
|
ui->noreturn->setEnabled(true);
|
|
|
|
|
2015-09-06 14:20:27 +02:00
|
|
|
ui->useretval->setChecked(function->useretval);
|
2015-11-05 16:34:08 +01:00
|
|
|
ui->useretval->setEnabled(true);
|
|
|
|
|
2015-09-06 14:20:27 +02:00
|
|
|
ui->leakignore->setChecked(function->leakignore);
|
2015-11-05 16:34:08 +01:00
|
|
|
ui->leakignore->setEnabled(true);
|
|
|
|
|
2015-09-06 14:20:27 +02:00
|
|
|
updateArguments(*function);
|
2015-11-05 16:34:08 +01:00
|
|
|
ui->arguments->setEnabled(true);
|
|
|
|
|
|
|
|
ui->editArgButton->setEnabled(true);
|
2015-08-29 17:24:56 +02:00
|
|
|
ignoreChanges = false;
|
2015-08-16 17:36:10 +02:00
|
|
|
}
|
2015-08-23 12:49:51 +02:00
|
|
|
|
2015-09-06 14:20:27 +02:00
|
|
|
void LibraryDialog::sortFunctions(bool sort)
|
|
|
|
{
|
2015-09-08 15:38:02 +02:00
|
|
|
if (sort) {
|
2015-09-06 14:20:27 +02:00
|
|
|
ui->functions->sortItems();
|
2015-09-08 15:38:02 +02:00
|
|
|
} else {
|
2015-09-06 14:20:27 +02:00
|
|
|
ignoreChanges = true;
|
2015-09-06 14:54:21 +02:00
|
|
|
CppcheckLibraryData::Function *selfunction = currentFunction();
|
2015-09-06 14:20:27 +02:00
|
|
|
ui->functions->clear();
|
2015-09-08 15:38:02 +02:00
|
|
|
for (struct CppcheckLibraryData::Function &function : data.functions) {
|
|
|
|
ui->functions->addItem(new FunctionListItem(ui->functions,
|
2015-09-08 15:43:22 +02:00
|
|
|
&function,
|
|
|
|
selfunction == &function));
|
2015-09-06 14:54:21 +02:00
|
|
|
}
|
2015-09-06 14:20:27 +02:00
|
|
|
if (!ui->filter->text().isEmpty())
|
|
|
|
filterFunctions(ui->filter->text());
|
|
|
|
ignoreChanges = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryDialog::filterFunctions(QString filter)
|
2015-09-03 20:36:26 +02:00
|
|
|
{
|
2015-09-06 14:20:27 +02:00
|
|
|
QList<QListWidgetItem *> allItems = ui->functions->findItems(QString(), Qt::MatchContains);
|
|
|
|
|
|
|
|
if (filter.isEmpty()) {
|
|
|
|
foreach(QListWidgetItem *item, allItems) {
|
|
|
|
item->setHidden(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
foreach(QListWidgetItem *item, allItems) {
|
|
|
|
item->setHidden(!item->text().startsWith(filter));
|
|
|
|
}
|
|
|
|
}
|
2015-09-03 20:36:26 +02:00
|
|
|
}
|
|
|
|
|
2015-08-23 12:49:51 +02:00
|
|
|
void LibraryDialog::changeFunction()
|
|
|
|
{
|
2015-08-29 17:24:56 +02:00
|
|
|
if (ignoreChanges)
|
|
|
|
return;
|
2015-09-06 14:20:27 +02:00
|
|
|
|
|
|
|
CppcheckLibraryData::Function *function = currentFunction();
|
2015-11-05 16:34:08 +01:00
|
|
|
if (!function)
|
|
|
|
return;
|
|
|
|
|
2015-09-10 20:11:05 +02:00
|
|
|
function->comments = ui->comments->toPlainText();
|
2015-09-06 14:20:27 +02:00
|
|
|
function->noreturn = (CppcheckLibraryData::Function::TrueFalseUnknown)ui->noreturn->currentIndex();
|
|
|
|
function->useretval = ui->useretval->isChecked();
|
|
|
|
function->leakignore = ui->leakignore->isChecked();
|
|
|
|
|
2015-08-23 12:49:51 +02:00
|
|
|
ui->buttonSave->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
2015-08-30 13:33:43 +02:00
|
|
|
void LibraryDialog::editArg()
|
2015-08-29 17:24:56 +02:00
|
|
|
{
|
2015-09-06 14:20:27 +02:00
|
|
|
CppcheckLibraryData::Function *function = currentFunction();
|
|
|
|
if (!function)
|
2015-08-30 13:33:43 +02:00
|
|
|
return;
|
2015-09-06 14:20:27 +02:00
|
|
|
|
2015-08-30 13:33:43 +02:00
|
|
|
if (ui->arguments->selectedItems().count() != 1)
|
2015-08-29 17:24:56 +02:00
|
|
|
return;
|
2015-09-06 14:20:27 +02:00
|
|
|
CppcheckLibraryData::Function::Arg &arg = function->args[ui->arguments->row(ui->arguments->selectedItems().first())];
|
2015-08-30 13:33:43 +02:00
|
|
|
|
|
|
|
LibraryEditArgDialog *d = new LibraryEditArgDialog(0, arg);
|
|
|
|
if (d->exec() == QDialog::Accepted) {
|
|
|
|
arg = d->getArg();
|
2015-08-30 18:37:12 +02:00
|
|
|
ui->arguments->selectedItems().first()->setText(getArgText(arg));
|
2015-08-29 17:24:56 +02:00
|
|
|
}
|
|
|
|
|
2015-08-30 13:33:43 +02:00
|
|
|
delete d;
|
2015-08-29 17:24:56 +02:00
|
|
|
ui->buttonSave->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
2015-08-30 18:37:12 +02:00
|
|
|
QString LibraryDialog::getArgText(const CppcheckLibraryData::Function::Arg &arg)
|
|
|
|
{
|
|
|
|
QString s("arg");
|
|
|
|
if (arg.nr != CppcheckLibraryData::Function::Arg::ANY)
|
|
|
|
s += QString::number(arg.nr);
|
|
|
|
|
|
|
|
s += "\n not bool: " + QString(arg.notbool ? "true" : "false");
|
|
|
|
s += "\n not null: " + QString(arg.notnull ? "true" : "false");
|
|
|
|
s += "\n not uninit: " + QString(arg.notuninit ? "true" : "false");
|
|
|
|
s += "\n format string: " + QString(arg.formatstr ? "true" : "false");
|
|
|
|
s += "\n strz: " + QString(arg.strz ? "true" : "false");
|
|
|
|
s += "\n valid: " + QString(arg.valid.isEmpty() ? "any" : arg.valid);
|
|
|
|
foreach(const CppcheckLibraryData::Function::Arg::MinSize &minsize, arg.minsizes) {
|
|
|
|
s += "\n minsize: " + minsize.type + " " + minsize.arg + " " + minsize.arg2;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-08-30 13:41:57 +02:00
|
|
|
void LibraryDialog::updateArguments(const CppcheckLibraryData::Function &function)
|
2015-08-30 13:33:43 +02:00
|
|
|
{
|
|
|
|
ui->arguments->clear();
|
2015-08-30 13:41:57 +02:00
|
|
|
foreach(const CppcheckLibraryData::Function::Arg &arg, function.args) {
|
2015-08-30 18:37:12 +02:00
|
|
|
ui->arguments->addItem(getArgText(arg));
|
2015-08-30 13:33:43 +02:00
|
|
|
}
|
|
|
|
}
|