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-writer: print level key in SARIF output #81

Merged
merged 1 commit into from
Aug 27, 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
21 changes: 21 additions & 0 deletions src/json-writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ static void sarifEncodeMsg(PTree *pDst, const std::string& text)
pDst->put_child("message", msg);
}

static void sarifEncodeLevel(PTree *result, const std::string &event) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: fix bracket formatting inconsistency in some future commit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might consider using this one-liner in CI to catch this:

grep '^static .*) *{$' src/*

There is one more inconsistency in abstarct-parser.cc:

src/abstract-parser.cc:static inline std::unique_ptr<T> make_unique(InStream &input) {
src/json-writer.cc:static void sarifEncodeLevel(PTree *result, const std::string &event) {

Copy link
Member Author

@lzaoral lzaoral Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a custom .clang-format config to enforce code style is would be a better solution. Tracked in #86.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would mean more work than the proposed one-liner. But if we have a volunteer to work on this, why not :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not that hard, I've already done it few times. Additionally, such config file can be then used in all of our projects.

std::string level = event;

// cut the [...] suffix from event if present
size_t pos = event.find('[');
if (std::string::npos != pos)
level = event.substr(0U, pos);

// go through events that denote warning level
for (const char *str : {"error", "warning", "note"}) {
if (str == level) {
// encode in the output if matched
result->put<std::string>("level", level);
return;
}
}
}

static void sarifEncodeLoc(PTree *pLoc, const Defect &def, unsigned idx)
{
// location ID within the result
Expand Down Expand Up @@ -291,6 +309,9 @@ void SarifTreeEncoder::appendDef(const Defect &def)
// update CWE map
cweMap_[ruleId] = def.cwe;

// key event severity level
sarifEncodeLevel(&result, keyEvt.event);

// key event location
PTree loc;
sarifEncodeLoc(&loc, def, def.keyEventIdx);
Expand Down
Loading