Skip to content

Commit

Permalink
Allow clang_tidy results with no ruleId.
Browse files Browse the repository at this point in the history
Signed-off-by: Eli Benevedes <[email protected]>

Signed-off-by: Eli Benevedes <[email protected]>
  • Loading branch information
Eli Benevedes committed Nov 19, 2022
1 parent 16d5e4a commit 5d5e2ed
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions ament_clang_tidy/ament_clang_tidy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ def start_subprocess(full_cmd):

# Regex explanation:
# \s*\^? : Capture many spaces, then an optional `^`. clang_tidy output isn't great.
# (\/home\/^[^:]*) : Group capture. Everything from `/home/` up until a `:`.
# (/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('\\s*\^?\/home\/([^:]*):(\\d+):(\\d+): (?:warning:|error:|note:).*\\[(.*)\\]')
error_re = re.compile(r'\s*\^?(?:/home/|/root/|/src/)([^:]*):(\d+):(\d+): (?:warning:|error:|note:).*(?:\[(.*)\])?')

current_file = None
new_file = None
Expand Down Expand Up @@ -447,8 +447,12 @@ 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']))
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,
'helpUri': 'https://clang.llvm.org/extra/clang-tidy/checks/list.html',
Expand All @@ -463,15 +467,18 @@ def get_sarif_content(report, clang_tidy_version):
artifacts.append(artifact)

# Strip error message of rule ID
strip_error = re.compile("(.*) \[.*]")
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']
Expand All @@ -480,7 +487,6 @@ def get_sarif_content(report, clang_tidy_version):
error_without_rule = strip_error.match(error['error_msg'])

results_dict = {
'ruleId': rule_id,
'level': 'warning',
'kind': 'review',
'message': {
Expand All @@ -499,6 +505,10 @@ def get_sarif_content(report, clang_tidy_version):
}
}]
}

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

results.append(results_dict)

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

0 comments on commit 5d5e2ed

Please sign in to comment.