Skip to content

Commit 9a51fc5

Browse files
authored
Add fix to support "dealloc" element in memory or resource section of library configuration. Add missing attributes to avoid loss during library configuration save. (cppcheck-opensource#2978)
1 parent 3470c2e commit 9a51fc5

2 files changed

Lines changed: 64 additions & 9 deletions

File tree

gui/cppchecklibrarydata.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,29 @@ static CppcheckLibraryData::MemoryResource loadMemoryResource(QXmlStreamReader &
198198
if (type != QXmlStreamReader::StartElement)
199199
continue;
200200
const QString elementName = xmlReader.name().toString();
201-
if (elementName == "alloc") {
201+
if (elementName == "alloc" || elementName == "realloc") {
202202
CppcheckLibraryData::MemoryResource::Alloc alloc;
203+
alloc.isRealloc = (elementName == "realloc");
203204
alloc.init = (xmlReader.attributes().value("init").toString() == "true");
205+
if (xmlReader.attributes().hasAttribute("arg")) {
206+
alloc.arg = xmlReader.attributes().value("arg").toInt();
207+
}
208+
if (alloc.isRealloc && xmlReader.attributes().hasAttribute("realloc-arg")) {
209+
alloc.reallocArg = xmlReader.attributes().value("realloc-arg").toInt();
210+
}
211+
if (memoryresource.type == "memory") {
212+
alloc.bufferSize = xmlReader.attributes().value("buffer-size").toString();
213+
}
204214
alloc.name = xmlReader.readElementText();
205215
memoryresource.alloc.append(alloc);
206-
} else if (elementName == "dealloc")
207-
memoryresource.dealloc.append(xmlReader.readElementText());
216+
} else if (elementName == "dealloc") {
217+
CppcheckLibraryData::MemoryResource::Dealloc dealloc;
218+
if (xmlReader.attributes().hasAttribute("arg")) {
219+
dealloc.arg = xmlReader.attributes().value("arg").toInt();
220+
}
221+
dealloc.name = xmlReader.readElementText();
222+
memoryresource.dealloc.append(dealloc);
223+
}
208224
else if (elementName == "use")
209225
memoryresource.use.append(xmlReader.readElementText());
210226
else
@@ -437,14 +453,34 @@ static void writeMemoryResource(QXmlStreamWriter &xmlWriter, const CppcheckLibra
437453
{
438454
xmlWriter.writeStartElement(mr.type);
439455
foreach (const CppcheckLibraryData::MemoryResource::Alloc &alloc, mr.alloc) {
456+
if (alloc.isRealloc) {
457+
xmlWriter.writeStartElement("realloc");
458+
} else {
440459
xmlWriter.writeStartElement("alloc");
460+
}
441461
xmlWriter.writeAttribute("init", alloc.init ? "true" : "false");
462+
if (alloc.arg != -1) {
463+
xmlWriter.writeAttribute("arg", QString("%1").arg(alloc.arg));
464+
}
465+
if (alloc.isRealloc && alloc.reallocArg != -1) {
466+
xmlWriter.writeAttribute("realloc-arg", QString("%1").arg(alloc.reallocArg));
467+
}
468+
if (mr.type == "memory" && !alloc.bufferSize.isEmpty()) {
469+
xmlWriter.writeAttribute("buffer-size", alloc.bufferSize);
470+
}
442471
xmlWriter.writeCharacters(alloc.name);
443472
xmlWriter.writeEndElement();
444473
}
445-
foreach (const QString &dealloc, mr.dealloc) {
446-
xmlWriter.writeTextElement("dealloc", dealloc);
474+
475+
foreach (const CppcheckLibraryData::MemoryResource::Dealloc &dealloc, mr.dealloc) {
476+
xmlWriter.writeStartElement("dealloc");
477+
if (dealloc.arg != -1) {
478+
xmlWriter.writeAttribute("arg", QString("%1").arg(dealloc.arg));
479+
}
480+
xmlWriter.writeCharacters(dealloc.name);
481+
xmlWriter.writeEndElement();
447482
}
483+
448484
foreach (const QString &use, mr.use) {
449485
xmlWriter.writeTextElement("use", use);
450486
}

gui/cppchecklibrarydata.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Cppcheck - A tool for static C/C++ code analysis
3-
* Copyright (C) 2007-2018 Cppcheck team.
3+
* Copyright (C) 2007-2020 Cppcheck team.
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -136,12 +136,31 @@ class CppcheckLibraryData {
136136
struct MemoryResource {
137137
QString type; // "memory" or "resource"
138138
struct Alloc {
139-
Alloc() : init(false) {}
139+
Alloc() :
140+
isRealloc(false),
141+
init(false),
142+
arg(-1), // -1: Has no optional "arg" attribute
143+
reallocArg(-1) // -1: Has no optional "realloc-arg" attribute
144+
{}
145+
146+
bool isRealloc;
140147
bool init;
148+
int arg;
149+
int reallocArg;
150+
QString bufferSize;
141151
QString name;
142152
};
153+
struct Dealloc {
154+
Dealloc() :
155+
arg(-1) // -1: Has no optional "arg" attribute
156+
{}
157+
158+
int arg;
159+
QString name;
160+
};
161+
143162
QList<struct Alloc> alloc;
144-
QStringList dealloc;
163+
QList<struct Dealloc> dealloc;
145164
QStringList use;
146165
};
147166

@@ -181,4 +200,4 @@ class CppcheckLibraryData {
181200
QStringList undefines;
182201
};
183202

184-
#endif // LIBRARYDATA_H
203+
#endif // CPPCHECKLIBRARYDATA_H

0 commit comments

Comments
 (0)