Cppcheck
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
xmlreportv1.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2016 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <QObject>
20 #include <QString>
21 #include <QList>
22 #include <QDir>
23 #include <QXmlStreamWriter>
24 #include <QDebug>
25 #include "report.h"
26 #include "erroritem.h"
27 #include "xmlreport.h"
28 #include "xmlreportv1.h"
29 
30 static const char ResultElementName[] = "results";
31 static const char ErrorElementName[] = "error";
32 static const char FilenameAttribute[] = "file";
33 static const char LineAttribute[] = "line";
34 static const char IdAttribute[] = "id";
35 static const char SeverityAttribute[] = "severity";
36 static const char MsgAttribute[] = "msg";
37 
38 XmlReportV1::XmlReportV1(const QString &filename) :
39  XmlReport(filename),
40  mXmlReader(NULL),
41  mXmlWriter(NULL)
42 {
43 }
44 
46 {
47  delete mXmlReader;
48  delete mXmlWriter;
49 }
50 
52 {
53  if (Report::create()) {
54  mXmlWriter = new QXmlStreamWriter(Report::getFile());
55  return true;
56  }
57  return false;
58 }
59 
61 {
62  if (Report::open()) {
63  mXmlReader = new QXmlStreamReader(Report::getFile());
64  return true;
65  }
66  return false;
67 }
68 
70 {
71  mXmlWriter->setAutoFormatting(true);
72  mXmlWriter->writeStartDocument();
73  mXmlWriter->writeStartElement(ResultElementName);
74 }
75 
77 {
78  mXmlWriter->writeEndElement();
79  mXmlWriter->writeEndDocument();
80 }
81 
83 {
84  /*
85  Error example from the core program in xml
86  <error file="gui/test.cpp" line="14" id="mismatchAllocDealloc" severity="error" msg="Mismatching allocation and deallocation: k"/>
87  The callstack seems to be ignored here as well, instead last item of the stack is used
88  */
89 
90  // Don't write inconclusive errors to XML V1
91  if (error.inconclusive)
92  return;
93 
94  mXmlWriter->writeStartElement(ErrorElementName);
95  QString file = QDir::toNativeSeparators(error.errorPath.back().file);
96  file = XmlReport::quoteMessage(file);
97  mXmlWriter->writeAttribute(FilenameAttribute, file);
98  const QString line = QString::number(error.errorPath.back().line);
99  mXmlWriter->writeAttribute(LineAttribute, line);
100  mXmlWriter->writeAttribute(IdAttribute, error.errorId);
101 
102  // Don't localize severity so we can read these files
104  const QString message = XmlReport::quoteMessage(error.message);
105  mXmlWriter->writeAttribute(MsgAttribute, message);
106  mXmlWriter->writeEndElement();
107 }
108 
109 QList<ErrorItem> XmlReportV1::read()
110 {
111  QList<ErrorItem> errors;
112  bool insideResults = false;
113  if (!mXmlReader) {
114  qDebug() << "You must Open() the file before reading it!";
115  return errors;
116  }
117  while (!mXmlReader->atEnd()) {
118  switch (mXmlReader->readNext()) {
119  case QXmlStreamReader::StartElement:
120  if (mXmlReader->name() == ResultElementName)
121  insideResults = true;
122 
123  // Read error element from inside result element
124  if (insideResults && mXmlReader->name() == ErrorElementName) {
126  errors.append(item);
127  }
128  break;
129 
130  case QXmlStreamReader::EndElement:
131  if (mXmlReader->name() == ResultElementName)
132  insideResults = false;
133  break;
134 
135  // Not handled
136  case QXmlStreamReader::NoToken:
137  case QXmlStreamReader::Invalid:
138  case QXmlStreamReader::StartDocument:
139  case QXmlStreamReader::EndDocument:
140  case QXmlStreamReader::Characters:
141  case QXmlStreamReader::Comment:
142  case QXmlStreamReader::DTD:
143  case QXmlStreamReader::EntityReference:
144  case QXmlStreamReader::ProcessingInstruction:
145  break;
146  }
147  }
148  return errors;
149 }
150 
151 ErrorItem XmlReportV1::readError(QXmlStreamReader *reader)
152 {
153  ErrorItem item;
154  if (reader->name().toString() == ErrorElementName) {
155  QXmlStreamAttributes attribs = reader->attributes();
156  QString file = attribs.value("", FilenameAttribute).toString();
157  file = XmlReport::unquoteMessage(file);
158  QErrorPathItem e;
159  e.file = file;
160  e.line = attribs.value("", LineAttribute).toString().toUInt();
161  item.errorPath << e;
162  item.errorId = attribs.value("", IdAttribute).toString();
163  item.severity = GuiSeverity::fromString(attribs.value("", SeverityAttribute).toString());
164 
165  // NOTE: This duplicates the message to Summary-field. But since
166  // old XML format doesn't have separate summary and verbose messages
167  // we must add same message to both data so it shows up in GUI.
168  // Check if there is full stop and cut the summary to it.
169  QString summary = attribs.value("", MsgAttribute).toString();
170  const int ind = summary.indexOf('.');
171  if (ind != -1)
172  summary = summary.left(ind + 1);
173  item.summary = XmlReport::unquoteMessage(summary);
174  QString message = attribs.value("", MsgAttribute).toString();
175  item.message = XmlReport::unquoteMessage(message);
176  }
177  return item;
178 }
ErrorItem readError(QXmlStreamReader *reader)
Read and parse error item from XML stream.
QXmlStreamWriter * mXmlWriter
XML stream writer for writing the report in XML format.
Definition: xmlreportv1.h:89
unsigned int line
Definition: erroritem.h:57
QString file
Definition: erroritem.h:56
QList< QErrorPathItem > errorPath
Definition: erroritem.h:88
QXmlStreamReader * mXmlReader
XML stream reader for reading the report in XML format.
Definition: xmlreportv1.h:84
A class containing data for one error path item.
Definition: erroritem.h:52
static const char ResultElementName[]
Definition: xmlreportv1.cpp:30
XmlReportV1(const QString &filename)
Definition: xmlreportv1.cpp:38
virtual void writeHeader()
Write report header.
Definition: xmlreportv1.cpp:69
static const char SeverityAttribute[]
Definition: xmlreportv1.cpp:35
static QString toString(Severity::SeverityType severity)
Definition: erroritem.h:40
static const char FilenameAttribute[]
Definition: xmlreportv1.cpp:32
QString message
Definition: erroritem.h:86
virtual ~XmlReportV1()
Definition: xmlreportv1.cpp:45
virtual bool create()
Create the report (file).
Definition: xmlreportv1.cpp:51
virtual bool create()
Create the report (file).
Definition: report.cpp:34
bool inconclusive
Definition: erroritem.h:84
static const char ErrorElementName[]
Definition: xmlreportv1.cpp:31
static const char LineAttribute[]
Definition: xmlreportv1.cpp:33
QString errorId
Definition: erroritem.h:82
virtual bool open()
Open the existing report (file).
Definition: report.cpp:44
static QString unquoteMessage(const QString &message)
Unquote the message.
Definition: xmlreport.cpp:45
static const char MsgAttribute[]
Definition: xmlreportv1.cpp:36
QString summary
Definition: erroritem.h:85
bool open()
Open existing report file.
Definition: xmlreportv1.cpp:60
static Severity::SeverityType fromString(const QString &severity)
Definition: erroritem.h:44
virtual void writeError(const ErrorItem &error)
Write error to report.
Definition: xmlreportv1.cpp:82
static QString quoteMessage(const QString &message)
Quote the message.
Definition: xmlreport.cpp:34
virtual void writeFooter()
Write report footer.
Definition: xmlreportv1.cpp:76
virtual QList< ErrorItem > read()
Read contents of the report file.
Base class for XML report classes.
Definition: xmlreport.h:36
Severity::SeverityType severity
Definition: erroritem.h:83
QFile * getFile()
Get the file object where the report is written to.
Definition: report.cpp:60
static const char IdAttribute[]
Definition: xmlreportv1.cpp:34
A class containing error data for one error.
Definition: erroritem.h:70