Skip to content

Commit 544c647

Browse files
committed
json-parser: check for Coverity JSON format
The corresponding tree decoder has not yet been implemented.
1 parent 2bdcdce commit 544c647

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

json-parser.cc

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ class SimpleTreeDecoder: public AbstractTreeDecoder {
5050
KeyEventDigger keDigger;
5151
};
5252

53+
/// tree decoder of the Coverity JSON format
54+
class CovTreeDecoder: public AbstractTreeDecoder {
55+
public:
56+
virtual void readNode(Defect *def, const pt::ptree &node);
57+
};
58+
5359
struct JsonParser::Private {
5460
const std::string fileName;
5561
const bool silent;
@@ -107,6 +113,15 @@ void JsonParser::Private::dataError(const std::string &msg) {
107113
<< this->defNumber << ": " << msg << "\n";
108114
}
109115

116+
bool findChildOf(pt::ptree **pDst, pt::ptree &node, const char *key)
117+
{
118+
if (node.not_found() == node.find(key))
119+
return false;
120+
121+
*pDst = &node.get_child(key);
122+
return true;
123+
}
124+
110125
JsonParser::JsonParser(
111126
std::istream &input,
112127
const std::string &fileName,
@@ -117,18 +132,25 @@ JsonParser::JsonParser(
117132
// parse JSON
118133
read_json(input, d->root);
119134

120-
// get the defect list
121-
d->decoder = new SimpleTreeDecoder;
122-
d->defList = &d->root.get_child("defects");
123-
d->defIter = d->defList->begin();
124-
d->jsonValid = true;
125-
126-
// read scan properties if available
135+
// read scan properties if available (csdiff-native JSON format only)
127136
pt::ptree emp;
128137
pt::ptree scanNode =
129138
d->root.get_child_optional("scan").get_value_or(emp);
130139
BOOST_FOREACH(const pt::ptree::value_type &item, scanNode)
131140
d->scanProps[item.first] = item.second.data();
141+
142+
if (findChildOf(&d->defList, d->root, "defects"))
143+
// csdiff-native JSON format
144+
d->decoder = new SimpleTreeDecoder;
145+
else if (findChildOf(&d->defList, d->root, "issues"))
146+
// Coverity JSON format
147+
d->decoder = new CovTreeDecoder;
148+
else
149+
throw pt::ptree_error("unknown JSON format");
150+
151+
// initialize the traversal through the list of defects/issues
152+
d->defIter = d->defList->begin();
153+
d->jsonValid = true;
132154
}
133155
catch (pt::file_parser_error &e) {
134156
d->parseError(e.message(), e.line());
@@ -246,3 +268,15 @@ void SimpleTreeDecoder::readNode(
246268
// read annotation if available
247269
def->annotation = valueOf<std::string>(defNode, "annotation", "");
248270
}
271+
272+
void CovTreeDecoder::readNode(
273+
Defect *def,
274+
const pt::ptree &defNode)
275+
{
276+
// make sure the Defect structure is properly initialized
277+
(*def) = Defect();
278+
279+
def->checker = defNode.get<std::string>("checkerName");
280+
281+
throw pt::ptree_error("CovTreeDecoder has not yet been implemented");
282+
}

0 commit comments

Comments
 (0)