Skip to content

Commit 64441e1

Browse files
committed
json-parser: optionally read code flow from SARIF
1 parent 17b9ed2 commit 64441e1

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/json-parser.cc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,58 @@ static void sarifReadMsg(std::string *pDst, const pt::ptree &node)
457457
*pDst = valueOf<std::string>(*msgNode, "text", "<unknown>");
458458
}
459459

460+
static void sarifReadCodeFlow(Defect *pDef, const pt::ptree &cf)
461+
{
462+
const pt::ptree *tf;
463+
if ((1U != cf.size())
464+
|| !findChildOf(&tf, cf.begin()->second, "threadFlows"))
465+
return;
466+
467+
const pt::ptree *locs;
468+
if (1U != tf->size()
469+
|| !findChildOf(&locs, tf->begin()->second, "locations"))
470+
return;
471+
472+
TEvtList events;
473+
unsigned keyEventIdx = 0U;
474+
475+
// read the full list of events
476+
for (const auto &item : *locs) {
477+
const pt::ptree &tfLoc = item.second;
478+
479+
const pt::ptree *kindList;
480+
if (!findChildOf(&kindList, tfLoc, "kinds") || (1U != kindList->size()))
481+
// not the format that csdiff produces
482+
return;
483+
484+
// append a new event of the specified kind
485+
const auto evtName = kindList->begin()->second.data();
486+
events.push_back(DefEvent(evtName));
487+
DefEvent &evt = events.back();
488+
489+
evt.verbosityLevel = valueOf<int>(tfLoc, "nestingLevel", 1);
490+
if (!evt.verbosityLevel)
491+
// update key event
492+
keyEventIdx = events.size() - 1U;
493+
494+
const pt::ptree *loc;
495+
if (!findChildOf(&loc, tfLoc, "location"))
496+
// location info missing
497+
return;
498+
499+
sarifReadLocation(&evt, *loc);
500+
sarifReadMsg(&evt.msg, *loc);
501+
}
502+
503+
if (events.size() <= 1U)
504+
// we failed to read more than one event
505+
return;
506+
507+
// update the list of events
508+
events.swap(pDef->events);
509+
pDef->keyEventIdx = keyEventIdx;
510+
}
511+
460512
bool SarifTreeDecoder::readNode(
461513
Defect *def,
462514
pt::ptree::const_iterator defIter)
@@ -494,5 +546,10 @@ bool SarifTreeDecoder::readNode(
494546
sarifReadLocation(&keyEvent, locs->begin()->second);
495547
sarifReadMsg(&keyEvent.msg, defNode);
496548

549+
// read code flow if available
550+
const pt::ptree *cf;
551+
if (findChildOf(&cf, defNode, "codeFlows"))
552+
sarifReadCodeFlow(def, *cf);
553+
497554
return true;
498555
}

0 commit comments

Comments
 (0)