]> git.ipfire.org Git - thirdparty/gcc.git/blob - contrib/check_GNU_style_lib.py
check_GNU_style_lib.py: Suggest to install all missing pip3 packages at once
[thirdparty/gcc.git] / contrib / check_GNU_style_lib.py
1 #!/usr/bin/env python3
2 #
3 # Checks some of the GNU style formatting rules in a set of patches.
4 # The script is a rewritten of the same bash script and should eventually
5 # replace the former script.
6 #
7 # This file is part of GCC.
8 #
9 # GCC is free software; you can redistribute it and/or modify it under
10 # the terms of the GNU General Public License as published by the Free
11 # Software Foundation; either version 3, or (at your option) any later
12 # version.
13 #
14 # GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 # for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with GCC; see the file COPYING3. If not see
21 # <http://www.gnu.org/licenses/>. */
22 #
23 # The script requires python packages, which can be installed via pip3
24 # like this:
25 # $ pip3 install unidiff termcolor
26
27 import sys
28 import re
29 import unittest
30
31 def import_pip3(*args):
32 missing=[]
33 for (module, names) in args:
34 try:
35 lib = __import__(module)
36 except ImportError:
37 missing.append(module)
38 continue
39 if not isinstance(names, list):
40 names=[names]
41 for name in names:
42 globals()[name]=getattr(lib, name)
43 if len(missing) > 0:
44 missing_and_sep = ' and '.join(missing)
45 missing_space_sep = ' '.join(missing)
46 print('%s %s missing (run: pip3 install %s)'
47 % (missing_and_sep,
48 ("module is" if len(missing) == 1 else "modules are"),
49 missing_space_sep))
50 exit(3)
51
52 import_pip3(('termcolor', 'colored'),
53 ('unidiff', 'PatchSet'))
54
55 from itertools import *
56
57 ws_char = '█'
58 ts = 8
59
60 def error_string(s):
61 return colored(s, 'red', attrs = ['bold'])
62
63 class CheckError:
64 def __init__(self, filename, lineno, console_error, error_message,
65 column = -1):
66 self.filename = filename
67 self.lineno = lineno
68 self.console_error = console_error
69 self.error_message = error_message
70 self.column = column
71
72 def error_location(self):
73 return '%s:%d:%d:' % (self.filename, self.lineno,
74 self.column if self.column != -1 else -1)
75
76 class LineLengthCheck:
77 def __init__(self):
78 self.limit = 80
79 self.expanded_tab = ' ' * ts
80
81 def check(self, filename, lineno, line):
82 line_expanded = line.replace('\t', self.expanded_tab)
83 if len(line_expanded) > self.limit:
84 return CheckError(filename, lineno,
85 line_expanded[:self.limit]
86 + error_string(line_expanded[self.limit:]),
87 'lines should not exceed 80 characters', self.limit)
88
89 return None
90
91 class SpacesCheck:
92 def __init__(self):
93 self.expanded_tab = ' ' * ts
94
95 def check(self, filename, lineno, line):
96 i = line.find(self.expanded_tab)
97 if i != -1:
98 return CheckError(filename, lineno,
99 line.replace(self.expanded_tab, error_string(ws_char * ts)),
100 'blocks of 8 spaces should be replaced with tabs', i)
101
102 class TrailingWhitespaceCheck:
103 def __init__(self):
104 self.re = re.compile('(\s+)$')
105
106 def check(self, filename, lineno, line):
107 m = self.re.search(line)
108 if m != None:
109 return CheckError(filename, lineno,
110 line[:m.start(1)] + error_string(ws_char * len(m.group(1)))
111 + line[m.end(1):],
112 'trailing whitespace', m.start(1))
113
114 class SentenceSeparatorCheck:
115 def __init__(self):
116 self.re = re.compile('\w\.(\s|\s{3,})\w')
117
118 def check(self, filename, lineno, line):
119 m = self.re.search(line)
120 if m != None:
121 return CheckError(filename, lineno,
122 line[:m.start(1)] + error_string(ws_char * len(m.group(1)))
123 + line[m.end(1):],
124 'dot, space, space, new sentence', m.start(1))
125
126 class SentenceEndOfCommentCheck:
127 def __init__(self):
128 self.re = re.compile('\w\.(\s{0,1}|\s{3,})\*/')
129
130 def check(self, filename, lineno, line):
131 m = self.re.search(line)
132 if m != None:
133 return CheckError(filename, lineno,
134 line[:m.start(1)] + error_string(ws_char * len(m.group(1)))
135 + line[m.end(1):],
136 'dot, space, space, end of comment', m.start(1))
137
138 class SentenceDotEndCheck:
139 def __init__(self):
140 self.re = re.compile('\w(\s*\*/)')
141
142 def check(self, filename, lineno, line):
143 m = self.re.search(line)
144 if m != None:
145 return CheckError(filename, lineno,
146 line[:m.start(1)] + error_string(m.group(1)) + line[m.end(1):],
147 'dot, space, space, end of comment', m.start(1))
148
149 class FunctionParenthesisCheck:
150 # TODO: filter out GTY stuff
151 def __init__(self):
152 self.re = re.compile('\w(\s{2,})?(\()')
153
154 def check(self, filename, lineno, line):
155 if '#define' in line:
156 return None
157
158 m = self.re.search(line)
159 if m != None:
160 return CheckError(filename, lineno,
161 line[:m.start(2)] + error_string(m.group(2)) + line[m.end(2):],
162 'there should be exactly one space between function name ' \
163 'and parenthesis', m.start(2))
164
165 class SquareBracketCheck:
166 def __init__(self):
167 self.re = re.compile('\w\s+(\[)')
168
169 def check(self, filename, lineno, line):
170 m = self.re.search(line)
171 if m != None:
172 return CheckError(filename, lineno,
173 line[:m.start(1)] + error_string(m.group(1)) + line[m.end(1):],
174 'there should be no space before a left square bracket',
175 m.start(1))
176
177 class ClosingParenthesisCheck:
178 def __init__(self):
179 self.re = re.compile('\S\s+(\))')
180
181 def check(self, filename, lineno, line):
182 m = self.re.search(line)
183 if m != None:
184 return CheckError(filename, lineno,
185 line[:m.start(1)] + error_string(m.group(1)) + line[m.end(1):],
186 'there should be no space before closing parenthesis',
187 m.start(1))
188
189 class BracesOnSeparateLineCheck:
190 # This will give false positives for C99 compound literals.
191
192 def __init__(self):
193 self.re = re.compile('(\)|else)\s*({)')
194
195 def check(self, filename, lineno, line):
196 m = self.re.search(line)
197 if m != None:
198 return CheckError(filename, lineno,
199 line[:m.start(2)] + error_string(m.group(2)) + line[m.end(2):],
200 'braces should be on a separate line', m.start(2))
201
202 class TrailinigOperatorCheck:
203 def __init__(self):
204 regex = '^\s.*(([^a-zA-Z_]\*)|([-%<=&|^?])|([^*]/)|([^:][+]))$'
205 self.re = re.compile(regex)
206
207 def check(self, filename, lineno, line):
208 m = self.re.search(line)
209 if m != None:
210 return CheckError(filename, lineno,
211 line[:m.start(1)] + error_string(m.group(1)) + line[m.end(1):],
212 'trailing operator', m.start(1))
213
214 class LineLengthTest(unittest.TestCase):
215 def setUp(self):
216 self.check = LineLengthCheck()
217
218 def test_line_length_check_basic(self):
219 r = self.check.check('foo', 123, self.check.limit * 'a' + ' = 123;')
220 self.assertIsNotNone(r)
221 self.assertEqual('foo', r.filename)
222 self.assertEqual(80, r.column)
223 self.assertEqual(r.console_error,
224 self.check.limit * 'a' + error_string(' = 123;'))
225
226 def check_GNU_style_file(file, format):
227 checks = [LineLengthCheck(), SpacesCheck(), TrailingWhitespaceCheck(),
228 SentenceSeparatorCheck(), SentenceEndOfCommentCheck(),
229 SentenceDotEndCheck(), FunctionParenthesisCheck(),
230 SquareBracketCheck(), ClosingParenthesisCheck(),
231 BracesOnSeparateLineCheck(), TrailinigOperatorCheck()]
232 errors = []
233
234 with open(file, 'rb') as diff_file:
235 patch = PatchSet(diff_file, encoding = 'utf-8')
236
237 for pfile in patch.added_files + patch.modified_files:
238 t = pfile.target_file.lstrip('b/')
239 # Skip testsuite files
240 if 'testsuite' in t:
241 continue
242
243 for hunk in pfile:
244 delta = 0
245 for line in hunk:
246 if line.is_added and line.target_line_no != None:
247 for check in checks:
248 e = check.check(t, line.target_line_no, line.value)
249 if e != None:
250 errors.append(e)
251
252 if format == 'stdio':
253 fn = lambda x: x.error_message
254 i = 1
255 for (k, errors) in groupby(sorted(errors, key = fn), fn):
256 errors = list(errors)
257 print('=== ERROR type #%d: %s (%d error(s)) ==='
258 % (i, k, len(errors)))
259 i += 1
260 for e in errors:
261 print(e.error_location () + e.console_error)
262 print()
263
264 exit(0 if len(errors) == 0 else 1)
265 elif format == 'quickfix':
266 f = 'errors.err'
267 with open(f, 'w+') as qf:
268 for e in errors:
269 qf.write('%s%s\n' % (e.error_location(), e.error_message))
270 if len(errors) == 0:
271 exit(0)
272 else:
273 print('%d error(s) written to %s file.' % (len(errors), f))
274 exit(1)
275 else:
276 assert False
277
278 if __name__ == '__main__':
279 unittest.main()