diff --git a/.custom-format.py b/.custom-format.py index e7e2892b..9899ea9d 100755 --- a/.custom-format.py +++ b/.custom-format.py @@ -21,12 +21,15 @@ import os # import re # TODO: for future use import shutil import importlib.metadata +import hashlib # string_re = re.compile('(\\"(\\\\.|[^"\\\\])*\\")') # TODO: for future use CURRENT_LLVM = os.getenv('LLVM_VERSION', 18) CLANG_FORMAT_BIN = os.getenv("CLANG_FORMAT_BIN", "") +FORMAT_CACHE_DIR = '.format-cache' +os.makedirs(FORMAT_CACHE_DIR, exist_ok=True) def check_clang_format_pip_version(): """ @@ -69,6 +72,8 @@ to install via pip.") if CLANG_FORMAT_PIP: CLANG_FORMAT_BIN = shutil.which("clang-format") +CLANG_FORMAT_VERSION = subprocess.check_output([CLANG_FORMAT_BIN, '--version']) + COLUMN_LIMIT = 80 for line in fmt.split("\n"): line = line.split(":") @@ -135,6 +140,38 @@ def custom_format(filename): return out +def hash_code_and_formatter(code): + hasher = hashlib.sha256() + + hasher.update(code.encode()) + hasher.update(CLANG_FORMAT_VERSION) + with open(__file__, 'rb') as f: + hasher.update(f.read()) + + return hasher.hexdigest() + + +def custom_format_cached(filename): + filename_hash = hashlib.sha256(filename.encode()).hexdigest() + cache_file = os.path.join(FORMAT_CACHE_DIR, filename_hash) + + if os.path.exists(cache_file): + with open(filename) as f: + code = f.read() + code_hash = hash_code_and_formatter(code) + with open(cache_file) as f: + if f.read() == code_hash: + return code + + code = custom_format(filename) + + code_hash = hash_code_and_formatter(code) + with open(cache_file, 'w') as f: + f.write(code_hash) + + return code + + args = sys.argv[1:] if len(args) == 0: print("Usage: ./format.py [-i] ") @@ -150,7 +187,7 @@ if args[0] == "-i": args = args[1:] for filename in args: - code = custom_format(filename) + code = custom_format_cached(filename) if in_place: with open(filename, "w") as f: f.write(code) diff --git a/.gitignore b/.gitignore index bc06ef2d..4f25e5e4 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ *.so *.swp .DS_Store +.format-cache .sync_tmp .test .test2 @@ -113,4 +114,4 @@ utils/replay_record/persistent_demo_replay_argparse utils/plot_ui/afl-plot-ui vuln_prog argv_fuzz_demo -argv_fuzz_persistent_demo \ No newline at end of file +argv_fuzz_persistent_demo diff --git a/GNUmakefile b/GNUmakefile index bbe95908..697c48af 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -684,6 +684,7 @@ deepclean: clean rm -rf unicorn_mode/unicornafl rm -rf qemu_mode/qemuafl rm -rf nyx_mode/libnyx nyx_mode/packer nyx_mode/QEMU-Nyx + rm -rf .format-cache ifeq "$(IN_REPO)" "1" git checkout coresight_mode/coresight-trace git checkout unicorn_mode/unicornafl