Skip to content

Commit eef13b6

Browse files
authored
Fix #12411 (GUI: product name and version in xml) (#5947)
1 parent 2fc6634 commit eef13b6

11 files changed

Lines changed: 67 additions & 20 deletions

File tree

gui/mainwindow.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ void MainWindow::analysisDone()
11661166
if (QDir(buildDir).exists()) {
11671167
mUI->mResults->saveStatistics(buildDir + "/statistics.txt");
11681168
mUI->mResults->updateFromOldReport(buildDir + "/lastResults.xml");
1169-
mUI->mResults->save(buildDir + "/lastResults.xml", Report::XMLV2);
1169+
mUI->mResults->save(buildDir + "/lastResults.xml", Report::XMLV2, mCppcheckCfgProductName);
11701170
}
11711171
}
11721172

@@ -1564,7 +1564,7 @@ void MainWindow::save()
15641564
type = Report::CSV;
15651565
}
15661566

1567-
mUI->mResults->save(selectedFile, type);
1567+
mUI->mResults->save(selectedFile, type, mCppcheckCfgProductName);
15681568
setPath(SETTINGS_LAST_RESULT_PATH, selectedFile);
15691569
}
15701570
}
@@ -1585,7 +1585,7 @@ void MainWindow::complianceReport()
15851585
tempResults.open();
15861586
tempResults.close();
15871587

1588-
mUI->mResults->save(tempResults.fileName(), Report::XMLV2);
1588+
mUI->mResults->save(tempResults.fileName(), Report::XMLV2, mCppcheckCfgProductName);
15891589

15901590
ComplianceReportDialog dlg(mProjectFile, tempResults.fileName());
15911591
dlg.exec();

gui/resultstree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ static int indexOf(const QList<ErrorItem> &list, const ErrorItem &item)
12311231
void ResultsTree::updateFromOldReport(const QString &filename)
12321232
{
12331233
QList<ErrorItem> oldErrors;
1234-
XmlReportV2 oldReport(filename);
1234+
XmlReportV2 oldReport(filename, QString());
12351235
if (oldReport.open()) {
12361236
oldErrors = oldReport.read();
12371237
oldReport.close();

gui/resultsview.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void ResultsView::updateFromOldReport(const QString &filename) const
204204
mUI->mTree->updateFromOldReport(filename);
205205
}
206206

207-
void ResultsView::save(const QString &filename, Report::Type type) const
207+
void ResultsView::save(const QString &filename, Report::Type type, const QString& productName) const
208208
{
209209
Report *report = nullptr;
210210

@@ -216,7 +216,7 @@ void ResultsView::save(const QString &filename, Report::Type type) const
216216
report = new TxtReport(filename);
217217
break;
218218
case Report::XMLV2:
219-
report = new XmlReportV2(filename);
219+
report = new XmlReportV2(filename, productName);
220220
break;
221221
}
222222

@@ -408,7 +408,7 @@ void ResultsView::readErrorsXml(const QString &filename)
408408
return;
409409
}
410410

