@@ -50,6 +50,12 @@ class SimpleTreeDecoder: public AbstractTreeDecoder {
50
50
KeyEventDigger keDigger;
51
51
};
52
52
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
+
53
59
struct JsonParser ::Private {
54
60
const std::string fileName;
55
61
const bool silent;
@@ -107,6 +113,15 @@ void JsonParser::Private::dataError(const std::string &msg) {
107
113
<< this ->defNumber << " : " << msg << " \n " ;
108
114
}
109
115
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
+
110
125
JsonParser::JsonParser (
111
126
std::istream &input,
112
127
const std::string &fileName,
@@ -117,18 +132,25 @@ JsonParser::JsonParser(
117
132
// parse JSON
118
133
read_json (input, d->root );
119
134
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)
127
136
pt::ptree emp;
128
137
pt::ptree scanNode =
129
138
d->root .get_child_optional (" scan" ).get_value_or (emp);
130
139
BOOST_FOREACH (const pt::ptree::value_type &item, scanNode)
131
140
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 ;
132
154
}
133
155
catch (pt::file_parser_error &e) {
134
156
d->parseError (e.message (), e.line ());
@@ -246,3 +268,15 @@ void SimpleTreeDecoder::readNode(
246
268
// read annotation if available
247
269
def->annotation = valueOf<std::string>(defNode, " annotation" , " " );
248
270
}
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