Skip to content

Commit

Permalink
json-parser: basic support for ShellCheck's JSON output
Browse files Browse the repository at this point in the history
... which is produced by `shellcheck --format=json ...`

Fixes: #75
  • Loading branch information
kdudka committed Sep 26, 2022
1 parent ceb9b62 commit 4e08114
Show file tree
Hide file tree
Showing 5 changed files with 2,115 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/json-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ class GccTreeDecoder: public AbstractTreeDecoder {
const GccPostProcessor postProc;
};

/// tree decoder of the JSON format produced by ShellCheck
class ShellCheckTreeDecoder: public AbstractTreeDecoder {
public:
bool readNode(Defect *def, pt::ptree::const_iterator defIter) override;

private:
const GccPostProcessor postProc;
};

struct JsonParser::Private {
InStream &input;
AbstractTreeDecoder *decoder = nullptr;
Expand Down Expand Up @@ -168,7 +177,11 @@ JsonParser::JsonParser(InStream &input):
else if (findChildOf(&node, d->root, "runs"))
// SARIF format
d->decoder = new SarifTreeDecoder;
else if (findChildOf(&node, d->root, "comments"))
// ShellCheck JSON format
d->decoder = new ShellCheckTreeDecoder;
else if (first.not_found() != first.find("kind"))
// GCC JSON format
d->decoder = new GccTreeDecoder;
else
throw pt::ptree_error("unknown JSON format");
Expand Down Expand Up @@ -781,3 +794,49 @@ bool GccTreeDecoder::readNode(Defect *def, pt::ptree::const_iterator defIter)

return true;
}

static bool scReadEvent(DefEvent *pEvt, const pt::ptree &evtNode)
{
using std::string;

// read level (error, warning, note)
string &evtName = pEvt->event;
evtName = valueOf<string>(evtNode, "level", "");
if (evtName.empty())
return false;

// read location
pEvt->fileName = valueOf<string>(evtNode, "file", "<unknown>");
pEvt->line = valueOf<int> (evtNode, "line", 0);
pEvt->column = valueOf<int> (evtNode, "byte-column", 0);

// read message
pEvt->msg = valueOf<string>(evtNode, "message", "<unknown>");

// append [SC...] if available
const string code = valueOf<string>(evtNode, "code", "");
if (!code.empty())
pEvt->msg += " [SC" + code + "]";

return true;
}

bool ShellCheckTreeDecoder::readNode(
Defect *def,
pt::ptree::const_iterator defIter)
{
*def = Defect("SHELLCHECK_WARNING");
const pt::ptree &defNode = defIter->second;

// read key event
def->events.push_back(DefEvent());
if (!scReadEvent(&def->events.back(), defNode))
return false;

// TODO: go through fix/replacements nodes

// apply post-processing rules
this->postProc.apply(def);

return true;
}
1 change: 1 addition & 0 deletions tests/csgrep/98-json-parser-shellcheck-args.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--mode=json
Loading

0 comments on commit 4e08114

Please sign in to comment.