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
|
|
|
|
2023-05-29 11:09:40 +02:00
|
|
|
from src.errors import ClangError
|
2023-05-29 17:00:06 +02:00
|
|
|
from src.utils import analyze_args
|
2023-05-29 11:09:40 +02:00
|
|
|
|
2023-03-12 10:22:09 +01:00
|
|
|
|
2023-05-29 17:17:24 +02:00
|
|
|
def clang_format(file_path) -> str:
|
2023-03-12 10:22:09 +01:00
|
|
|
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:
|
2023-05-29 11:08:11 +02:00
|
|
|
print(f"Commande non trouvé : {e.filename}", file=sys.stderr)
|
|
|
|
print(
|
|
|
|
"clang est requis pour utilisé ce programme, installé-le via "
|
|
|
|
f"votre gestionnaire de paquet : {e.filename}",
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2023-04-18 18:48:18 +02:00
|
|
|
exit(1)
|
2023-03-12 10:22:09 +01:00
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
2023-05-29 17:00:06 +02:00
|
|
|
def parse_clang_format_output(output) -> list[ClangError]:
|
2023-04-18 22:27:34 +02:00
|
|
|
error_pattern = (
|
|
|
|
r"(?P<filename>.+):(?P<line_number>\d+):(?P<column_number>\d+):"
|
2023-05-29 11:48:54 +02:00
|
|
|
r" warning: code should be clang-formatted \[(?P<warning_message>.+)\]"
|
2023-04-18 22:27:34 +02:00
|
|
|
)
|
2023-03-12 10:22:09 +01:00
|
|
|
error_matches = re.finditer(error_pattern, output)
|
|
|
|
|
2023-05-29 17:00:06 +02:00
|
|
|
errors = []
|
|
|
|
for error_match in error_matches:
|
2023-05-30 15:08:41 +02:00
|
|
|
filename = str(error_match.group("filename"))
|
2023-05-29 17:00:06 +02:00
|
|
|
line_number = int(error_match.group("line_number"))
|
|
|
|
column_number = int(error_match.group("column_number"))
|
2023-05-30 15:08:41 +02:00
|
|
|
warning_message = str(error_match.group("warning_message"))
|
2023-03-12 10:22:09 +01:00
|
|
|
|
2023-05-29 17:00:06 +02:00
|
|
|
errors.append(
|
|
|
|
ClangError(
|
|
|
|
filename,
|
|
|
|
line_number,
|
|
|
|
column_number,
|
|
|
|
warning_message[1:],
|
|
|
|
)
|
|
|
|
)
|
2023-03-12 10:22:09 +01:00
|
|
|
|
2023-05-29 17:00:06 +02:00
|
|
|
return errors
|
2023-03-12 10:22:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-05-29 17:00:06 +02:00
|
|
|
files = analyze_args(sys.argv[1:])
|
|
|
|
for file in files:
|
|
|
|
clang_format_output = clang_format(file)
|
|
|
|
parsed_output = parse_clang_format_output(clang_format_output)
|
2023-03-12 10:22:09 +01:00
|
|
|
|
2023-05-29 17:00:06 +02:00
|
|
|
if parsed_output:
|
|
|
|
for error in parsed_output:
|
|
|
|
print(
|
|
|
|
f"Avertissement dans {error.filename} "
|
|
|
|
f"à la ligne {error.line_number}, "
|
|
|
|
f"caractère {error.column_number} : {error.warning_message}"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
print(f"Aucun avertissement trouvé dans {file}.")
|