blob: 8978df25c15992b38c9fe050ea764b36e594aedb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass26132882012-01-14 15:12:45 +00002# Copyright (c) 2011 The Chromium OS Authors.
3#
Simon Glass26132882012-01-14 15:12:45 +00004
Simon Glass0495abf2013-03-26 13:09:39 +00005import collections
Simon Glass26132882012-01-14 15:12:45 +00006import os
7import re
Vadim Bendeburyc549f082013-01-09 16:00:10 +00008import sys
Simon Glassa997ea52020-04-17 18:09:04 -06009
10from patman import command
11from patman import gitutil
12from patman import terminal
Evan Bennec7378b2021-04-01 13:49:30 +110013
14EMACS_PREFIX = r'(?:[0-9]{4}.*\.patch:[0-9]+: )?'
15TYPE_NAME = r'([A-Z_]+:)?'
16RE_ERROR = re.compile(r'ERROR:%s (.*)' % TYPE_NAME)
17RE_WARNING = re.compile(EMACS_PREFIX + r'WARNING:%s (.*)' % TYPE_NAME)
18RE_CHECK = re.compile(r'CHECK:%s (.*)' % TYPE_NAME)
19RE_FILE = re.compile(r'#(\d+): (FILE: ([^:]*):(\d+):)?')
20RE_NOTE = re.compile(r'NOTE: (.*)')
21
Simon Glass26132882012-01-14 15:12:45 +000022
23def FindCheckPatch():
Doug Anderson4baede02012-11-26 15:23:23 +000024 top_level = gitutil.GetTopLevel()
Simon Glass26132882012-01-14 15:12:45 +000025 try_list = [
26 os.getcwd(),
27 os.path.join(os.getcwd(), '..', '..'),
Doug Anderson4baede02012-11-26 15:23:23 +000028 os.path.join(top_level, 'tools'),
29 os.path.join(top_level, 'scripts'),
Simon Glass26132882012-01-14 15:12:45 +000030 '%s/bin' % os.getenv('HOME'),
31 ]
32 # Look in current dir
33 for path in try_list:
34 fname = os.path.join(path, 'checkpatch.pl')
35 if os.path.isfile(fname):
36 return fname
37
38 # Look upwwards for a Chrome OS tree
39 while not os.path.ismount(path):
40 fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files',
41 'scripts', 'checkpatch.pl')
42 if os.path.isfile(fname):
43 return fname
44 path = os.path.dirname(path)
Vadim Bendeburyc549f082013-01-09 16:00:10 +000045
Masahiro Yamada880828d2014-08-16 00:59:26 +090046 sys.exit('Cannot find checkpatch.pl - please put it in your ' +
47 '~/bin directory or use --no-check')
Simon Glass26132882012-01-14 15:12:45 +000048
Evan Bennec7378b2021-04-01 13:49:30 +110049
50def CheckPatchParseOneMessage(message):
51 """Parse one checkpatch message
Simon Glass26132882012-01-14 15:12:45 +000052
Simon Glass24725af2020-07-05 21:41:49 -060053 Args:
Evan Bennec7378b2021-04-01 13:49:30 +110054 message: string to parse
55
56 Returns:
57 dict:
58 'type'; error or warning
59 'msg': text message
60 'file' : filename
61 'line': line number
62 """
63
64 if RE_NOTE.match(message):
65 return {}
66
67 item = {}
68
69 err_match = RE_ERROR.match(message)
70 warn_match = RE_WARNING.match(message)
71 check_match = RE_CHECK.match(message)
72 if err_match:
73 item['cptype'] = err_match.group(1)
74 item['msg'] = err_match.group(2)
75 item['type'] = 'error'
76 elif warn_match:
77 item['cptype'] = warn_match.group(1)
78 item['msg'] = warn_match.group(2)
79 item['type'] = 'warning'
80 elif check_match:
81 item['cptype'] = check_match.group(1)
82 item['msg'] = check_match.group(2)
83 item['type'] = 'check'
84 else:
85 message_indent = ' '
86 print('patman: failed to parse checkpatch message:\n%s' %
87 (message_indent + message.replace('\n', '\n' + message_indent)),
88 file=sys.stderr)
89 return {}
90
91 file_match = RE_FILE.search(message)
92 # some messages have no file, catch those here
93 no_file_match = any(s in message for s in [
94 '\nSubject:', 'Missing Signed-off-by: line(s)',
95 'does MAINTAINERS need updating'
96 ])
97
98 if file_match:
99 err_fname = file_match.group(3)
100 if err_fname:
101 item['file'] = err_fname
102 item['line'] = int(file_match.group(4))
103 else:
104 item['file'] = '<patch>'
105 item['line'] = int(file_match.group(1))
106 elif no_file_match:
107 item['file'] = '<patch>'
108 else:
109 message_indent = ' '
110 print('patman: failed to find file / line information:\n%s' %
111 (message_indent + message.replace('\n', '\n' + message_indent)),
112 file=sys.stderr)
113
114 return item
115
116
117def CheckPatchParse(checkpatch_output, verbose=False):
118 """Parse checkpatch.pl output
119
120 Args:
121 checkpatch_output: string to parse
Simon Glass24725af2020-07-05 21:41:49 -0600122 verbose: True to print out every line of the checkpatch output as it is
123 parsed
Simon Glass24725af2020-07-05 21:41:49 -0600124
Simon Glass26132882012-01-14 15:12:45 +0000125 Returns:
Simon Glass0495abf2013-03-26 13:09:39 +0000126 namedtuple containing:
127 ok: False=failure, True=ok
Simon Glass26132882012-01-14 15:12:45 +0000128 problems: List of problems, each a dict:
129 'type'; error or warning
130 'msg': text message
131 'file' : filename
132 'line': line number
Simon Glass0495abf2013-03-26 13:09:39 +0000133 errors: Number of errors
134 warnings: Number of warnings
135 checks: Number of checks
Simon Glass26132882012-01-14 15:12:45 +0000136 lines: Number of lines
Evan Bennec7378b2021-04-01 13:49:30 +1100137 stdout: checkpatch_output
Simon Glass26132882012-01-14 15:12:45 +0000138 """
Simon Glass0495abf2013-03-26 13:09:39 +0000139 fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines',
140 'stdout']
141 result = collections.namedtuple('CheckPatchResult', fields)
Evan Bennec7378b2021-04-01 13:49:30 +1100142 result.stdout = checkpatch_output
Simon Glass0495abf2013-03-26 13:09:39 +0000143 result.ok = False
Simon Glass39d6d842020-05-06 16:29:04 -0600144 result.errors, result.warnings, result.checks = 0, 0, 0
Simon Glass0495abf2013-03-26 13:09:39 +0000145 result.lines = 0
146 result.problems = []
Simon Glass26132882012-01-14 15:12:45 +0000147
148 # total: 0 errors, 0 warnings, 159 lines checked
Simon Glass0495abf2013-03-26 13:09:39 +0000149 # or:
150 # total: 0 errors, 2 warnings, 7 checks, 473 lines checked
Evan Bennec7378b2021-04-01 13:49:30 +1100151 emacs_stats = r'(?:[0-9]{4}.*\.patch )?'
Simon Glass396168c2020-05-06 16:29:05 -0600152 re_stats = re.compile(emacs_stats +
Evan Bennec7378b2021-04-01 13:49:30 +1100153 r'total: (\d+) errors, (\d+) warnings, (\d+)')
Simon Glass396168c2020-05-06 16:29:05 -0600154 re_stats_full = re.compile(emacs_stats +
Evan Bennec7378b2021-04-01 13:49:30 +1100155 r'total: (\d+) errors, (\d+) warnings, (\d+)'
156 r' checks, (\d+)')
157 re_ok = re.compile(r'.*has no obvious style problems')
158 re_bad = re.compile(r'.*has style problems, please review')
159
160 # A blank line indicates the end of a message
161 for message in result.stdout.split('\n\n'):
Simon Glass26132882012-01-14 15:12:45 +0000162 if verbose:
Evan Bennec7378b2021-04-01 13:49:30 +1100163 print(message)
Simon Glass26132882012-01-14 15:12:45 +0000164
Evan Bennec7378b2021-04-01 13:49:30 +1100165 # either find stats, the verdict, or delegate
166 match = re_stats_full.match(message)
Simon Glass0495abf2013-03-26 13:09:39 +0000167 if not match:
Evan Bennec7378b2021-04-01 13:49:30 +1100168 match = re_stats.match(message)
Simon Glass26132882012-01-14 15:12:45 +0000169 if match:
Simon Glass0495abf2013-03-26 13:09:39 +0000170 result.errors = int(match.group(1))
171 result.warnings = int(match.group(2))
172 if len(match.groups()) == 4:
173 result.checks = int(match.group(3))
174 result.lines = int(match.group(4))
175 else:
176 result.lines = int(match.group(3))
Evan Bennec7378b2021-04-01 13:49:30 +1100177 elif re_ok.match(message):
Simon Glass0495abf2013-03-26 13:09:39 +0000178 result.ok = True
Evan Bennec7378b2021-04-01 13:49:30 +1100179 elif re_bad.match(message):
Simon Glass0495abf2013-03-26 13:09:39 +0000180 result.ok = False
Simon Glass7a543652020-05-06 16:29:09 -0600181 else:
Evan Bennec7378b2021-04-01 13:49:30 +1100182 problem = CheckPatchParseOneMessage(message)
183 if problem:
184 result.problems.append(problem)
Simon Glass26132882012-01-14 15:12:45 +0000185
Simon Glass0495abf2013-03-26 13:09:39 +0000186 return result
Simon Glass26132882012-01-14 15:12:45 +0000187
Evan Bennec7378b2021-04-01 13:49:30 +1100188
189def CheckPatch(fname, verbose=False, show_types=False):
190 """Run checkpatch.pl on a file and parse the results.
191
192 Args:
193 fname: Filename to check
194 verbose: True to print out every line of the checkpatch output as it is
195 parsed
196 show_types: Tell checkpatch to show the type (number) of each message
197
198 Returns:
199 namedtuple containing:
200 ok: False=failure, True=ok
201 problems: List of problems, each a dict:
202 'type'; error or warning
203 'msg': text message
204 'file' : filename
205 'line': line number
206 errors: Number of errors
207 warnings: Number of warnings
208 checks: Number of checks
209 lines: Number of lines
210 stdout: Full output of checkpatch
211 """
212 chk = FindCheckPatch()
213 args = [chk, '--no-tree']
214 if show_types:
215 args.append('--show-types')
216 output = command.Output(*args, fname, raise_on_error=False)
217
218 return CheckPatchParse(output, verbose)
219
220
Simon Glass26132882012-01-14 15:12:45 +0000221def GetWarningMsg(col, msg_type, fname, line, msg):
222 '''Create a message for a given file/line
223
224 Args:
225 msg_type: Message type ('error' or 'warning')
226 fname: Filename which reports the problem
227 line: Line number where it was noticed
228 msg: Message to report
229 '''
230 if msg_type == 'warning':
231 msg_type = col.Color(col.YELLOW, msg_type)
232 elif msg_type == 'error':
233 msg_type = col.Color(col.RED, msg_type)
Simon Glass0495abf2013-03-26 13:09:39 +0000234 elif msg_type == 'check':
235 msg_type = col.Color(col.MAGENTA, msg_type)
Simon Glassd2d136c2020-05-06 16:29:08 -0600236 line_str = '' if line is None else '%d' % line
237 return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg)
Simon Glass26132882012-01-14 15:12:45 +0000238
239def CheckPatches(verbose, args):
240 '''Run the checkpatch.pl script on each patch'''
Simon Glass0495abf2013-03-26 13:09:39 +0000241 error_count, warning_count, check_count = 0, 0, 0
Simon Glass26132882012-01-14 15:12:45 +0000242 col = terminal.Color()
243
244 for fname in args:
Simon Glass0495abf2013-03-26 13:09:39 +0000245 result = CheckPatch(fname, verbose)
246 if not result.ok:
247 error_count += result.errors
248 warning_count += result.warnings
249 check_count += result.checks
Paul Burtonc3931342016-09-27 16:03:50 +0100250 print('%d errors, %d warnings, %d checks for %s:' % (result.errors,
251 result.warnings, result.checks, col.Color(col.BLUE, fname)))
Simon Glass0495abf2013-03-26 13:09:39 +0000252 if (len(result.problems) != result.errors + result.warnings +
253 result.checks):
Paul Burtonc3931342016-09-27 16:03:50 +0100254 print("Internal error: some problems lost")
Simon Glass0495abf2013-03-26 13:09:39 +0000255 for item in result.problems:
Simon Glassc004a5f2017-01-17 16:52:23 -0700256 sys.stderr.write(
257 GetWarningMsg(col, item.get('type', '<unknown>'),
Simon Glasseb1526c2012-09-27 15:33:46 +0000258 item.get('file', '<unknown>'),
Paul Burtonc3931342016-09-27 16:03:50 +0100259 item.get('line', 0), item.get('msg', 'message')))
Simon Glass0495abf2013-03-26 13:09:39 +0000260 print
Paul Burtonc3931342016-09-27 16:03:50 +0100261 #print(stdout)
Simon Glass0495abf2013-03-26 13:09:39 +0000262 if error_count or warning_count or check_count:
263 str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
Simon Glass26132882012-01-14 15:12:45 +0000264 color = col.GREEN
265 if warning_count:
266 color = col.YELLOW
267 if error_count:
268 color = col.RED
Paul Burtonc3931342016-09-27 16:03:50 +0100269 print(col.Color(color, str % (error_count, warning_count, check_count)))
Simon Glass26132882012-01-14 15:12:45 +0000270 return False
271 return True