Skip to content

Commit

Permalink
specific fix for special robin_hood.h
Browse files Browse the repository at this point in the history
Signed-off-by: M. Fatih Cırıt <[email protected]>
  • Loading branch information
xmfcx committed Dec 3, 2024
1 parent 2fcd264 commit a413285
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
112 changes: 112 additions & 0 deletions fix_includes_pre-commit.py
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

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

add_includes_to_file has a cyclomatic complexity of 11, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

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()
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
#include <string>
#include <type_traits>
#include <utility>
#include <iostream>
#include <tuple>
#if __cplusplus >= 201703L
#include <string_view>
Expand All @@ -87,7 +86,7 @@
#ifdef ROBIN_HOOD_TRACE_ENABLED
#include <iostream>
#define ROBIN_HOOD_TRACE(...) \
std::cout << __FUNCTION__ << "@" << __LINE__ << ": " << __VA_ARGS__ << std::endl;
std::cout << __FUNCTION__ << "@" << __LINE__ << ": " << __VA_ARGS__ << std::endl; // NOLINT
#else
#define ROBIN_HOOD_TRACE(x)
#endif
Expand Down

0 comments on commit a413285

Please sign in to comment.