blob: 263bac3fc90d72a475e52a68d508ed8f1900f7ff [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 Glass0d24de92012-01-14 15:12:45 +000096 re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):')
Simon Glass666eb152020-05-06 16:29:07 -060097 re_note = re.compile('NOTE: (.*)')
98 indent = ' ' * 6
Simon Glassd29fe6e2013-03-26 13:09:39 +000099 for line in result.stdout.splitlines():
Simon Glass0d24de92012-01-14 15:12:45 +0000100 if verbose:
Paul Burtona920a172016-09-27 16:03:50 +0100101 print(line)
Simon Glass0d24de92012-01-14 15:12:45 +0000102
103 # A blank line indicates the end of a message
Simon Glass7175b082020-05-06 16:29:06 -0600104 if not line:
105 if item:
106 result.problems.append(item)
107 item = {}
108 continue
Simon Glass666eb152020-05-06 16:29:07 -0600109 if re_note.match(line):
110 continue
111 # Skip lines which quote code
112 if line.startswith(indent):
113 continue
114 # Skip code quotes and #<n>
115 if line.startswith('+') or line.startswith('#'):
116 continue
Simon Glassd29fe6e2013-03-26 13:09:39 +0000117 match = re_stats_full.match(line)
118 if not match:
119 match = re_stats.match(line)
Simon Glass0d24de92012-01-14 15:12:45 +0000120 if match:
Simon Glassd29fe6e2013-03-26 13:09:39 +0000121 result.errors = int(match.group(1))
122 result.warnings = int(match.group(2))
123 if len(match.groups()) == 4:
124 result.checks = int(match.group(3))
125 result.lines = int(match.group(4))
126 else:
127 result.lines = int(match.group(3))
Simon Glass7175b082020-05-06 16:29:06 -0600128 continue
Simon Glass0d24de92012-01-14 15:12:45 +0000129 elif re_ok.match(line):
Simon Glassd29fe6e2013-03-26 13:09:39 +0000130 result.ok = True
Simon Glass7175b082020-05-06 16:29:06 -0600131 continue
Simon Glass0d24de92012-01-14 15:12:45 +0000132 elif re_bad.match(line):
Simon Glassd29fe6e2013-03-26 13:09:39 +0000133 result.ok = False
Simon Glass7175b082020-05-06 16:29:06 -0600134 continue
Simon Glassd29fe6e2013-03-26 13:09:39 +0000135 err_match = re_error.match(line)
136 warn_match = re_warning.match(line)
137 file_match = re_file.match(line)
138 check_match = re_check.match(line)
Simon Glass37f3bb52020-05-06 16:29:08 -0600139 subject_match = line.startswith('Subject:')
Simon Glassd29fe6e2013-03-26 13:09:39 +0000140 if err_match:
Simon Glass89fb8b72020-06-14 10:54:06 -0600141 item['cptype'] = err_match.group(1)
142 item['msg'] = err_match.group(2)
Simon Glass0d24de92012-01-14 15:12:45 +0000143 item['type'] = 'error'
Simon Glassd29fe6e2013-03-26 13:09:39 +0000144 elif warn_match:
Simon Glass89fb8b72020-06-14 10:54:06 -0600145 item['cptype'] = warn_match.group(1)
146 item['msg'] = warn_match.group(2)
Simon Glass0d24de92012-01-14 15:12:45 +0000147 item['type'] = 'warning'
Simon Glassd29fe6e2013-03-26 13:09:39 +0000148 elif check_match:
Simon Glass89fb8b72020-06-14 10:54:06 -0600149 item['cptype'] = check_match.group(1)
150 item['msg'] = check_match.group(2)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000151 item['type'] = 'check'
152 elif file_match:
153 item['file'] = file_match.group(1)
154 item['line'] = int(file_match.group(2))
Simon Glass37f3bb52020-05-06 16:29:08 -0600155 elif subject_match:
156 item['file'] = '<patch subject>'
157 item['line'] = None
Simon Glass96daa412020-05-06 16:29:09 -0600158 else:
159 print('bad line "%s", %d' % (line, len(line)))
Simon Glass0d24de92012-01-14 15:12:45 +0000160
Simon Glassd29fe6e2013-03-26 13:09:39 +0000161 return result
Simon Glass0d24de92012-01-14 15:12:45 +0000162
163def GetWarningMsg(col, msg_type, fname, line, msg):
164 '''Create a message for a given file/line
165
166 Args:
167 msg_type: Message type ('error' or 'warning')
168 fname: Filename which reports the problem
169 line: Line number where it was noticed
170 msg: Message to report
171 '''
172 if msg_type == 'warning':
173 msg_type = col.Color(col.YELLOW, msg_type)
174 elif msg_type == 'error':
175 msg_type = col.Color(col.RED, msg_type)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000176 elif msg_type == 'check':
177 msg_type = col.Color(col.MAGENTA, msg_type)
Simon Glass37f3bb52020-05-06 16:29:08 -0600178 line_str = '' if line is None else '%d' % line
179 return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg)
Simon Glass0d24de92012-01-14 15:12:45 +0000180
181def CheckPatches(verbose, args):
182 '''Run the checkpatch.pl script on each patch'''
Simon Glassd29fe6e2013-03-26 13:09:39 +0000183 error_count, warning_count, check_count = 0, 0, 0
Simon Glass0d24de92012-01-14 15:12:45 +0000184 col = terminal.Color()
185
186 for fname in args:
Simon Glassd29fe6e2013-03-26 13:09:39 +0000187 result = CheckPatch(fname, verbose)
188 if not result.ok:
189 error_count += result.errors
190 warning_count += result.warnings
191 check_count += result.checks
Paul Burtona920a172016-09-27 16:03:50 +0100192 print('%d errors, %d warnings, %d checks for %s:' % (result.errors,
193 result.warnings, result.checks, col.Color(col.BLUE, fname)))
Simon Glassd29fe6e2013-03-26 13:09:39 +0000194 if (len(result.problems) != result.errors + result.warnings +
195 result.checks):
Paul Burtona920a172016-09-27 16:03:50 +0100196 print("Internal error: some problems lost")
Simon Glassd29fe6e2013-03-26 13:09:39 +0000197 for item in result.problems:
Simon Glass8aa41362017-01-17 16:52:23 -0700198 sys.stderr.write(
199 GetWarningMsg(col, item.get('type', '<unknown>'),
Simon Glassafb9bf52012-09-27 15:33:46 +0000200 item.get('file', '<unknown>'),
Paul Burtona920a172016-09-27 16:03:50 +0100201 item.get('line', 0), item.get('msg', 'message')))
Simon Glassd29fe6e2013-03-26 13:09:39 +0000202 print
Paul Burtona920a172016-09-27 16:03:50 +0100203 #print(stdout)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000204 if error_count or warning_count or check_count:
205 str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
Simon Glass0d24de92012-01-14 15:12:45 +0000206 color = col.GREEN
207 if warning_count:
208 color = col.YELLOW
209 if error_count:
210 color = col.RED
Paul Burtona920a172016-09-27 16:03:50 +0100211 print(col.Color(color, str % (error_count, warning_count, check_count)))
Simon Glass0d24de92012-01-14 15:12:45 +0000212 return False
213 return True