3
3
# american fuzzy lop++ - custom code formatter
4
4
# --------------------------------------------
5
5
#
6
- # Written and maintaned by Andrea Fioraldi <[email protected] >
6
+ # Written and maintained by Andrea Fioraldi <[email protected] >
7
7
#
8
8
# Copyright 2015, 2016, 2017 Google Inc. All rights reserved.
9
9
# Copyright 2019-2022 AFLplusplus Project. All rights reserved.
18
18
import subprocess
19
19
import sys
20
20
import os
21
- import re
21
+ # import re # TODO: for future use
22
22
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
23
51
24
- # string_re = re.compile('(\\"(\\\\.|[^"\\\\])*\\")') # future use
25
52
26
53
with open (".clang-format" ) as f :
27
54
fmt = f .read ()
28
55
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 ( )
31
58
32
59
if shutil .which (CLANG_FORMAT_BIN ) is None :
33
60
CLANG_FORMAT_BIN = f"clang-format-{ CURRENT_LLVM } "
34
61
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 :
36
64
print (f"[!] clang-format-{ CURRENT_LLVM } is needed. Aborted." )
65
+ print (f"Run `pip3 install \" clang-format=={ CURRENT_LLVM } .*\" ` \
66
+ to install via pip." )
37
67
exit (1 )
38
68
69
+ if CLANG_FORMAT_PIP :
70
+ CLANG_FORMAT_BIN = shutil .which ("clang-format" )
71
+
39
72
COLUMN_LIMIT = 80
40
73
for line in fmt .split ("\n " ):
41
74
line = line .split (":" )
@@ -54,43 +87,43 @@ def custom_format(filename):
54
87
55
88
for line in src .split ("\n " ):
56
89
if line .lstrip ().startswith ("#" ):
57
- if line [line .find ("#" ) + 1 :].lstrip ().startswith ("define" ):
90
+ if line [line .find ("#" ) + 1 :].lstrip ().startswith ("define" ):
58
91
in_define = True
59
92
60
93
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 )
65
98
):
66
99
cmt_start = line .rfind ("/*" )
67
100
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 :]
71
104
)
72
105
73
106
define_padding = 0
74
107
if last_line is not None and in_define and last_line .endswith ("\\ " ):
75
108
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 :]))
77
110
78
111
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 () != ""
82
115
):
83
116
line = (" " * define_padding + "\\ " if in_define else "" ) + "\n " + line
84
117
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 () != ""
88
121
):
89
122
line = (" " * define_padding + "\\ " if in_define else "" ) + "\n " + line
90
123
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 () != ""
94
127
):
95
128
line = (" " * define_padding + "\\ " if in_define else "" ) + "\n " + line
96
129
0 commit comments