papermario/tools/coverage.py

62 lines
2.1 KiB
Python
Raw Normal View History

2020-08-16 07:11:40 +02:00
#!/usr/bin/env python3
from os import path
2020-08-18 02:19:21 +02:00
from os import remove
2020-08-19 02:52:52 +02:00
from sys import argv
2020-08-16 07:11:40 +02:00
import re
from glob import glob
DIR = path.dirname(__file__)
C_FILES = glob(path.join(DIR, "../src/*.c"))
ASM_FILES = glob(path.join(DIR, "../asm/nonmatchings/**/*.s"))
def strip_c_comments(text):
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return " "
else:
return s
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)
c_func_pattern = re.compile(
r"^[^\s]+\s+([^\s(]+)\(([^\n)]*)\)\s+{",
re.MULTILINE
)
def funcs_in_c(text):
text = strip_c_comments(text)
return [match.group(1) for match in c_func_pattern.finditer(text)]
matched = []
for filename in C_FILES:
with open(filename, "r") as file:
matched += funcs_in_c(file.read())
non_matched = [path.splitext(path.basename(filename))[0] for filename in ASM_FILES]
2020-08-19 02:52:52 +02:00
matched_but_undeleted_asm = [f for f in matched if f in non_matched]
2020-08-16 07:11:40 +02:00
2020-08-19 02:52:52 +02:00
if __name__ == "__main__":
if "--help" in argv:
print("--fail-matched-undeleted exit with error code 1 if matched function(s) exist in asm/nonmatchings/")
print("--delete-matched delete matched function(s) from asm/nonmatchings/ without asking")
exit()
2020-08-18 02:19:21 +02:00
2020-08-19 02:52:52 +02:00
total = len(matched) + len(non_matched)
print(f"{len(matched)}/{total} ({(len(matched) / total) * 100:.2f}%)")
if len(matched_but_undeleted_asm) > 0:
print(f"The following functions have been matched but still exist in asm/nonmatchings/: {' '.join(matched_but_undeleted_asm)}")
if "--fail-matched-undeleted" in argv:
print(f"The following functions have been matched but still exist in asm/nonmatchings/: {' '.join(matched_but_undeleted_asm)}")
exit(1)
else:
if "--delete-matched" in argv or input("Delete them [y/N]? ").upper() == "Y":
for func in matched_but_undeleted_asm:
file = glob(path.join(DIR, f"../asm/nonmatchings/*/{func}.s"))[0]
remove(file)