clang-format pip
# american fuzzy lop++ - custom code formatter
# --------------------------------------------
#
-# Written and maintaned by Andrea Fioraldi <andreafioraldi@gmail.com>
+# Written and maintained by Andrea Fioraldi <andreafioraldi@gmail.com>
#
# Copyright 2015, 2016, 2017 Google Inc. All rights reserved.
# Copyright 2019-2022 AFLplusplus Project. All rights reserved.
import subprocess
import sys
import os
-import re
+# import re # TODO: for future use
import shutil
+import importlib.metadata
+
+# string_re = re.compile('(\\"(\\\\.|[^"\\\\])*\\")') # TODO: for future use
+
+CURRENT_LLVM = os.getenv('LLVM_VERSION', 14)
+CLANG_FORMAT_BIN = os.getenv("CLANG_FORMAT_BIN", "")
+
+
+def check_clang_format_pip_version():
+ """
+ Check if the correct version of clang-format is installed via pip.
+
+ Returns:
+ bool: True if the correct version of clang-format is installed,
+ False otherwise.
+ """
+ # Check if clang-format is installed
+ if importlib.util.find_spec('clang_format'):
+ # Check if the installed version is the expected LLVM version
+ if importlib.metadata.version('clang-format')\
+ .startswith(CURRENT_LLVM+'.'):
+ return True
+ else:
+ # Return False, because the clang-format version does not match
+ return False
+ else:
+ # If the 'clang_format' package isn't installed, return False
+ return False
-# string_re = re.compile('(\\"(\\\\.|[^"\\\\])*\\")') # future use
with open(".clang-format") as f:
fmt = f.read()
-CURRENT_LLVM = os.getenv('LLVM_VERSION', 14)
-CLANG_FORMAT_BIN = os.getenv("CLANG_FORMAT_BIN", "")
+
+CLANG_FORMAT_PIP = check_clang_format_pip_version()
if shutil.which(CLANG_FORMAT_BIN) is None:
CLANG_FORMAT_BIN = f"clang-format-{CURRENT_LLVM}"
-if shutil.which(CLANG_FORMAT_BIN) is None:
+if shutil.which(CLANG_FORMAT_BIN) is None \
+ and CLANG_FORMAT_PIP is False:
print(f"[!] clang-format-{CURRENT_LLVM} is needed. Aborted.")
+ print(f"Run `pip3 install \"clang-format=={CURRENT_LLVM}.*\"` \
+to install via pip.")
exit(1)
+if CLANG_FORMAT_PIP:
+ CLANG_FORMAT_BIN = shutil.which("clang-format")
+
COLUMN_LIMIT = 80
for line in fmt.split("\n"):
line = line.split(":")
