GUI: Better error handling when library file has unhandled elements
This commit is contained in:
parent
d75d0965ff
commit
247192452f
|
@ -28,6 +28,11 @@ CppcheckLibraryData::CppcheckLibraryData()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static std::string unhandledElement(const QXmlStreamReader &xmlReader)
|
||||||
|
{
|
||||||
|
throw std::runtime_error(QObject::tr("line %1: Unhandled element %2").arg(xmlReader.lineNumber()).arg(xmlReader.name().toString()).toStdString());
|
||||||
|
}
|
||||||
|
|
||||||
static CppcheckLibraryData::Container loadContainer(QXmlStreamReader &xmlReader)
|
static CppcheckLibraryData::Container loadContainer(QXmlStreamReader &xmlReader)
|
||||||
{
|
{
|
||||||
CppcheckLibraryData::Container container;
|
CppcheckLibraryData::Container container;
|
||||||
|
@ -72,7 +77,7 @@ static CppcheckLibraryData::Container loadContainer(QXmlStreamReader &xmlReader)
|
||||||
container.otherFunctions.append(function);
|
container.otherFunctions.append(function);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error("Unhandled element " + elementName.toStdString());
|
unhandledElement(xmlReader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return container;
|
return container;
|
||||||
|
@ -126,7 +131,7 @@ static CppcheckLibraryData::Function::Arg loadFunctionArg(QXmlStreamReader &xmlR
|
||||||
arg.iterator.container = xmlReader.attributes().value("container").toInt();
|
arg.iterator.container = xmlReader.attributes().value("container").toInt();
|
||||||
arg.iterator.type = xmlReader.attributes().value("type").toString();
|
arg.iterator.type = xmlReader.attributes().value("type").toString();
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error("Unhandled element " + elementName.toStdString());
|
unhandledElement(xmlReader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arg;
|
return arg;
|
||||||
|
@ -170,7 +175,7 @@ static CppcheckLibraryData::Function loadFunction(QXmlStreamReader &xmlReader, c
|
||||||
function.warn.alternatives = xmlReader.attributes().value("alternatives").toString();
|
function.warn.alternatives = xmlReader.attributes().value("alternatives").toString();
|
||||||
function.warn.msg = xmlReader.readElementText();
|
function.warn.msg = xmlReader.readElementText();
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error("Unhandled element " + elementName.toStdString());
|
unhandledElement(xmlReader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return function;
|
return function;
|
||||||
|
@ -196,7 +201,7 @@ static CppcheckLibraryData::MemoryResource loadMemoryResource(QXmlStreamReader &
|
||||||
else if (elementName == "use")
|
else if (elementName == "use")
|
||||||
memoryresource.use.append(xmlReader.readElementText());
|
memoryresource.use.append(xmlReader.readElementText());
|
||||||
else
|
else
|
||||||
throw std::runtime_error("Unhandled element " + elementName.toStdString());
|
unhandledElement(xmlReader);
|
||||||
}
|
}
|
||||||
return memoryresource;
|
return memoryresource;
|
||||||
}
|
}
|
||||||
|
@ -210,7 +215,7 @@ static CppcheckLibraryData::PodType loadPodType(const QXmlStreamReader &xmlReade
|
||||||
return podtype;
|
return podtype;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CppcheckLibraryData::open(QIODevice &file)
|
QString CppcheckLibraryData::open(QIODevice &file)
|
||||||
{
|
{
|
||||||
clear();
|
clear();
|
||||||
QString comments;
|
QString comments;
|
||||||
|
@ -239,10 +244,9 @@ bool CppcheckLibraryData::open(QIODevice &file)
|
||||||
else if (elementName == "podtype")
|
else if (elementName == "podtype")
|
||||||
podtypes.append(loadPodType(xmlReader));
|
podtypes.append(loadPodType(xmlReader));
|
||||||
else
|
else
|
||||||
return false;
|
unhandledElement(xmlReader);
|
||||||
} catch (std::runtime_error &e) {
|
} catch (std::runtime_error &e) {
|
||||||
const QString what(e.what());
|
return e.what();
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
comments.clear();
|
comments.clear();
|
||||||
break;
|
break;
|
||||||
|
@ -251,7 +255,7 @@ bool CppcheckLibraryData::open(QIODevice &file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void writeContainerFunctions(QXmlStreamWriter &xmlWriter, const QString name, int extra, const QList<struct CppcheckLibraryData::Container::Function> &functions)
|
static void writeContainerFunctions(QXmlStreamWriter &xmlWriter, const QString name, int extra, const QList<struct CppcheckLibraryData::Container::Function> &functions)
|
||||||
|
|
|
@ -159,7 +159,16 @@ public:
|
||||||
podtypes.clear();
|
podtypes.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool open(QIODevice &file);
|
|
||||||
|
void swap(CppcheckLibraryData &other) {
|
||||||
|
containers.swap(other.containers);
|
||||||
|
defines.swap(other.defines);
|
||||||
|
functions.swap(other.functions);
|
||||||
|
memoryresource.swap(other.memoryresource);
|
||||||
|
podtypes.swap(other.podtypes);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString open(QIODevice &file);
|
||||||
QString toString() const;
|
QString toString() const;
|
||||||
|
|
||||||
QList<struct Container> containers;
|
QList<struct Container> containers;
|
||||||
|
|
|
@ -90,31 +90,44 @@ void LibraryDialog::openCfg()
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QFile file(selectedFile);
|
QFile file(selectedFile);
|
||||||
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
ignoreChanges = true;
|
|
||||||
data.open(file);
|
|
||||||
mFileName = selectedFile;
|
|
||||||
ui->buttonSave->setEnabled(false);
|
|
||||||
ui->buttonSaveAs->setEnabled(true);
|
|
||||||
ui->filter->clear();
|
|
||||||
ui->functions->clear();
|
|
||||||
for (CppcheckLibraryData::Function &function : data.functions) {
|
|
||||||
ui->functions->addItem(new FunctionListItem(ui->functions,
|
|
||||||
&function,
|
|
||||||
false));
|
|
||||||
}
|
|
||||||
ui->sortFunctions->setEnabled(!data.functions.empty());
|
|
||||||
ui->filter->setEnabled(!data.functions.empty());
|
|
||||||
ui->addFunction->setEnabled(true);
|
|
||||||
ignoreChanges = false;
|
|
||||||
} else {
|
|
||||||
QMessageBox msg(QMessageBox::Critical,
|
QMessageBox msg(QMessageBox::Critical,
|
||||||
tr("Cppcheck"),
|
tr("Cppcheck"),
|
||||||
tr("Can not open file %1.").arg(selectedFile),
|
tr("Can not open file %1.").arg(selectedFile),
|
||||||
QMessageBox::Ok,
|
QMessageBox::Ok,
|
||||||
this);
|
this);
|
||||||
msg.exec();
|
msg.exec();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CppcheckLibraryData tempdata;
|
||||||
|
const QString errmsg = tempdata.open(file);
|
||||||
|
if (!errmsg.isNull()) {
|
||||||
|
QMessageBox msg(QMessageBox::Critical,
|
||||||
|
tr("Cppcheck"),
|
||||||
|
tr("Failed to load %1. %2.").arg(selectedFile).arg(errmsg),
|
||||||
|
QMessageBox::Ok,
|
||||||
|
this);
|
||||||
|
msg.exec();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ignoreChanges = true;
|
||||||
|
data.swap(tempdata);
|
||||||
|
mFileName = selectedFile;
|
||||||
|
ui->buttonSave->setEnabled(false);
|
||||||
|
ui->buttonSaveAs->setEnabled(true);
|
||||||
|
ui->filter->clear();
|
||||||
|
ui->functions->clear();
|
||||||
|
for (CppcheckLibraryData::Function &function : data.functions) {
|
||||||
|
ui->functions->addItem(new FunctionListItem(ui->functions,
|
||||||
|
&function,
|
||||||
|
false));
|
||||||
|
}
|
||||||
|
ui->sortFunctions->setEnabled(!data.functions.empty());
|
||||||
|
ui->filter->setEnabled(!data.functions.empty());
|
||||||
|
ui->addFunction->setEnabled(true);
|
||||||
|
ignoreChanges = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LibraryDialog::saveCfg()
|
void LibraryDialog::saveCfg()
|
||||||
|
|
Loading…
Reference in New Issue