|
| 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 | +} |
0 commit comments