blob: 07c3e2739abee4931f453aec64b26b09c3da776a [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
44 Returns:
Simon Glassd29fe6e2013-03-26 13:09:39 +000045 namedtuple containing:
46 ok: False=failure, True=ok
Simon Glass0d24de92012-01-14 15:12:45 +000047 problems: List of problems, each a dict:
48 'type'; error or warning
49 'msg': text message
50 'file' : filename
51 'line': line number
Simon Glassd29fe6e2013-03-26 13:09:39 +000052 errors: Number of errors
53 warnings: Number of warnings
54 checks: Number of checks
Simon Glass0d24de92012-01-14 15:12:45 +000055 lines: Number of lines
Simon Glassd29fe6e2013-03-26 13:09:39 +000056 stdout: Full output of checkpatch
Simon Glass0d24de92012-01-14 15:12:45 +000057 """
Simon Glassd29fe6e2013-03-26 13:09:39 +000058 fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines',
59 'stdout']
60 result = collections.namedtuple('CheckPatchResult', fields)
61 result.ok = False
Simon Glass870bd562020-05-06 16:29:04 -060062 result.errors, result.warnings, result.checks = 0, 0, 0
Simon Glassd29fe6e2013-03-26 13:09:39 +000063 result.lines = 0
64 result.problems = []
Simon Glass0d24de92012-01-14 15:12:45 +000065 chk = FindCheckPatch()
Simon Glass0d24de92012-01-14 15:12:45 +000066 item = {}
Simon Glass89fb8b72020-06-14 10:54:06 -060067 args = [chk, '--no-tree']
68 if show_types:
69 args.append('--show-types')
70 result.stdout = command.Output(*args, fname, raise_on_error=False)
Simon Glass0d24de92012-01-14 15:12:45 +000071 #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
72 #stdout, stderr = pipe.communicate()
73
74 # total: 0 errors, 0 warnings, 159 lines checked
Simon Glassd29fe6e2013-03-26 13:09:39 +000075 # or:
76 # total: 0 errors, 2 warnings, 7 checks, 473 lines checked
Simon Glass7d5b5e82020-05-06 16:29:05 -060077 emacs_prefix = '(?:[0-9]{4}.*\.patch:[0-9]+: )?'
78 emacs_stats = '(?:[0-9]{4}.*\.patch )?'
79 re_stats = re.compile(emacs_stats +
80 'total: (\\d+) errors, (\d+) warnings, (\d+)')
81 re_stats_full = re.compile(emacs_stats +
82 'total: (\\d+) errors, (\d+) warnings, (\d+)'
Simon Glassd29fe6e2013-03-26 13:09:39 +000083 ' checks, (\d+)')
Simon Glass0d24de92012-01-14 15:12:45 +000084 re_ok = re.compile('.*has no obvious style problems')
85 re_bad = re.compile('.*has style problems, please review')
Simon Glass89fb8b72020-06-14 10:54:06 -060086 type_name = '([A-Z_]+:)?'
87 re_error = re.compile('ERROR:%s (.*)' % type_name)
88 re_warning = re.compile(emacs_prefix + 'WARNING:%s (.*)' % type_name)
89 re_check = re.compile('CHECK:%s (.*)' % type_name)
Simon Glass0d24de92012-01-14 15:12:45 +000090 re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):')
Simon Glass666eb152020-05-06 16:29:07 -060091 re_note = re.compile('NOTE: (.*)')
92 indent = ' ' * 6
Simon Glassd29fe6e2013-03-26 13:09:39 +000093 for line in result.stdout.splitlines():
Simon Glass0d24de92012-01-14 15:12:45 +000094 if verbose:
Paul Burtona920a172016-09-27 16:03:50 +010095 print(line)
Simon Glass0d24de92012-01-14 15:12:45 +000096
97 # A blank line indicates the end of a message
Simon Glass7175b082020-05-06 16:29:06 -060098 if not line:
99 if item:
100 result.problems.append(item)
101 item = {}
102 continue
Simon Glass666eb152020-05-06 16:29:07 -0600103 if re_note.match(line):
104 continue
105 # Skip lines which quote code
106 if line.startswith(indent):
107 continue
108 # Skip code quotes and #<n>
109 if line.startswith('+') or line.startswith('#'):
110 continue
Simon Glassd29fe6e2013-03-26 13:09:39 +0000111 match = re_stats_full.match(line)
112 if not match:
113 match = re_stats.match(line)
Simon Glass0d24de92012-01-14 15:12:45 +0000114 if match:
Simon Glassd29fe6e2013-03-26 13:09:39 +0000115 result.errors = int(match.group(1))
116 result.warnings = int(match.group(2))
117 if len(match.groups()) == 4:
118 result.checks = int(match.group(3))
119 result.lines = int(match.group(4))
120 else:
121 result.lines = int(match.group(3))
Simon Glass7175b082020-05-06 16:29:06 -0600122 continue
Simon Glass0d24de92012-01-14 15:12:45 +0000123 elif re_ok.match(line):
Simon Glassd29fe6e2013-03-26 13:09:39 +0000124 result.ok = True
Simon Glass7175b082020-05-06 16:29:06 -0600125 continue
Simon Glass0d24de92012-01-14 15:12:45 +0000126 elif re_bad.match(line):
Simon Glassd29fe6e2013-03-26 13:09:39 +0000127 result.ok = False
Simon Glass7175b082020-05-06 16:29:06 -0600128 continue
Simon Glassd29fe6e2013-03-26 13:09:39 +0000129 err_match = re_error.match(line)
130 warn_match = re_warning.match(line)
131 file_match = re_file.match(line)
132 check_match = re_check.match(line)
Simon Glass37f3bb52020-05-06 16:29:08 -0600133 subject_match = line.startswith('Subject:')
Simon Glassd29fe6e2013-03-26 13:09:39 +0000134 if err_match:
Simon Glass89fb8b72020-06-14 10:54:06 -0600135 item['cptype'] = err_match.group(1)
136 item['msg'] = err_match.group(2)
Simon Glass0d24de92012-01-14 15:12:45 +0000137 item['type'] = 'error'
Simon Glassd29fe6e2013-03-26 13:09:39 +0000138 elif warn_match:
Simon Glass89fb8b72020-06-14 10:54:06 -0600139 item['cptype'] = warn_match.group(1)
140 item['msg'] = warn_match.group(2)
Simon Glass0d24de92012-01-14 15:12:45 +0000141 item['type'] = 'warning'
Simon Glassd29fe6e2013-03-26 13:09:39 +0000142 elif check_match:
Simon Glass89fb8b72020-06-14 10:54:06 -0600143 item['cptype'] = check_match.group(1)
144 item['msg'] = check_match.group(2)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000145 item['type'] = 'check'
146 elif file_match:
147 item['file'] = file_match.group(1)
148 item['line'] = int(file_match.group(2))
Simon Glass37f3bb52020-05-06 16:29:08 -0600149 elif subject_match:
150 item['file'] = '<patch subject>'
151 item['line'] = None
Simon Glass96daa412020-05-06 16:29:09 -0600152 else:
153 print('bad line "%s", %d' % (line, len(line)))
Simon Glass0d24de92012-01-14 15:12:45 +0000154
Simon Glassd29fe6e2013-03-26 13:09:39 +0000155 return result
Simon Glass0d24de92012-01-14 15:12:45 +0000156
157def GetWarningMsg(col, msg_type, fname, line, msg):
158 '''Create a message for a given file/line
159
160 Args:
161 msg_type: Message type ('error' or 'warning')
162 fname: Filename which reports the problem
163 line: Line number where it was noticed
164 msg: Message to report
165 '''
166 if msg_type == 'warning':
167 msg_type = col.Color(col.YELLOW, msg_type)
168 elif msg_type == 'error':
169 msg_type = col.Color(col.RED, msg_type)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000170 elif msg_type == 'check':
171 msg_type = col.Color(col.MAGENTA, msg_type)
Simon Glass37f3bb52020-05-06 16:29:08 -0600172 line_str = '' if line is None else '%d' % line
173 return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg)
Simon Glass0d24de92012-01-14 15:12:45 +0000174
175def CheckPatches(verbose, args):
176 '''Run the checkpatch.pl script on each patch'''
Simon Glassd29fe6e2013-03-26 13:09:39 +0000177 error_count, warning_count, check_count = 0, 0, 0
Simon Glass0d24de92012-01-14 15:12:45 +0000178 col = terminal.Color()
179
180 for fname in args:
Simon Glassd29fe6e2013-03-26 13:09:39 +0000181 result = CheckPatch(fname, verbose)
182 if not result.ok:
183 error_count += result.errors
184 warning_count += result.warnings
185 check_count += result.checks
Paul Burtona920a172016-09-27 16:03:50 +0100186 print('%d errors, %d warnings, %d checks for %s:' % (result.errors,
187 result.warnings, result.checks, col.Color(col.BLUE, fname)))
Simon Glassd29fe6e2013-03-26 13:09:39 +0000188 if (len(result.problems) != result.errors + result.warnings +
189 result.checks):
Paul Burtona920a172016-09-27 16:03:50 +0100190 print("Internal error: some problems lost")
Simon Glassd29fe6e2013-03-26 13:09:39 +0000191 for item in result.problems:
Simon Glass8aa41362017-01-17 16:52:23 -0700192 sys.stderr.write(
193 GetWarningMsg(col, item.get('type', '<unknown>'),
Simon Glassafb9bf52012-09-27 15:33:46 +0000194 item.get('file', '<unknown>'),
Paul Burtona920a172016-09-27 16:03:50 +0100195 item.get('line', 0), item.get('msg', 'message')))
Simon Glassd29fe6e2013-03-26 13:09:39 +0000196 print
Paul Burtona920a172016-09-27 16:03:50 +0100197 #print(stdout)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000198 if error_count or warning_count or check_count:
199 str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
Simon Glass0d24de92012-01-14 15:12:45 +0000200 color = col.GREEN
201 if warning_count:
202 color = col.YELLOW
203 if error_count:
204 color = col.RED
Paul Burtona920a172016-09-27 16:03:50 +0100205 print(col.Color(color, str % (error_count, warning_count, check_count)))
Simon Glass0d24de92012-01-14 15:12:45 +0000206 return False
207 return True