Skip to content

Commit 5e0601e

Browse files
committed
sarif: do not include comments in CodeFlows
... but stash them into `relatedLocations`, so that they do not disturb while visualizing the results on Github.
1 parent d57f93b commit 5e0601e

9 files changed

+7753
-6
lines changed

src/json-parser.cc

+31-3
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,34 @@ static void sarifReadLocation(DefEvent *pEvt, const pt::ptree &loc)
492492
}
493493
}
494494

495-
static void sarifReadMsg(std::string *pDst, const pt::ptree &node)
495+
static bool sarifReadMsg(std::string *pDst, const pt::ptree &node)
496496
{
497497
const pt::ptree *msgNode;
498-
if (findChildOf(&msgNode, node, "message"))
499-
*pDst = valueOf<std::string>(*msgNode, "text", "<unknown>");
498+
if (!findChildOf(&msgNode, node, "message"))
499+
return false;
500+
501+
*pDst = valueOf<std::string>(*msgNode, "text", "<unknown>");
502+
return true;
503+
}
504+
505+
static void sarifReadComments(Defect *pDef, const pt::ptree &relatedLocs)
506+
{
507+
for (const auto &item : relatedLocs) {
508+
const pt::ptree &loc = item.second;
509+
510+
DefEvent tmp;
511+
sarifReadLocation(&tmp, loc);
512+
if (!tmp.fileName.empty())
513+
// location info available --> not a csdiff-encoded comment
514+
continue;
515+
516+
DefEvent evt("#");
517+
if (!sarifReadMsg(&evt.msg, loc))
518+
continue;
519+
520+
evt.verbosityLevel = 1;
521+
pDef->events.push_back(evt);
522+
}
500523
}
501524

502525
static void sarifReadCodeFlow(Defect *pDef, const pt::ptree &cf)
@@ -598,5 +621,10 @@ bool SarifTreeDecoder::readNode(
598621
if (findChildOf(&cf, defNode, "codeFlows"))
599622
sarifReadCodeFlow(def, *cf);
600623

624+
// read comments if available
625+
const pt::ptree *relatedLocs;
626+
if (findChildOf(&relatedLocs, defNode, "relatedLocations"))
627+
sarifReadComments(def, *relatedLocs);
628+
601629
return true;
602630
}

src/json-writer.cc

+22-3
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,17 @@ static void sarifEncodeLoc(PTree *pLoc, const Defect &def, unsigned idx)
225225
pLoc->put_child("physicalLocation", locPhy);
226226
}
227227

228+
static void sarifEncodeComment(PTree *pDst, const Defect &def, unsigned idx)
229+
{
230+
PTree comment;
231+
232+
// needed for Github to see the SARIF data as valid
233+
sarifEncodeLoc(&comment, def, idx);
234+
235+
sarifEncodeMsg(&comment, def.events[idx].msg);
236+
appendNode(pDst, comment);
237+
}
238+
228239
static void sarifEncodeEvt(PTree *pDst, const Defect &def, unsigned idx)
229240
{
230241
const DefEvent &evt = def.events[idx];
@@ -275,9 +286,13 @@ void SarifTreeEncoder::appendDef(const Defect &def)
275286
sarifEncodeMsg(&result, keyEvt.msg);
276287

277288
// other events
278-
PTree flowLocs;
279-
for (unsigned i = 0; i < def.events.size(); ++i)
280-
sarifEncodeEvt(&flowLocs, def, i);
289+
PTree flowLocs, relatedLocs;
290+
for (unsigned i = 0; i < def.events.size(); ++i) {
291+
if (def.events[i].event == "#")
292+
sarifEncodeComment(&relatedLocs, def, i);
293+
else
294+
sarifEncodeEvt(&flowLocs, def, i);
295+
}
281296

282297
// locations
283298
PTree tf;
@@ -294,6 +309,10 @@ void SarifTreeEncoder::appendDef(const Defect &def)
294309
appendNode(&cfList, cf);
295310
result.put_child("codeFlows", cfList);
296311

312+
if (!relatedLocs.empty())
313+
// our stash for comments
314+
result.put_child("relatedLocations", relatedLocs);
315+
297316
// append the `result` object to the `results` array
298317
appendNode(&results_, result);
299318
}

tests/csgrep/85-sarif-writer-args.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--mode=sarif

0 commit comments

Comments
 (0)