Skip to content

Commit

Permalink
Stricter verification of line numbers that warnings relate to
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-derevenetz authored and platisd committed Dec 4, 2023
1 parent 121c8f8 commit 4733885
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions run_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import yaml


def get_diff_lines_per_file(pr_files):
"""Generates and returns a set of affected line numbers for each file that has been modified
by the processed PR"""
def get_diff_line_ranges_per_file(pr_files):
"""Generates and returns a list of line ranges affected by the corresponding patch hunks for
each file that has been modified by the processed PR"""

def change_to_line_range(change):
split_change = change.split(",")
Expand Down Expand Up @@ -49,10 +49,9 @@ def change_to_line_range(change):
for tag in git_line_tags
]

result[file_name] = set()
for lines in [change_to_line_range(change) for change in changes]:
for line in lines:
result[file_name].add(line)
result[file_name] = []
for line_range in [change_to_line_range(change) for change in changes]:
result[file_name].append(line_range)

return result

Expand Down Expand Up @@ -112,7 +111,7 @@ def get_pull_request_comments(


def generate_review_comments(
clang_tidy_fixes, repository_root, diff_lines_per_file
clang_tidy_fixes, repository_root, diff_line_ranges_per_file
): # pylint: disable=too-many-locals,too-many-branches,too-many-statements
"""Generator of the Clang-Tidy review comments"""

Expand All @@ -123,6 +122,25 @@ def get_line_by_offset(repository_root, file_path, offset):

return source_file[:offset].count("\n") + 1

def validate_warning_applicability(
diff_line_ranges_per_file, file_path, start_line_num, end_line_num
):
assert end_line_num >= start_line_num

if file_path not in diff_line_ranges_per_file:
return False

for line_range in diff_line_ranges_per_file[file_path]:
assert line_range.step == 1

if (
line_range.start <= start_line_num < line_range.stop
and end_line_num < line_range.stop
):
return True

return False

def calculate_replacements_diff(repository_root, file_path, replacements):
# Apply the replacements in reverse order so that subsequent offsets are not shifted
replacements.sort(key=lambda item: (-item["Offset"]))
Expand Down Expand Up @@ -230,9 +248,8 @@ def generate_single_comment(

print(f"Processing '{diag_name}' at line {line_num:d} of {file_path}...")

if (
file_path in diff_lines_per_file
and line_num in diff_lines_per_file[file_path]
if validate_warning_applicability(
diff_line_ranges_per_file, file_path, line_num, line_num
):
yield generate_single_comment(
file_path, line_num, line_num, diag_name, diag_message_msg
Expand Down Expand Up @@ -307,9 +324,11 @@ def generate_single_comment(
f"Processing '{diag_name}' at lines {start_line_num:d}-{end_line_num:d} of {file_path}..."
)

if (
file_path in diff_lines_per_file
and start_line_num in diff_lines_per_file[file_path]
if validate_warning_applicability(
diff_line_ranges_per_file,
file_path,
start_line_num,
end_line_num,
):
yield generate_single_comment(
file_path,
Expand Down Expand Up @@ -347,9 +366,11 @@ def generate_single_comment(
f"Processing '{diag_name}' at lines {start_line_num:d}-{end_line_num:d} of {file_path}..."
)

if (
file_path in diff_lines_per_file
and start_line_num in diff_lines_per_file[file_path]
if validate_warning_applicability(
diff_line_ranges_per_file,
file_path,
start_line_num,
end_line_num,
):
yield generate_single_comment(
file_path,
Expand Down Expand Up @@ -533,7 +554,7 @@ def main():
":warning: `Clang-Tidy` found issue(s) with the introduced code"
)

diff_lines_per_file = get_diff_lines_per_file(
diff_line_ranges_per_file = get_diff_line_ranges_per_file(
get_pull_request_files(
github_api_url,
github_token,
Expand Down Expand Up @@ -564,7 +585,7 @@ def main():

review_comments = list(
generate_review_comments(
clang_tidy_fixes, args.repository_root + "/", diff_lines_per_file
clang_tidy_fixes, args.repository_root + "/", diff_line_ranges_per_file
)
)

Expand Down

0 comments on commit 4733885

Please sign in to comment.