-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
specific fix for special robin_hood.h
Signed-off-by: M. Fatih Cırıt <[email protected]>
- Loading branch information
Showing
2 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import subprocess | ||
import re | ||
import os | ||
|
||
def run_pre_commit_command(): | ||
subdirectory = "." # example: perception | ||
cmd = f"git ls-files -- {subdirectory} | xargs pre-commit run cpplint --files" | ||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True) | ||
return result.stdout | ||
|
||
def parse_pre_commit_output(output): | ||
fixes = [] | ||
fixes_dict = {} | ||
|
||
# Regex pattern to match the error lines with include suggestions | ||
issue_pattern = re.compile( | ||
r"^(?P<file_path>.+?):(?P<line_number>\d+):\s+Add #include <(?P<include>.+?)> for .+\s+\[.+\]" | ||
) | ||
|
||
for line in output.splitlines(): | ||
issue_match = issue_pattern.match(line) | ||
if issue_match: | ||
file_path = issue_match.group('file_path') | ||
line_number = int(issue_match.group('line_number')) | ||
include_name = issue_match.group('include') | ||
|
||
if file_path not in fixes_dict: | ||
fixes_dict[file_path] = { | ||
"fix": { | ||
"includes": set(), | ||
"path": file_path, | ||
"lines": [] | ||
} | ||
} | ||
fixes_dict[file_path]["fix"]["includes"].add(include_name) | ||
fixes_dict[file_path]["fix"]["lines"].append(line_number) | ||
|
||
# Convert includes set to list and collect fixes | ||
for fix in fixes_dict.values(): | ||
fix["fix"]["includes"] = list(fix["fix"]["includes"]) | ||
fixes.append(fix) | ||
|
||
return fixes | ||
|
||
def find_include_block(lines): | ||
include_pattern = re.compile(r'^\s*#include\s+[<"].+[>"].*') | ||
start_idx = None | ||
end_idx = None | ||
|
||
for idx, line in enumerate(lines): | ||
if include_pattern.match(line): | ||
if start_idx is None: | ||
start_idx = idx | ||
end_idx = idx | ||
elif start_idx is not None: | ||
# We've passed the include block | ||
break | ||
return start_idx, end_idx | ||
|
||
def add_includes_to_file(file_path, includes): | ||
# Read the contents of the file if it exists | ||
if os.path.exists(file_path): | ||
with open(file_path, 'r') as file: | ||
lines = file.readlines() | ||
else: | ||
print(f"Could not read file: {file_path}") | ||
return | ||
|
||
# Find the include block in the file | ||
start_idx, end_idx = find_include_block(lines) | ||
if start_idx is not None: | ||
# Extract existing includes to avoid duplicates | ||
existing_includes = set() | ||
include_line_pattern = re.compile(r'^\s*#include\s+[<"]([^>"]+)[>"].*') | ||
for line in lines[start_idx:end_idx+1]: | ||
match = include_line_pattern.match(line) | ||
if match: | ||
existing_includes.add(match.group(1)) | ||
|
||
# Determine which includes are missing | ||
missing_includes = [inc for inc in includes if inc not in existing_includes] | ||
|
||
if missing_includes: | ||
# Prepare new include directives | ||
new_include_lines = [f'#include <{inc}>\n' for inc in missing_includes] | ||
# Insert missing includes after the existing includes | ||
insertion_point = end_idx + 1 | ||
lines[insertion_point:insertion_point] = new_include_lines | ||
# Optionally, sort the include block (uncomment the following lines if needed) | ||
# all_includes = lines[start_idx:end_idx+len(new_include_lines)+1] | ||
# all_includes_sorted = sorted(all_includes, key=lambda s: s.strip()) | ||
# lines[start_idx:end_idx+len(new_include_lines)+1] = all_includes_sorted | ||
# Write back to the file | ||
with open(file_path, 'w') as file: | ||
file.writelines(lines) | ||
print(f"Added includes {missing_includes} to {file_path}") | ||
else: | ||
print(f"All includes already present in {file_path}") | ||
else: | ||
print(f"No #include directives found in {file_path}. Cannot add includes.") | ||
Check warning on line 100 in fix_includes_pre-commit.py
|
||
|
||
def main(): | ||
output = run_pre_commit_command() | ||
parsed_fixes = parse_pre_commit_output(output) | ||
for fix in parsed_fixes: | ||
print(fix) | ||
file_path = fix["fix"]["path"] | ||
includes_to_add = fix["fix"]["includes"] | ||
add_includes_to_file(file_path, includes_to_add) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters