2023-03-12 10:22:09 +01:00
|
|
|
import re
|
|
|
|
import subprocess
|
2023-04-18 18:48:18 +02:00
|
|
|
import sys
|
2023-03-12 10:22:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
def clang_format(file_path):
|
|
|
|
command = ["clang-format", "--dry-run", file_path]
|
|
|
|
try:
|
2023-04-18 22:27:34 +02:00
|
|
|
output = subprocess.check_output(command, stderr=subprocess.STDOUT, text=True)
|
2023-03-12 10:22:09 +01:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
output = e.output
|
2023-04-18 18:48:18 +02:00
|
|
|
except FileNotFoundError as e:
|
|
|
|
print(f"Command not found: {e.filename}", file=sys.stderr)
|
|
|
|
exit(1)
|
2023-03-12 10:22:09 +01:00
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
def parse_clang_format_output(output):
|
2023-04-18 22:27:34 +02:00
|
|
|
error_pattern = (
|
|
|
|
r"(?P<filename>.+):(?P<line_number>\d+):(?P<column_number>\d+):"
|
|
|
|
+ r" warning: code should be clang-formatted \[(?P<warning_message>.+)\]"
|
|
|
|
)
|
2023-03-12 10:22:09 +01:00
|
|
|
error_matches = re.finditer(error_pattern, output)
|
|
|
|
|
|
|
|
if error_matches:
|
|
|
|
errors = []
|
|
|
|
for error_match in error_matches:
|
|
|
|
filename = error_match.group("filename")
|
|
|
|
line_number = int(error_match.group("line_number"))
|
|
|
|
column_number = int(error_match.group("column_number"))
|
|
|
|
warning_message = error_match.group("warning_message")
|
|
|
|
|
2023-04-18 22:27:34 +02:00
|
|
|
errors.append(
|
|
|
|
{
|
|
|
|
"filename": filename,
|
|
|
|
"line_number": line_number,
|
|
|
|
"column_number": column_number,
|
|
|
|
"warning_message": warning_message[1:],
|
|
|
|
}
|
|
|
|
)
|
2023-03-12 10:22:09 +01:00
|
|
|
|
|
|
|
return errors
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
clang_format_output = clang_format("tests/1.c")
|
|
|
|
parsed_output = parse_clang_format_output(clang_format_output)
|
|
|
|
|
|
|
|
if parsed_output:
|
|
|
|
for error in parsed_output:
|
|
|
|
print(
|
2023-04-18 22:27:34 +02:00
|
|
|
f"Warning dans {error['filename']} à la ligne {error['line_number']},"
|
|
|
|
+ f" caractère {error['column_number']} : {error['warning_message']}"
|
|
|
|
)
|
2023-03-12 10:22:09 +01:00
|
|
|
else:
|
|
|
|
print("No warnings found.")
|