blob: 63a8e37e8c5478012b3de6ee8da3a79f3bf96e0e [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
13from patman import tools
Simon Glass26132882012-01-14 15:12:45 +000014
15def FindCheckPatch():
Doug Anderson4baede02012-11-26 15:23:23 +000016 top_level = gitutil.GetTopLevel()
Simon Glass26132882012-01-14 15:12:45 +000017 try_list = [
18 os.getcwd(),
19 os.path.join(os.getcwd(), '..', '..'),
Doug Anderson4baede02012-11-26 15:23:23 +000020 os.path.join(top_level, 'tools'),
21 os.path.join(top_level, 'scripts'),
Simon Glass26132882012-01-14 15:12:45 +000022 '%s/bin' % os.getenv('HOME'),
23 ]
24 # Look in current dir
25 for path in try_list:
26 fname = os.path.join(path, 'checkpatch.pl')
27 if os.path.isfile(fname):
28 return fname
29
30 # Look upwwards for a Chrome OS tree
31 while not os.path.ismount(path):
32 fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files',
33 'scripts', 'checkpatch.pl')
34 if os.path.isfile(fname):
35 return fname
36 path = os.path.dirname(path)
Vadim Bendeburyc549f082013-01-09 16:00:10 +000037
Masahiro Yamada880828d2014-08-16 00:59:26 +090038 sys.exit('Cannot find checkpatch.pl - please put it in your ' +
39 '~/bin directory or use --no-check')
Simon Glass26132882012-01-14 15:12:45 +000040
Simon Glass08cfa412020-06-14 10:54:06 -060041def CheckPatch(fname, verbose=False, show_types=False):
Simon Glass26132882012-01-14 15:12:45 +000042 """Run checkpatch.pl on a file.
43
Simon Glass24725af2020-07-05 21:41:49 -060044 Args:
45 fname: Filename to check
46 verbose: True to print out every line of the checkpatch output as it is
47 parsed
48 show_types: Tell checkpatch to show the type (number) of each message
49
Simon Glass26132882012-01-14 15:12:45 +000050 Returns:
Simon Glass0495abf2013-03-26 13:09:39 +000051 namedtuple containing:
52 ok: False=failure, True=ok
Simon Glass26132882012-01-14 15:12:45 +000053 problems: List of problems, each a dict:
54 'type'; error or warning
55 'msg': text message
56 'file' : filename
57 'line': line number
Simon Glass0495abf2013-03-26 13:09:39 +000058 errors: Number of errors
59 warnings: Number of warnings
60 checks: Number of checks
Simon Glass26132882012-01-14 15:12:45 +000061 lines: Number of lines
Simon Glass0495abf2013-03-26 13:09:39 +000062 stdout: Full output of checkpatch
Simon Glass26132882012-01-14 15:12:45 +000063 """
Simon Glass0495abf2013-03-26 13:09:39 +000064 fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines',
65 'stdout']
66 result = collections.namedtuple('CheckPatchResult', fields)
67 result.ok = False
Simon Glass39d6d842020-05-06 16:29:04 -060068 result.errors, result.warnings, result.checks = 0, 0, 0
Simon Glass0495abf2013-03-26 13:09:39 +000069 result.lines = 0
70 result.problems = []
Simon Glass26132882012-01-14 15:12:45 +000071 chk = FindCheckPatch()
Simon Glass26132882012-01-14 15:12:45 +000072 item = {}
Simon Glass08cfa412020-06-14 10:54:06 -060073 args = [chk, '--no-tree']
74 if show_types:
75 args.append('--show-types')
76 result.stdout = command.Output(*args, fname, raise_on_error=False)
Simon Glass26132882012-01-14 15:12:45 +000077 #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
78 #stdout, stderr = pipe.communicate()
79
80 # total: 0 errors, 0 warnings, 159 lines checked
Simon Glass0495abf2013-03-26 13:09:39 +000081 # or:
82 # total: 0 errors, 2 warnings, 7 checks, 473 lines checked
Simon Glass396168c2020-05-06 16:29:05 -060083 emacs_prefix = '(?:[0-9]{4}.*\.patch:[0-9]+: )?'
84 emacs_stats = '(?:[0-9]{4}.*\.patch )?'
85 re_stats = re.compile(emacs_stats +
86 'total: (\\d+) errors, (\d+) warnings, (\d+)')
87 re_stats_full = re.compile(emacs_stats +
88 'total: (\\d+) errors, (\d+) warnings, (\d+)'
Simon Glass0495abf2013-03-26 13:09:39 +000089 ' checks, (\d+)')
Simon Glass26132882012-01-14 15:12:45 +000090 re_ok = re.compile('.*has no obvious style problems')
91 re_bad = re.compile('.*has style problems, please review')
Simon Glass08cfa412020-06-14 10:54:06 -060092 type_name = '([A-Z_]+:)?'
93 re_error = re.compile('ERROR:%s (.*)' % type_name)
94 re_warning = re.compile(emacs_prefix + 'WARNING:%s (.*)' % type_name)
95 re_check = re.compile('CHECK:%s (.*)' % type_name)
Simon Glass8cf23642020-10-29 21:46:33 -060096 re_file = re.compile('#(\d+): (FILE: ([^:]*):(\d+):)?')
Simon Glass9a690c42020-05-06 16:29:07 -060097 re_note = re.compile('NOTE: (.*)')
Simon Glass5c906fb2020-10-29 21:46:31 -060098 re_new_file = re.compile('new file mode .*')
Simon Glass9a690c42020-05-06 16:29:07 -060099 indent = ' ' * 6
Simon Glass0495abf2013-03-26 13:09:39 +0000100 for line in result.stdout.splitlines():
Simon Glass26132882012-01-14 15:12:45 +0000101 if verbose:
Paul Burtonc3931342016-09-27 16:03:50 +0100102 print(line)
Simon Glass26132882012-01-14 15:12:45 +0000103
104 # A blank line indicates the end of a message
Simon Glass9eb74a92020-05-06 16:29:06 -0600105 if not line:
106 if item:
107 result.problems.append(item)
108 item = {}
109 continue
Simon Glass9a690c42020-05-06 16:29:07 -0600110 if re_note.match(line):
111 continue
112 # Skip lines which quote code
113 if line.startswith(indent):
114 continue
Simon Glass5c906fb2020-10-29 21:46:31 -0600115 # Skip code quotes
116 if line.startswith('+'):
117 continue
118 if re_new_file.match(line):
Simon Glass9a690c42020-05-06 16:29:07 -0600119 continue
Simon Glass0495abf2013-03-26 13:09:39 +0000120 match = re_stats_full.match(line)
121 if not match:
122 match = re_stats.match(line)
Simon Glass26132882012-01-14 15:12:45 +0000123 if match:
Simon Glass0495abf2013-03-26 13:09:39 +0000124 result.errors = int(match.group(1))
125 result.warnings = int(match.group(2))
126 if len(match.groups()) == 4:
127 result.checks = int(match.group(3))
128 result.lines = int(match.group(4))
129 else:
130 result.lines = int(match.group(3))
Simon Glass9eb74a92020-05-06 16:29:06 -0600131 continue
Simon Glass26132882012-01-14 15:12:45 +0000132 elif re_ok.match(line):
Simon Glass0495abf2013-03-26 13:09:39 +0000133 result.ok = True
Simon Glass9eb74a92020-05-06 16:29:06 -0600134 continue
Simon Glass26132882012-01-14 15:12:45 +0000135 elif re_bad.match(line):
Simon Glass0495abf2013-03-26 13:09:39 +0000136 result.ok = False
Simon Glass9eb74a92020-05-06 16:29:06 -0600137 continue
Simon Glass0495abf2013-03-26 13:09:39 +0000138 err_match = re_error.match(line)
139 warn_match = re_warning.match(line)
140 file_match = re_file.match(line)
141 check_match = re_check.match(line)
Simon Glassd2d136c2020-05-06 16:29:08 -0600142 subject_match = line.startswith('Subject:')
Simon Glass0495abf2013-03-26 13:09:39 +0000143 if err_match:
Simon Glass08cfa412020-06-14 10:54:06 -0600144 item['cptype'] = err_match.group(1)
145 item['msg'] = err_match.group(2)
Simon Glass26132882012-01-14 15:12:45 +0000146 item['type'] = 'error'
Simon Glass0495abf2013-03-26 13:09:39 +0000147 elif warn_match:
Simon Glass08cfa412020-06-14 10:54:06 -0600148 item['cptype'] = warn_match.group(1)
149 item['msg'] = warn_match.group(2)
Simon Glass26132882012-01-14 15:12:45 +0000150 item['type'] = 'warning'
Simon Glass0495abf2013-03-26 13:09:39 +0000151 elif check_match:
Simon Glass08cfa412020-06-14 10:54:06 -0600152 item['cptype'] = check_match.group(1)
153 item['msg'] = check_match.group(2)
Simon Glass0495abf2013-03-26 13:09:39 +0000154 item['type'] = 'check'
155 elif file_match:
Simon Glass8cf23642020-10-29 21:46:33 -0600156 err_fname = file_match.group(3)
157 if err_fname:
158 item['file'] = err_fname
159 item['line'] = int(file_match.group(4))
160 else:
161 item['file'] = '<patch>'
162 item['line'] = int(file_match.group(1))
Simon Glassd2d136c2020-05-06 16:29:08 -0600163 elif subject_match:
164 item['file'] = '<patch subject>'
165 item['line'] = None
Simon Glass7a543652020-05-06 16:29:09 -0600166 else:
167 print('bad line "%s", %d' % (line, len(line)))
Simon Glass26132882012-01-14 15:12:45 +0000168
Simon Glass0495abf2013-03-26 13:09:39 +0000169 return result
Simon Glass26132882012-01-14 15:12:45 +0000170
171def GetWarningMsg(col, msg_type, fname, line, msg):
172 '''Create a message for a given file/line
173
174 Args:
175 msg_type: Message type ('error' or 'warning')
176 fname: Filename which reports the problem
177 line: Line number where it was noticed
178 msg: Message to report
179 '''
180 if msg_type == 'warning':
181 msg_type = col.Color(col.YELLOW, msg_type)
182 elif msg_type == 'error':
183 msg_type = col.Color(col.RED, msg_type)
Simon Glass0495abf2013-03-26 13:09:39 +0000184 elif msg_type == 'check':
185 msg_type = col.Color(col.MAGENTA, msg_type)
Simon Glassd2d136c2020-05-06 16:29:08 -0600186 line_str = '' if line is None else '%d' % line
187 return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg)
Simon Glass26132882012-01-14 15:12:45 +0000188
189def CheckPatches(verbose, args):
190 '''Run the checkpatch.pl script on each patch'''
Simon Glass0495abf2013-03-26 13:09:39 +0000191 error_count, warning_count, check_count = 0, 0, 0
Simon Glass26132882012-01-14 15:12:45 +0000192 col = terminal.Color()
193
194 for fname in args:
Simon Glass0495abf2013-03-26 13:09:39 +0000195 result = CheckPatch(fname, verbose)
196 if not result.ok:
197 error_count += result.errors
198 warning_count += result.warnings
199 check_count += result.checks
Paul Burtonc3931342016-09-27 16:03:50 +0100200 print('%d errors, %d warnings, %d checks for %s:' % (result.errors,
201 result.warnings, result.checks, col.Color(col.BLUE, fname)))
Simon Glass0495abf2013-03-26 13:09:39 +0000202 if (len(result.problems) != result.errors + result.warnings +
203 result.checks):
Paul Burtonc3931342016-09-27 16:03:50 +0100204 print("Internal error: some problems lost")
Simon Glass0495abf2013-03-26 13:09:39 +0000205 for item in result.problems:
Simon Glassc004a5f2017-01-17 16:52:23 -0700206 sys.stderr.write(
207 GetWarningMsg(col, item.get('type', '<unknown>'),
Simon Glasseb1526c2012-09-27 15:33:46 +0000208 item.get('file', '<unknown>'),
Paul Burtonc3931342016-09-27 16:03:50 +0100209 item.get('line', 0), item.get('msg', 'message')))
Simon Glass0495abf2013-03-26 13:09:39 +0000210 print
Paul Burtonc3931342016-09-27 16:03:50 +0100211 #print(stdout)
Simon Glass0495abf2013-03-26 13:09:39 +0000212 if error_count or warning_count or check_count:
213 str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
Simon Glass26132882012-01-14 15:12:45 +0000214 color = col.GREEN
215 if warning_count:
216 color = col.YELLOW
217 if error_count:
218 color = col.RED
Paul Burtonc3931342016-09-27 16:03:50 +0100219 print(col.Color(color, str % (error_count, warning_count, check_count)))
Simon Glass26132882012-01-14 15:12:45 +0000220 return False
221 return True