blob: 63a8e37e8c5478012b3de6ee8da3a79f3bf96e0e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass0d24de92012-01-14 15:12:45 +00002# Copyright (c) 2011 The Chromium OS Authors.
3#
Simon Glass0d24de92012-01-14 15:12:45 +00004
Simon Glassd29fe6e2013-03-26 13:09:39 +00005import collections
Simon Glass0d24de92012-01-14 15:12:45 +00006import os
7import re
Vadim Bendebury99adf6e2013-01-09 16:00:10 +00008import sys
Simon Glassbf776672020-04-17 18:09:04 -06009
10from patman import command
11from patman import gitutil
12from patman import terminal
13from patman import tools
Simon Glass0d24de92012-01-14 15:12:45 +000014
15def FindCheckPatch():
Doug Andersond96ef372012-11-26 15:23:23 +000016 top_level = gitutil.GetTopLevel()
Simon Glass0d24de92012-01-14 15:12:45 +000017 try_list = [
18 os.getcwd(),
19 os.path.join(os.getcwd(), '..', '..'),
Doug Andersond96ef372012-11-26 15:23:23 +000020 os.path.join(top_level, 'tools'),
21 os.path.join(top_level, 'scripts'),
Simon Glass0d24de92012-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 Bendebury99adf6e2013-01-09 16:00:10 +000037
Masahiro Yamada31e21412014-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 Glass0d24de92012-01-14 15:12:45 +000040
Simon Glass89fb8b72020-06-14 10:54:06 -060041def CheckPatch(fname, verbose=False, show_types=False):
Simon Glass0d24de92012-01-14 15:12:45 +000042 """Run checkpatch.pl on a file.
43
Simon Glass7d5b04e2020-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 Glass0d24de92012-01-14 15:12:45 +000050 Returns:
Simon Glassd29fe6e2013-03-26 13:09:39 +000051 namedtuple containing:
52 ok: False=failure, True=ok
Simon Glass0d24de92012-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 Glassd29fe6e2013-03-26 13:09:39 +000058 errors: Number of errors
59 warnings: Number of warnings
60 checks: Number of checks
Simon Glass0d24de92012-01-14 15:12:45 +000061 lines: Number of lines
Simon Glassd29fe6e2013-03-26 13:09:39 +000062 stdout: Full output of checkpatch
Simon Glass0d24de92012-01-14 15:12:45 +000063 """
Simon Glassd29fe6e2013-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 Glass870bd562020-05-06 16:29:04 -060068 result.errors, result.warnings, result.checks = 0, 0, 0
Simon Glassd29fe6e2013-03-26 13:09:39 +000069 result.lines = 0
70 result.problems = []
Simon Glass0d24de92012-01-14 15:12:45 +000071 chk = FindCheckPatch()
Simon Glass0d24de92012-01-14 15:12:45 +000072 item = {}
Simon Glass89fb8b72020-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 Glass0d24de92012-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 Glassd29fe6e2013-03-26 13:09:39 +000081 # or:
82 # total: 0 errors, 2 warnings, 7 checks, 473 lines checked
Simon Glass7d5b5e82020-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 Glassd29fe6e2013-03-26 13:09:39 +000089 ' checks, (\d+)')
Simon Glass0d24de92012-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 Glass89fb8b72020-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 Glassb3348522020-10-29 21:46:33 -060096 re_file = re.compile('#(\d+): (FILE: ([^:]*):(\d+):)?')
Simon Glass666eb152020-05-06 16:29:07 -060097 re_note = re.compile('NOTE: (.*)')
Simon Glassb5e18812020-10-29 21:46:31 -060098 re_new_file = re.compile('new file mode .*')
Simon Glass666eb152020-05-06 16:29:07 -060099 indent = ' ' * 6
Simon Glassd29fe6e2013-03-26 13:09:39 +0000100 for line in result.stdout.splitlines():
Simon Glass0d24de92012-01-14 15:12:45 +0000101 if verbose:
Paul Burtona920a172016-09-27 16:03:50 +0100102 print(line)
Simon Glass0d24de92012-01-14 15:12:45 +0000103
104 # A blank line indicates the end of a message
Simon Glass7175b082020-05-06 16:29:06 -0600105 if not line:
106 if item:
107 result.problems.append(item)
108 item = {}
109 continue
Simon Glass666eb152020-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 Glassb5e18812020-10-29 21:46:31 -0600115 # Skip code quotes
116 if line.startswith('+'):
117 continue
118 if re_new_file.match(line):
Simon Glass666eb152020-05-06 16:29:07 -0600119 continue
Simon Glassd29fe6e2013-03-26 13:09:39 +0000120 match = re_stats_full.match(line)
121 if not match:
122 match = re_stats.match(line)
Simon Glass0d24de92012-01-14 15:12:45 +0000123 if match:
Simon Glassd29fe6e2013-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 Glass7175b082020-05-06 16:29:06 -0600131 continue
Simon Glass0d24de92012-01-14 15:12:45 +0000132 elif re_ok.match(line):
Simon Glassd29fe6e2013-03-26 13:09:39 +0000133 result.ok = True
Simon Glass7175b082020-05-06 16:29:06 -0600134 continue
Simon Glass0d24de92012-01-14 15:12:45 +0000135 elif re_bad.match(line):
Simon Glassd29fe6e2013-03-26 13:09:39 +0000136 result.ok = False
Simon Glass7175b082020-05-06 16:29:06 -0600137 continue
Simon Glassd29fe6e2013-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 Glass37f3bb52020-05-06 16:29:08 -0600142 subject_match = line.startswith('Subject:')
Simon Glassd29fe6e2013-03-26 13:09:39 +0000143 if err_match:
Simon Glass89fb8b72020-06-14 10:54:06 -0600144 item['cptype'] = err_match.group(1)
145 item['msg'] = err_match.group(2)
Simon Glass0d24de92012-01-14 15:12:45 +0000146 item['type'] = 'error'
Simon Glassd29fe6e2013-03-26 13:09:39 +0000147 elif warn_match:
Simon Glass89fb8b72020-06-14 10:54:06 -0600148 item['cptype'] = warn_match.group(1)
149 item['msg'] = warn_match.group(2)
Simon Glass0d24de92012-01-14 15:12:45 +0000150 item['type'] = 'warning'
Simon Glassd29fe6e2013-03-26 13:09:39 +0000151 elif check_match:
Simon Glass89fb8b72020-06-14 10:54:06 -0600152 item['cptype'] = check_match.group(1)
153 item['msg'] = check_match.group(2)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000154 item['type'] = 'check'
155 elif file_match:
Simon Glassb3348522020-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 Glass37f3bb52020-05-06 16:29:08 -0600163 elif subject_match:
164 item['file'] = '<patch subject>'
165 item['line'] = None
Simon Glass96daa412020-05-06 16:29:09 -0600166 else:
167 print('bad line "%s", %d' % (line, len(line)))
Simon Glass0d24de92012-01-14 15:12:45 +0000168
Simon Glassd29fe6e2013-03-26 13:09:39 +0000169 return result
Simon Glass0d24de92012-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 Glassd29fe6e2013-03-26 13:09:39 +0000184 elif msg_type == 'check':
185 msg_type = col.Color(col.MAGENTA, msg_type)
Simon Glass37f3bb52020-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 Glass0d24de92012-01-14 15:12:45 +0000188
189def CheckPatches(verbose, args):
190 '''Run the checkpatch.pl script on each patch'''
Simon Glassd29fe6e2013-03-26 13:09:39 +0000191 error_count, warning_count, check_count = 0, 0, 0
Simon Glass0d24de92012-01-14 15:12:45 +0000192 col = terminal.Color()
193
194 for fname in args:
Simon Glassd29fe6e2013-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 Burtona920a172016-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 Glassd29fe6e2013-03-26 13:09:39 +0000202 if (len(result.problems) != result.errors + result.warnings +
203 result.checks):
Paul Burtona920a172016-09-27 16:03:50 +0100204 print("Internal error: some problems lost")
Simon Glassd29fe6e2013-03-26 13:09:39 +0000205 for item in result.problems:
Simon Glass8aa41362017-01-17 16:52:23 -0700206 sys.stderr.write(
207 GetWarningMsg(col, item.get('type', '<unknown>'),
Simon Glassafb9bf52012-09-27 15:33:46 +0000208 item.get('file', '<unknown>'),
Paul Burtona920a172016-09-27 16:03:50 +0100209 item.get('line', 0), item.get('msg', 'message')))
Simon Glassd29fe6e2013-03-26 13:09:39 +0000210 print
Paul Burtona920a172016-09-27 16:03:50 +0100211 #print(stdout)
Simon Glassd29fe6e2013-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 Glass0d24de92012-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 Burtona920a172016-09-27 16:03:50 +0100219 print(col.Color(color, str % (error_count, warning_count, check_count)))
Simon Glass0d24de92012-01-14 15:12:45 +0000220 return False
221 return True