@@ -136,22 +136,46 @@ class SarifTreeEncoder: public AbstractTreeEncoder {
136
136
void writeTo (std::ostream &) override ;
137
137
138
138
private:
139
+ void serializeCweMap ();
140
+
141
+ typedef std::map<std::string, int > TCweMap;
142
+ TCweMap cweMap_;
139
143
TScanProps scanProps_;
140
- PTree run0_ ;
144
+ PTree driver_ ;
141
145
PTree results_;
142
146
};
143
147
144
148
SarifTreeEncoder::SarifTreeEncoder ()
145
149
{
146
150
// mandatory: tool/driver
147
- PTree driver;
148
- driver.put <std::string>(" name" , " csdiff" );
149
- driver.put <std::string>(" version" , CS_VERSION);
150
- driver.put <std::string>(" informationUri" ,
151
+ driver_.put <std::string>(" name" , " csdiff" );
152
+ driver_.put <std::string>(" version" , CS_VERSION);
153
+ driver_.put <std::string>(" informationUri" ,
151
154
" https://github.com/csutils/csdiff" );
152
- PTree tool;
153
- tool.put_child (" driver" , driver);
154
- run0_.put_child (" tool" , tool);
155
+ }
156
+
157
+ void SarifTreeEncoder::serializeCweMap ()
158
+ {
159
+ PTree ruleList;
160
+
161
+ for (const auto &item : cweMap_) {
162
+ PTree rule;
163
+ const auto &id = item.first ;
164
+ rule.put <std::string>(" id" , id);
165
+
166
+ PTree cweList;
167
+ const auto cwe = item.second ;
168
+ const auto cweStr = " CWE-" + std::to_string (cwe);
169
+ appendNode (&cweList, PTree (cweStr));
170
+
171
+ PTree props;
172
+ props.put_child (" cwe" , cweList);
173
+ rule.put_child (" properties" , props);
174
+
175
+ appendNode (&ruleList, rule);
176
+ }
177
+
178
+ driver_.put_child (" rules" , ruleList);
155
179
}
156
180
157
181
void SarifTreeEncoder::importScanProps (const TScanProps &scanProps)
@@ -228,6 +252,9 @@ void SarifTreeEncoder::appendDef(const Defect &def)
228
252
// checker (FIXME: suboptimal mapping to SARIF)
229
253
const std::string ruleId = def.checker + " : " + keyEvt.event ;
230
254
result.put <std::string>(" ruleId" , ruleId);
255
+ if (def.cwe )
256
+ // update CWE map
257
+ cweMap_[ruleId] = def.cwe ;
231
258
232
259
// key event location
233
260
PTree loc;
@@ -285,13 +312,23 @@ void SarifTreeEncoder::writeTo(std::ostream &str)
285
312
root.put_child (" inlineExternalProperties" , propsList);
286
313
}
287
314
315
+ if (!cweMap_.empty ())
316
+ // needs to run before we pick driver_
317
+ this ->serializeCweMap ();
318
+
319
+ PTree tool;
320
+ tool.put_child (" driver" , driver_);
321
+
322
+ PTree run0;
323
+ run0.put_child (" tool" , tool);
324
+
288
325
if (!results_.empty ())
289
326
// results
290
- run0_ .put_child (" results" , results_);
327
+ run0 .put_child (" results" , results_);
291
328
292
329
// mandatory: runs
293
330
PTree runs;
294
- appendNode (&runs, run0_ );
331
+ appendNode (&runs, run0 );
295
332
root.put_child (" runs" , runs);
296
333
297
334
// encode as JSON
0 commit comments