411-
XmlReportV2 report(filename);
411+
XmlReportV2 report(filename, QString());
412412
QList<ErrorItem> errors;
413413
if (report.open()) {
414414
errors = report.read();

gui/resultsview.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ class ResultsView : public QWidget {
8686
*
8787
* @param filename Filename to save results to
8888
* @param type Type of the report.
89+
* @param productName Custom product name
8990
*/
90-
void save(const QString &filename, Report::Type type) const;
91+
void save(const QString &filename, Report::Type type, const QString& productName) const;
9192

9293
/**
9394
* @brief Update results from old report (tag, sinceDate)

gui/test/xmlreportv2/testxmlreportv2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
void TestXmlReportV2::readXml() const
2828
{
2929
const QString filepath(QString(SRCDIR) + "/../data/xmlfiles/xmlreport_v2.xml");
30-
XmlReportV2 report(filepath);
30+
XmlReportV2 report(filepath, QString());
3131
QVERIFY(report.open());
3232
QList<ErrorItem> errors = report.read();
3333
QCOMPARE(errors.size(), 6);

gui/xmlreportv2.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ static const QString IdAttribute = "id";
5353
static const QString SeverityAttribute = "severity";
5454
static const QString MsgAttribute = "msg";
5555
static const QString VersionAttribute = "version";
56+
static const QString ProductNameAttribute = "product-name";
5657
static const QString VerboseAttribute = "verbose";
5758

58-
XmlReportV2::XmlReportV2(const QString &filename) :
59+
XmlReportV2::XmlReportV2(const QString &filename, QString productName) :
5960
XmlReport(filename),
61+
mProductName(std::move(productName)),
6062
mXmlReader(nullptr),
6163
mXmlWriter(nullptr)
6264
{}
@@ -87,12 +89,18 @@ bool XmlReportV2::open()
8789

8890
void XmlReportV2::writeHeader()
8991
{
92+
const auto nameAndVersion = Settings::getNameAndVersion(mProductName.toStdString());
93+
const QString name = QString::fromStdString(nameAndVersion.first);
94+
const QString version = nameAndVersion.first.empty() ? CppCheck::version() : QString::fromStdString(nameAndVersion.second);
95+
9096
mXmlWriter->setAutoFormatting(true);
9197
mXmlWriter->writeStartDocument();
9298
mXmlWriter->writeStartElement(ResultElementName);
9399
mXmlWriter->writeAttribute(VersionAttribute, QString::number(2));
94100
mXmlWriter->writeStartElement(CppcheckElementName);
95-
mXmlWriter->writeAttribute(VersionAttribute, QString(CppCheck::version()));
101+
if (!name.isEmpty())
102+
mXmlWriter->writeAttribute(ProductNameAttribute, name);
103+
mXmlWriter->writeAttribute(VersionAttribute, version);
96104
mXmlWriter->writeEndElement();
97105
mXmlWriter->writeStartElement(ErrorsElementName);
98106
}

gui/xmlreportv2.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class QXmlStreamWriter;
3939
*/
4040
class XmlReportV2 : public XmlReport {
4141
public:
42-
explicit XmlReportV2(const QString &filename);
42+
explicit XmlReportV2(const QString &filename, QString productName);
4343
~XmlReportV2() override;
4444

4545
/**
@@ -82,6 +82,9 @@ class XmlReportV2 : public XmlReport {
8282
ErrorItem readError(const QXmlStreamReader *reader);
8383

8484
private:
85+
/** Product name read from cppcheck.cfg */
86+
const QString mProductName;
87+
8588
/**
8689
* @brief XML stream reader for reading the report in XML format.
8790
*/

lib/errorlogger.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -425,14 +425,9 @@ void ErrorMessage::deserialize(const std::string &data)
425425

426426
std::string ErrorMessage::getXMLHeader(std::string productName)
427427
{
428-
std::string version = CppCheck::version();
429-
if (!productName.empty() && std::isdigit(productName.back())) {
430-
const std::string::size_type pos = productName.find_last_not_of(".0123456789");
431-
if (pos > 1 && pos != std::string::npos && productName[pos] == ' ') {
432-
version = productName.substr(pos+1);
433-
productName.erase(pos);
434-
}
435-
}
428+
const auto nameAndVersion = Settings::getNameAndVersion(productName);
429+
productName = nameAndVersion.first;
430+
const std::string version = nameAndVersion.first.empty() ? CppCheck::version() : nameAndVersion.second;
436431

437432
tinyxml2::XMLPrinter printer;
438433

lib/settings.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ std::string Settings::loadCppcheckCfg()
132132
return "";
133133
}
134134

135+
std::pair<std::string, std::string> Settings::getNameAndVersion(const std::string& productName) {
136+
if (productName.empty())
137+
return {};
138+
const std::string::size_type pos1 = productName.rfind(' ');
139+
if (pos1 == std::string::npos)
140+
return {};
141+
if (pos1 + 2 >= productName.length())
142+
return {};
143+
for (auto pos2 = pos1 + 1; pos2 < productName.length(); ++pos2) {
144+
const char c = productName[pos2];
145+
const char prev = productName[pos2-1];
146+
if (std::isdigit(c))
147+
continue;
148+
if (c == '.' && std::isdigit(prev))
149+
continue;
150+
if (c == 's' && pos2 + 1 == productName.length() && std::isdigit(prev))
151+
continue;
152+
return {};
153+
}
154+
return {productName.substr(0, pos1), productName.substr(pos1+1)};
155+
}
156+
135157
std::string Settings::parseEnabled(const std::string &str, std::tuple<SimpleEnableGroup<Severity>, SimpleEnableGroup<Checks>> &groups)
136158
{
137159
// Enable parameters may be comma separated...

lib/settings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class CPPCHECKLIB WARN_UNUSED Settings {
101101

102102
std::string loadCppcheckCfg();
103103

104+
static std::pair<std::string, std::string> getNameAndVersion(const std::string& productName);
105+
104106
/** @brief addons, either filename of python/json file or json data */
105107
std::unordered_set<std::string> addons;
106108

0 commit comments

Comments
 (0)