Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json-parser: basic support for ShellCheck's JSON output #83

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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