Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 2 | # Copyright (c) 2011 The Chromium OS Authors. |
| 3 | # |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 4 | |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 5 | import collections |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 6 | import os |
| 7 | import re |
Vadim Bendebury | c549f08 | 2013-01-09 16:00:10 +0000 | [diff] [blame] | 8 | import sys |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 9 | |
| 10 | from patman import command |
| 11 | from patman import gitutil |
| 12 | from patman import terminal |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 13 | |
| 14 | EMACS_PREFIX = r'(?:[0-9]{4}.*\.patch:[0-9]+: )?' |
| 15 | TYPE_NAME = r'([A-Z_]+:)?' |
| 16 | RE_ERROR = re.compile(r'ERROR:%s (.*)' % TYPE_NAME) |
| 17 | RE_WARNING = re.compile(EMACS_PREFIX + r'WARNING:%s (.*)' % TYPE_NAME) |
| 18 | RE_CHECK = re.compile(r'CHECK:%s (.*)' % TYPE_NAME) |
| 19 | RE_FILE = re.compile(r'#(\d+): (FILE: ([^:]*):(\d+):)?') |
| 20 | RE_NOTE = re.compile(r'NOTE: (.*)') |
| 21 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 22 | |
| 23 | def FindCheckPatch(): |
Doug Anderson | 4baede0 | 2012-11-26 15:23:23 +0000 | [diff] [blame] | 24 | top_level = gitutil.GetTopLevel() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 25 | try_list = [ |
| 26 | os.getcwd(), |
| 27 | os.path.join(os.getcwd(), '..', '..'), |
Doug Anderson | 4baede0 | 2012-11-26 15:23:23 +0000 | [diff] [blame] | 28 | os.path.join(top_level, 'tools'), |
| 29 | os.path.join(top_level, 'scripts'), |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 30 | '%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 Bendebury | c549f08 | 2013-01-09 16:00:10 +0000 | [diff] [blame] | 45 | |
Masahiro Yamada | 880828d | 2014-08-16 00:59:26 +0900 | [diff] [blame] | 46 | sys.exit('Cannot find checkpatch.pl - please put it in your ' + |
| 47 | '~/bin directory or use --no-check') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 48 | |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 49 | |
| 50 | def CheckPatchParseOneMessage(message): |
| 51 | """Parse one checkpatch message |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 52 | |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 53 | Args: |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 54 | 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 | |
| 117 | def CheckPatchParse(checkpatch_output, verbose=False): |
| 118 | """Parse checkpatch.pl output |
| 119 | |
| 120 | Args: |
| 121 | checkpatch_output: string to parse |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 122 | verbose: True to print out every line of the checkpatch output as it is |
| 123 | parsed |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 124 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 125 | Returns: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 126 | namedtuple containing: |
| 127 | ok: False=failure, True=ok |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 128 | problems: List of problems, each a dict: |
| 129 | 'type'; error or warning |
| 130 | 'msg': text message |
| 131 | 'file' : filename |
| 132 | 'line': line number |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 133 | errors: Number of errors |
| 134 | warnings: Number of warnings |
| 135 | checks: Number of checks |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 136 | lines: Number of lines |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 137 | stdout: checkpatch_output |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 138 | """ |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 139 | fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines', |
| 140 | 'stdout'] |
| 141 | result = collections.namedtuple('CheckPatchResult', fields) |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 142 | result.stdout = checkpatch_output |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 143 | result.ok = False |
Simon Glass | 39d6d84 | 2020-05-06 16:29:04 -0600 | [diff] [blame] | 144 | result.errors, result.warnings, result.checks = 0, 0, 0 |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 145 | result.lines = 0 |
| 146 | result.problems = [] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 147 | |
| 148 | # total: 0 errors, 0 warnings, 159 lines checked |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 149 | # or: |
| 150 | # total: 0 errors, 2 warnings, 7 checks, 473 lines checked |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 151 | emacs_stats = r'(?:[0-9]{4}.*\.patch )?' |
Simon Glass | 396168c | 2020-05-06 16:29:05 -0600 | [diff] [blame] | 152 | re_stats = re.compile(emacs_stats + |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 153 | r'total: (\d+) errors, (\d+) warnings, (\d+)') |
Simon Glass | 396168c | 2020-05-06 16:29:05 -0600 | [diff] [blame] | 154 | re_stats_full = re.compile(emacs_stats + |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 155 | 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 Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 162 | if verbose: |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 163 | print(message) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 164 | |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 165 | # either find stats, the verdict, or delegate |
| 166 | match = re_stats_full.match(message) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 167 | if not match: |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 168 | match = re_stats.match(message) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 169 | if match: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 170 | 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 Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 177 | elif re_ok.match(message): |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 178 | result.ok = True |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 179 | elif re_bad.match(message): |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 180 | result.ok = False |
Simon Glass | 7a54365 | 2020-05-06 16:29:09 -0600 | [diff] [blame] | 181 | else: |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 182 | problem = CheckPatchParseOneMessage(message) |
| 183 | if problem: |
| 184 | result.problems.append(problem) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 185 | |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 186 | return result |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 187 | |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 188 | |
| 189 | def 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 Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 221 | def 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 Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 234 | elif msg_type == 'check': |
| 235 | msg_type = col.Color(col.MAGENTA, msg_type) |
Simon Glass | d2d136c | 2020-05-06 16:29:08 -0600 | [diff] [blame] | 236 | line_str = '' if line is None else '%d' % line |
| 237 | return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 238 | |
| 239 | def CheckPatches(verbose, args): |
| 240 | '''Run the checkpatch.pl script on each patch''' |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 241 | error_count, warning_count, check_count = 0, 0, 0 |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 242 | col = terminal.Color() |
| 243 | |
| 244 | for fname in args: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 245 | 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 Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 250 | print('%d errors, %d warnings, %d checks for %s:' % (result.errors, |
| 251 | result.warnings, result.checks, col.Color(col.BLUE, fname))) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 252 | if (len(result.problems) != result.errors + result.warnings + |
| 253 | result.checks): |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 254 | print("Internal error: some problems lost") |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 255 | for item in result.problems: |
Simon Glass | c004a5f | 2017-01-17 16:52:23 -0700 | [diff] [blame] | 256 | sys.stderr.write( |
| 257 | GetWarningMsg(col, item.get('type', '<unknown>'), |
Simon Glass | eb1526c | 2012-09-27 15:33:46 +0000 | [diff] [blame] | 258 | item.get('file', '<unknown>'), |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 259 | item.get('line', 0), item.get('msg', 'message'))) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 260 | print |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 261 | #print(stdout) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 262 | if error_count or warning_count or check_count: |
| 263 | str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)' |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 264 | color = col.GREEN |
| 265 | if warning_count: |
| 266 | color = col.YELLOW |
| 267 | if error_count: |
| 268 | color = col.RED |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 269 | print(col.Color(color, str % (error_count, warning_count, check_count))) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 270 | return False |
| 271 | return True |