1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2011 The Chromium OS Authors.
6 import concurrent.futures
11 from patman import gitutil
12 from u_boot_pylib import command
13 from u_boot_pylib import terminal
15 EMACS_PREFIX = r'(?:[0-9]{4}.*\.patch:[0-9]+: )?'
16 TYPE_NAME = r'([A-Z_]+:)?'
17 RE_ERROR = re.compile(r'ERROR:%s (.*)' % TYPE_NAME)
18 RE_WARNING = re.compile(EMACS_PREFIX + r'WARNING:%s (.*)' % TYPE_NAME)
19 RE_CHECK = re.compile(r'CHECK:%s (.*)' % TYPE_NAME)
20 RE_FILE = re.compile(r'#(\d+): (FILE: ([^:]*):(\d+):)?')
21 RE_NOTE = re.compile(r'NOTE: (.*)')
24 def find_check_patch():
25 top_level = gitutil.get_top_level()
28 os.path.join(os.getcwd(), '..', '..'),
29 os.path.join(top_level, 'tools'),
30 os.path.join(top_level, 'scripts'),
31 '%s/bin' % os.getenv('HOME'),
35 fname = os.path.join(path, 'checkpatch.pl')
36 if os.path.isfile(fname):
39 # Look upwwards for a Chrome OS tree
40 while not os.path.ismount(path):
41 fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files',
42 'scripts', 'checkpatch.pl')
43 if os.path.isfile(fname):
45 path = os.path.dirname(path)
47 sys.exit('Cannot find checkpatch.pl - please put it in your ' +
48 '~/bin directory or use --no-check')
51 def check_patch_parse_one_message(message):
52 """Parse one checkpatch message
55 message: string to parse
59 'type'; error or warning
65 if RE_NOTE.match(message):
70 err_match = RE_ERROR.match(message)
71 warn_match = RE_WARNING.match(message)
72 check_match = RE_CHECK.match(message)
74 item['cptype'] = err_match.group(1)
75 item['msg'] = err_match.group(2)
76 item['type'] = 'error'
78 item['cptype'] = warn_match.group(1)
79 item['msg'] = warn_match.group(2)
80 item['type'] = 'warning'
82 item['cptype'] = check_match.group(1)
83 item['msg'] = check_match.group(2)
84 item['type'] = 'check'
87 print('patman: failed to parse checkpatch message:\n%s' %
88 (message_indent + message.replace('\n', '\n' + message_indent)),
92 file_match = RE_FILE.search(message)
93 # some messages have no file, catch those here
94 no_file_match = any(s in message for s in [
95 '\nSubject:', 'Missing Signed-off-by: line(s)',
96 'does MAINTAINERS need updating'
100 err_fname = file_match.group(3)
102 item['file'] = err_fname
103 item['line'] = int(file_match.group(4))
105 item['file'] = '<patch>'
106 item['line'] = int(file_match.group(1))
108 item['file'] = '<patch>'
111 print('patman: failed to find file / line information:\n%s' %
112 (message_indent + message.replace('\n', '\n' + message_indent)),
118 def check_patch_parse(checkpatch_output, verbose=False):
119 """Parse checkpatch.pl output
122 checkpatch_output: string to parse
123 verbose: True to print out every line of the checkpatch output as it is
127 namedtuple containing:
128 ok: False=failure, True=ok
129 problems (list of problems): each a dict:
130 'type'; error or warning
134 errors: Number of errors
135 warnings: Number of warnings
136 checks: Number of checks
137 lines: Number of lines
138 stdout: checkpatch_output
140 fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines',
142 result = collections.namedtuple('CheckPatchResult', fields)
143 result.stdout = checkpatch_output
145 result.errors, result.warnings, result.checks = 0, 0, 0
149 # total: 0 errors, 0 warnings, 159 lines checked
151 # total: 0 errors, 2 warnings, 7 checks, 473 lines checked
152 emacs_stats = r'(?:[0-9]{4}.*\.patch )?'
153 re_stats = re.compile(emacs_stats +
154 r'total: (\d+) errors, (\d+) warnings, (\d+)')
155 re_stats_full = re.compile(emacs_stats +
156 r'total: (\d+) errors, (\d+) warnings, (\d+)'
158 re_ok = re.compile(r'.*has no obvious style problems')
159 re_bad = re.compile(r'.*has style problems, please review')
161 # A blank line indicates the end of a message
162 for message in result.stdout.split('\n\n'):
166 # either find stats, the verdict, or delegate
167 match = re_stats_full.match(message)
169 match = re_stats.match(message)
171 result.errors = int(match.group(1))
172 result.warnings = int(match.group(2))
173 if len(match.groups()) == 4:
174 result.checks = int(match.group(3))
175 result.lines = int(match.group(4))
177 result.lines = int(match.group(3))
178 elif re_ok.match(message):
180 elif re_bad.match(message):
183 problem = check_patch_parse_one_message(message)
185 result.problems.append(problem)
190 def check_patch(fname, verbose=False, show_types=False, use_tree=False):
191 """Run checkpatch.pl on a file and parse the results.
194 fname: Filename to check
195 verbose: True to print out every line of the checkpatch output as it is
197 show_types: Tell checkpatch to show the type (number) of each message
198 use_tree (bool): If False we'll pass '--no-tree' to checkpatch.
201 namedtuple containing:
202 ok: False=failure, True=ok
203 problems: List of problems, each a dict:
204 'type'; error or warning
208 errors: Number of errors
209 warnings: Number of warnings
210 checks: Number of checks
211 lines: Number of lines
212 stdout: Full output of checkpatch
214 chk = find_check_patch()
217 args.append('--no-tree')
219 args.append('--show-types')
220 output = command.output(*args, fname, raise_on_error=False)
222 return check_patch_parse(output, verbose)
225 def get_warning_msg(col, msg_type, fname, line, msg):
226 '''Create a message for a given file/line
229 msg_type: Message type ('error' or 'warning')
230 fname: Filename which reports the problem
231 line: Line number where it was noticed
232 msg: Message to report
234 if msg_type == 'warning':
235 msg_type = col.build(col.YELLOW, msg_type)
236 elif msg_type == 'error':
237 msg_type = col.build(col.RED, msg_type)
238 elif msg_type == 'check':
239 msg_type = col.build(col.MAGENTA, msg_type)
240 line_str = '' if line is None else '%d' % line
241 return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg)
243 def check_patches(verbose, args, use_tree):
244 '''Run the checkpatch.pl script on each patch'''
245 error_count, warning_count, check_count = 0, 0, 0
246 col = terminal.Color()
248 with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor:
251 f = executor.submit(check_patch, fname, verbose, use_tree=use_tree)
254 for fname, f in zip(args, futures):
257 error_count += result.errors
258 warning_count += result.warnings
259 check_count += result.checks
260 print('%d errors, %d warnings, %d checks for %s:' % (result.errors,
261 result.warnings, result.checks, col.build(col.BLUE, fname)))
262 if (len(result.problems) != result.errors + result.warnings +
264 print("Internal error: some problems lost")
265 # Python seems to get confused by this
266 # pylint: disable=E1133
267 for item in result.problems:
269 get_warning_msg(col, item.get('type', '<unknown>'),
270 item.get('file', '<unknown>'),
271 item.get('line', 0), item.get('msg', 'message')))
273 if error_count or warning_count or check_count:
274 str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
280 print(col.build(color, str % (error_count, warning_count, check_count)))