Skip to content

Commit 110d8a5

Browse files
committed
xml-parser: new module for parsing XML
Only recognition (but not parsing) of valgrind's XML format implemented.
1 parent c31e0f0 commit 110d8a5

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ add_library(cs STATIC
3939
parser-common.cc
4040
shared-string.cc
4141
version.cc
42+
xml-parser.cc
4243
)
4344

4445
# link cslib.a and boost libraries

src/abstract-parser.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "csparser.hh"
2323
#include "gcc-parser.hh"
2424
#include "json-parser.hh"
25+
#include "xml-parser.hh"
2526

2627
AbstractParser* createParser(InStream &input)
2728
{
@@ -37,6 +38,10 @@ AbstractParser* createParser(InStream &input)
3738
// JSON
3839
return new JsonParser(input);
3940

41+
case '<':
42+
// XML
43+
return new XmlParser(input);
44+
4045
case 'E':
4146
case '#':
4247
// Coverity

src/xml-parser.cc

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (C) 2011-2021 Red Hat, Inc.
3+
*
4+
* This file is part of csdiff.
5+
*
6+
* csdiff is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* any later version.
10+
*
11+
* csdiff is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "xml-parser.hh"
21+
22+
#include "abstract-tree.hh"
23+
24+
#include <boost/property_tree/xml_parser.hpp>
25+
26+
/// tree decoder for valgrind XML output
27+
class ValgrindTreeDecoder: public AbstractTreeDecoder {
28+
public:
29+
virtual bool readNode(Defect *def, pt::ptree::const_iterator defIter);
30+
31+
virtual void readRoot(
32+
const pt::ptree **pDefList,
33+
const pt::ptree *root);
34+
};
35+
36+
void ValgrindTreeDecoder::readRoot(
37+
const pt::ptree **pDefList,
38+
const pt::ptree *root)
39+
{
40+
// TODO: read path to the binary and command-line args
41+
*pDefList = root;
42+
}
43+
44+
bool ValgrindTreeDecoder::readNode(Defect *pDef, pt::ptree::const_iterator defIter)
45+
{
46+
// TODO
47+
(void) pDef;
48+
(void) defIter;
49+
assert(!"implemented");
50+
return false;
51+
}
52+
53+
struct XmlParser::Private {
54+
InStream &input;
55+
AbstractTreeDecoder *decoder = nullptr;
56+
pt::ptree root;
57+
const pt::ptree *defList = nullptr;
58+
pt::ptree::const_iterator defIter = root.end();
59+
60+
Private(InStream &input):
61+
input(input)
62+
{
63+
}
64+
65+
~Private()
66+
{
67+
delete this->decoder;
68+
}
69+
};
70+
71+
XmlParser::XmlParser(InStream &input):
72+
d(new Private(input))
73+
{
74+
try {
75+
// parse XML
76+
read_xml(d->input.str(), d->root);
77+
78+
// recognize inner format of the XML document
79+
pt::ptree *node = nullptr;
80+
if (findChildOf(&node, d->root, "valgrindoutput"))
81+
// valgrind XML format
82+
d->decoder = new ValgrindTreeDecoder;
83+
else
84+
throw pt::ptree_error("unknown XML format");
85+
86+
// process the root node
87+
d->decoder->readRoot(&d->defList, node);
88+
89+
// initialize the traversal through the list of defects/issues
90+
d->defIter = d->defList->begin();
91+
}
92+
catch (pt::file_parser_error &e) {
93+
d->input.handleError(e.message(), e.line());
94+
}
95+
catch (pt::ptree_error &e) {
96+
d->input.handleError(e.what());
97+
}
98+
}
99+
100+
XmlParser::~XmlParser()
101+
{
102+
delete d;
103+
}
104+
105+
bool XmlParser::hasError() const
106+
{
107+
return d->input.anyError();
108+
}
109+
110+
bool XmlParser::getNext(Defect *pDef)
111+
{
112+
if (!d->defList)
113+
// initialization failed, error has already been reported
114+
return false;
115+
116+
while (d->defList->end() != d->defIter) {
117+
// get the current node and move to the next one
118+
try {
119+
if (d->decoder->readNode(pDef, d->defIter++))
120+
return true;
121+
}
122+
catch (pt::ptree_error &e) {
123+
d->input.handleError(e.what());
124+
}
125+
}
126+
127+
// EOF
128+
return false;
129+
}

src/xml-parser.hh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2021 Red Hat, Inc.
3+
*
4+
* This file is part of csdiff.
5+
*
6+
* csdiff is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* any later version.
10+
*
11+
* csdiff is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef H_GUARD_XML_PARSER_H
21+
#define H_GUARD_XML_PARSER_H
22+
23+
#include "abstract-parser.hh"
24+
25+
class XmlParser: public AbstractParser {
26+
public:
27+
XmlParser(InStream &input);
28+
29+
virtual ~XmlParser();
30+
virtual bool getNext(Defect *);
31+
virtual bool hasError() const;
32+
33+
private:
34+
XmlParser(const Parser &);
35+
XmlParser& operator=(const Parser &);
36+
37+
struct Private;
38+
Private *d;
39+
};
40+
41+
#endif /* H_GUARD_XML_PARSER_H */

0 commit comments

Comments
 (0)