Skip to content

Commit 5d5e2ed

Browse files
author
Eli Benevedes
committed
Allow clang_tidy results with no ruleId.
Signed-off-by: Eli Benevedes <[email protected]> Signed-off-by: Eli Benevedes <[email protected]>
1 parent 16d5e4a commit 5d5e2ed

File tree

1 file changed

+17
-7
lines changed
  • ament_clang_tidy/ament_clang_tidy

1 file changed

+17
-7
lines changed

ament_clang_tidy/ament_clang_tidy/main.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,12 @@ def start_subprocess(full_cmd):
216216

217217
# Regex explanation:
218218
# \s*\^? : Capture many spaces, then an optional `^`. clang_tidy output isn't great.
219-
# (\/home\/^[^:]*) : Group capture. Everything from `/home/` up until a `:`.
219+
# (/home/^[^:]*) : Group capture. Everything from `/home/` up until a `:`.
220220
# (\d+) : Group capture. Grabs line number.
221221
# (\d+) : Group capture. Grabs column number.
222222
# (?:warning:|error:|note:) : Non-capturing group. Matches warning, error, note.
223223
# \[(.*)\] : Matches and captures [<rule_name>]. Ignores any messages from clang_tidy without an ending [<rule_name>].
224-
error_re = re.compile('\\s*\^?\/home\/([^:]*):(\\d+):(\\d+): (?:warning:|error:|note:).*\\[(.*)\\]')
224+
error_re = re.compile(r'\s*\^?(?:/home/|/root/|/src/)([^:]*):(\d+):(\d+): (?:warning:|error:|note:).*(?:\[(.*)\])?')
225225

226226
current_file = None
227227
new_file = None
@@ -447,8 +447,12 @@ def get_sarif_content(report, clang_tidy_version):
447447
rules = sarif['runs'][0]['tool']['driver']['rules']
448448
for filename in sorted(report.keys()):
449449
for error in report[filename]:
450-
description, rule_id = filter(
451-
None, re.split('\[|\]', error['error_msg']))
450+
try:
451+
_, rule_id = filter(
452+
None, re.split('\[|\]', error['error_msg']))
453+
except ValueError:
454+
# Rule string not found (clang_tidy doesn't always have a [rule-id] post-fixing the result), so skip
455+
continue
452456
new_rule = {
453457
'id': rule_id,
454458
'helpUri': 'https://clang.llvm.org/extra/clang-tidy/checks/list.html',
@@ -463,15 +467,18 @@ def get_sarif_content(report, clang_tidy_version):
463467
artifacts.append(artifact)
464468

465469
# Strip error message of rule ID
466-
strip_error = re.compile("(.*) \[.*]")
470+
strip_error = re.compile("(.*)( \[.*])?")
467471

468472
# Populate the results of the analysis (issues discovered)
469473
results = sarif['runs'][0]['results']
470474
for filename in sorted(report.keys()):
471475
errors = report[filename]
472476
for error in errors:
473477
# Get the symbolic rule name (it's the part in brackets)
474-
_, rule_id = filter(None, re.split('\[|\]', error['error_msg']))
478+
try:
479+
_, rule_id = filter(None, re.split('\[|\]', error['error_msg']))
480+
except ValueError:
481+
rule_id = ""
475482

476483
start_line = error['line_no']
477484
start_column = error['offset_in_line']
@@ -480,7 +487,6 @@ def get_sarif_content(report, clang_tidy_version):
480487
error_without_rule = strip_error.match(error['error_msg'])
481488

482489
results_dict = {
483-
'ruleId': rule_id,
484490
'level': 'warning',
485491
'kind': 'review',
486492
'message': {
@@ -499,6 +505,10 @@ def get_sarif_content(report, clang_tidy_version):
499505
}
500506
}]
501507
}
508+
509+
if not rule_id == "":
510+
results_dict['ruleId'] = rule_id
511+
502512
results.append(results_dict)
503513

504514
return json.dumps(sarif, indent=2)

0 commit comments

Comments
 (0)