Skip to content

Commit 026404c

Browse files
Merge pull request AFLplusplus#1608 from ahpaleus/custom-format-pip
Support for clang-format from pip in the .custom-format.py
2 parents ffe89e8 + 1bcc9bf commit 026404c

File tree

1 file changed

+57
-24
lines changed

1 file changed

+57
-24
lines changed

.custom-format.py

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# american fuzzy lop++ - custom code formatter
44
# --------------------------------------------
55
#
6-
# Written and maintaned by Andrea Fioraldi <[email protected]>
6+
# Written and maintained by Andrea Fioraldi <[email protected]>
77
#
88
# Copyright 2015, 2016, 2017 Google Inc. All rights reserved.
99
# Copyright 2019-2022 AFLplusplus Project. All rights reserved.
@@ -18,24 +18,57 @@
1818
import subprocess
1919
import sys
2020
import os
21-
import re
21+
# import re # TODO: for future use
2222
import shutil
23+
import importlib.metadata
24+
25+
# string_re = re.compile('(\\"(\\\\.|[^"\\\\])*\\")') # TODO: for future use
26+
27+
CURRENT_LLVM = os.getenv('LLVM_VERSION', 14)
28+
CLANG_FORMAT_BIN = os.getenv("CLANG_FORMAT_BIN", "")
29+
30+
31+
def check_clang_format_pip_version():
32+
"""
33+
Check if the correct version of clang-format is installed via pip.
34+
35+
Returns:
36+
bool: True if the correct version of clang-format is installed,
37+
False otherwise.
38+
"""
39+
# Check if clang-format is installed
40+
if importlib.util.find_spec('clang_format'):
41+
# Check if the installed version is the expected LLVM version
42+
if importlib.metadata.version('clang-format')\
43+
.startswith(CURRENT_LLVM+'.'):
44+
return True
45+
else:
46+
# Return False, because the clang-format version does not match
47+
return False
48+
else:
49+
# If the 'clang_format' package isn't installed, return False
50+
return False
2351

24-
# string_re = re.compile('(\\"(\\\\.|[^"\\\\])*\\")') # future use
2552

2653
with open(".clang-format") as f:
2754
fmt = f.read()
2855

29-
CURRENT_LLVM = os.getenv('LLVM_VERSION', 14)
30-
CLANG_FORMAT_BIN = os.getenv("CLANG_FORMAT_BIN", "")
56+
57+
CLANG_FORMAT_PIP = check_clang_format_pip_version()
3158

3259
if shutil.which(CLANG_FORMAT_BIN) is None:
3360
CLANG_FORMAT_BIN = f"clang-format-{CURRENT_LLVM}"
3461

35-
if shutil.which(CLANG_FORMAT_BIN) is None:
62+
if shutil.which(CLANG_FORMAT_BIN) is None \
63+
and CLANG_FORMAT_PIP is False:
3664
print(f"[!] clang-format-{CURRENT_LLVM} is needed. Aborted.")
65+
print(f"Run `pip3 install \"clang-format=={CURRENT_LLVM}.*\"` \
66+
to install via pip.")
3767
exit(1)
3868

69+
if CLANG_FORMAT_PIP:
70+
CLANG_FORMAT_BIN = shutil.which("clang-format")
71+
3972
COLUMN_LIMIT = 80
4073
for line in fmt.split("\n"):
4174
line = line.split(":")
@@ -54,43 +87,43 @@ def custom_format(filename):
5487

5588
for line in src.split("\n"):
5689
if line.lstrip().startswith("#"):
57-
if line[line.find("#") + 1 :].lstrip().startswith("define"):
90+
if line[line.find("#") + 1:].lstrip().startswith("define"):
5891
in_define = True
5992

6093
if (
61-
"/*" in line
62-
and not line.strip().startswith("/*")
63-
and line.endswith("*/")
64-
and len(line) < (COLUMN_LIMIT - 2)
94+
"/*" in line
95+
and not line.strip().startswith("/*")
96+
and line.endswith("*/")
97+
and len(line) < (COLUMN_LIMIT - 2)
6598
):
6699
cmt_start = line.rfind("/*")
67100
line = (
68-
line[:cmt_start]
69-
+ " " * (COLUMN_LIMIT - 2 - len(line))
70-
+ line[cmt_start:]
101+
line[:cmt_start]
102+
+ " " * (COLUMN_LIMIT - 2 - len(line))
103+
+ line[cmt_start:]
71104
)
72105

73106
define_padding = 0
74107
if last_line is not None and in_define and last_line.endswith("\\"):
75108
last_line = last_line[:-1]
76-
define_padding = max(0, len(last_line[last_line.rfind("\n") + 1 :]))
109+
define_padding = max(0, len(last_line[last_line.rfind("\n") + 1:]))
77110

78111
if (
79-
last_line is not None
80-
and last_line.strip().endswith("{")
81-
and line.strip() != ""
112+
last_line is not None
113+
and last_line.strip().endswith("{")
114+
and line.strip() != ""
82115
):
83116
line = (" " * define_padding + "\\" if in_define else "") + "\n" + line
84117
elif (
85-
last_line is not None
86-
and last_line.strip().startswith("}")
87-
and line.strip() != ""
118+
last_line is not None
119+
and last_line.strip().startswith("}")
120+
and line.strip() != ""
88121
):
89122
line = (" " * define_padding + "\\" if in_define else "") + "\n" + line
90123
elif (
91-
line.strip().startswith("}")
92-
and last_line is not None
93-
and last_line.strip() != ""
124+
line.strip().startswith("}")
125+
and last_line is not None
126+
and last_line.strip() != ""
94127
):
95128
line = (" " * define_padding + "\\" if in_define else "") + "\n" + line
96129

0 commit comments

Comments
 (0)