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

Fix issues with parsing and SARIF generation for SpaceROS #419

Open
wants to merge 4 commits into
base: spaceros
Choose a base branch
from
Open
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
48 changes: 34 additions & 14 deletions ament_clang_tidy/ament_clang_tidy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,14 @@ def start_subprocess(full_cmd):
for filename in files:
report[filename] = []

error_re = re.compile('(/.*?\\.(?:%s)):(\\d+):(\\d+): (?:warning:|error:)' %
'|'.join(extensions))
Ronoman marked this conversation as resolved.
Show resolved Hide resolved
# Regex explanation:
# \s*\^? : Capture many spaces, then an optional `^`. clang_tidy output isn't great.
nuclearsandwich marked this conversation as resolved.
Show resolved Hide resolved
# (/home/^[^:]*) : Group capture. Everything from `/home/` up until a `:`.
# (\d+) : Group capture. Grabs line number.
# (\d+) : Group capture. Grabs column number.
# (?:warning:|error:|note:) : Non-capturing group. Matches warning, error, note.
# \[(.*)\] : Matches and captures [<rule_name>]. Ignores any messages from clang_tidy without an ending [<rule_name>].
error_re = re.compile(r'\s*\^?(?:/home/|/root/|/src/)([^:]*):(\d+):(\d+): (?:warning:|error:|note:).*(?:\[(.*)\])?')

current_file = None
new_file = None
Expand Down Expand Up @@ -441,40 +447,50 @@ def get_sarif_content(report, clang_tidy_version):
rules = sarif['runs'][0]['tool']['driver']['rules']
for filename in sorted(report.keys()):
for error in report[filename]:
description, rule_id = filter(
None, re.split('\[|\]', error['error_msg']))
rules.append({
try:
_, rule_id = filter(
None, re.split('\[|\]', error['error_msg']))
except ValueError:
# Rule string not found (clang_tidy doesn't always have a [rule-id] post-fixing the result), so skip
continue
new_rule = {
'id': rule_id,
'shortDescription': {
'text': description.rstrip()
},
'helpUri': 'https://clang.llvm.org/extra/clang-tidy/checks/list.html',
})
}
if new_rule not in rules:
rules.append(new_rule)
Ronoman marked this conversation as resolved.
Show resolved Hide resolved

# Populate the artifact information (source files analyzed)
artifacts = sarif['runs'][0]['artifacts']
for filename in sorted(report.keys()):
artifact = {'location': {'uri': filename}}
artifacts.append(artifact)

# Strip error message of rule ID
strip_error = re.compile("(.*)( \[.*])?")

# Populate the results of the analysis (issues discovered)
results = sarif['runs'][0]['results']
for filename in sorted(report.keys()):
errors = report[filename]
for error in errors:
# Get the symbolic rule name (it's the part in brackets)
_, rule_id = filter(None, re.split('\[|\]', error['error_msg']))
try:
_, rule_id = filter(None, re.split('\[|\]', error['error_msg']))
except ValueError:
rule_id = ""

start_line = error['line_no']
start_column = error['offset_in_line']
index = artifacts.index({'location': {'uri': filename}})

error_without_rule = strip_error.match(error['error_msg'])

results_dict = {
'ruleId': rule_id,
'level': 'warning',
'kind': 'review',
'message': {
'text': error['code_correct_rec']
'text': error_without_rule.group(1)
},
'locations': [{
'physicalLocation': {
Expand All @@ -483,12 +499,16 @@ def get_sarif_content(report, clang_tidy_version):
'index': index
},
'region': {
'startLine': start_line,
'startColumn': start_column
'startLine': int(start_line),
'startColumn': int(start_column)
}
}
}]
}

if not rule_id == "":
results_dict['ruleId'] = rule_id

results.append(results_dict)

return json.dumps(sarif, indent=2)
Expand Down