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 | dc19cee | 2023-03-08 10:52:55 -0800 | [diff] [blame] | 6 | import concurrent.futures |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 7 | import os |
| 8 | import re |
Vadim Bendebury | c549f08 | 2013-01-09 16:00:10 +0000 | [diff] [blame] | 9 | import sys |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 10 | |
Simon Glass | a997ea5 | 2020-04-17 18:09:04 -0600 | [diff] [blame] | 11 | from patman import gitutil |
Simon Glass | 131444f | 2023-02-23 18:18:04 -0700 | [diff] [blame] | 12 | from u_boot_pylib import command |
| 13 | from u_boot_pylib import terminal |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 14 | |
| 15 | EMACS_PREFIX = r'(?:[0-9]{4}.*\.patch:[0-9]+: )?' |
| 16 | TYPE_NAME = r'([A-Z_]+:)?' |
| 17 | RE_ERROR = re.compile(r'ERROR:%s (.*)' % TYPE_NAME) |
| 18 | RE_WARNING = re.compile(EMACS_PREFIX + r'WARNING:%s (.*)' % TYPE_NAME) |
| 19 | RE_CHECK = re.compile(r'CHECK:%s (.*)' % TYPE_NAME) |
| 20 | RE_FILE = re.compile(r'#(\d+): (FILE: ([^:]*):(\d+):)?') |
| 21 | RE_NOTE = re.compile(r'NOTE: (.*)') |
| 22 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 23 | |
Simon Glass | d84e84a | 2022-01-29 14:14:06 -0700 | [diff] [blame] | 24 | def find_check_patch(): |
Simon Glass | 761648b | 2022-01-29 14:14:11 -0700 | [diff] [blame] | 25 | top_level = gitutil.get_top_level() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 26 | try_list = [ |
| 27 | os.getcwd(), |
| 28 | os.path.join(os.getcwd(), '..', '..'), |
Doug Anderson | 4baede0 | 2012-11-26 15:23:23 +0000 | [diff] [blame] | 29 | os.path.join(top_level, 'tools'), |
| 30 | os.path.join(top_level, 'scripts'), |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 31 | '%s/bin' % os.getenv('HOME'), |
| 32 | ] |
| 33 | # Look in current dir |
| 34 | for path in try_list: |
| 35 | fname = os.path.join(path, 'checkpatch.pl') |
| 36 | if os.path.isfile(fname): |
| 37 | return fname |
| 38 | |
| 39 | # Look upwwards for a Chrome OS tree |
| 40 | while not os.path.ismount(path): |
| 41 | fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files', |
| 42 | 'scripts', 'checkpatch.pl') |
| 43 | if os.path.isfile(fname): |
| 44 | return fname |
| 45 | path = os.path.dirname(path) |
Vadim Bendebury | c549f08 | 2013-01-09 16:00:10 +0000 | [diff] [blame] | 46 | |
Masahiro Yamada | 880828d | 2014-08-16 00:59:26 +0900 | [diff] [blame] | 47 | sys.exit('Cannot find checkpatch.pl - please put it in your ' + |
| 48 | '~/bin directory or use --no-check') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 49 | |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 50 | |
Simon Glass | d84e84a | 2022-01-29 14:14:06 -0700 | [diff] [blame] | 51 | def check_patch_parse_one_message(message): |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 52 | """Parse one checkpatch message |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 53 | |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 54 | Args: |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 55 | message: string to parse |
| 56 | |
| 57 | Returns: |
| 58 | dict: |
| 59 | 'type'; error or warning |
| 60 | 'msg': text message |
| 61 | 'file' : filename |
| 62 | 'line': line number |
| 63 | """ |
| 64 | |
| 65 | if RE_NOTE.match(message): |
| 66 | return {} |
| 67 | |
| 68 | item = {} |
| 69 | |
| 70 | err_match = RE_ERROR.match(message) |
| 71 | warn_match = RE_WARNING.match(message) |
| 72 | check_match = RE_CHECK.match(message) |
| 73 | if err_match: |
| 74 | item['cptype'] = err_match.group(1) |
| 75 | item['msg'] = err_match.group(2) |
| 76 | item['type'] = 'error' |
| 77 | elif warn_match: |
| 78 | item['cptype'] = warn_match.group(1) |
| 79 | item['msg'] = warn_match.group(2) |
| 80 | item['type'] = 'warning' |
| 81 | elif check_match: |
| 82 | item['cptype'] = check_match.group(1) |
| 83 | item['msg'] = check_match.group(2) |
| 84 | item['type'] = 'check' |
| 85 | else: |
| 86 | message_indent = ' ' |
| 87 | print('patman: failed to parse checkpatch message:\n%s' % |
| 88 | (message_indent + message.replace('\n', '\n' + message_indent)), |
| 89 | file=sys.stderr) |
| 90 | return {} |
| 91 | |
| 92 | file_match = RE_FILE.search(message) |
| 93 | # some messages have no file, catch those here |
| 94 | no_file_match = any(s in message for s in [ |
| 95 | '\nSubject:', 'Missing Signed-off-by: line(s)', |
| 96 | 'does MAINTAINERS need updating' |
| 97 | ]) |
| 98 | |
| 99 | if file_match: |
| 100 | err_fname = file_match.group(3) |
| 101 | if err_fname: |
| 102 | item['file'] = err_fname |
| 103 | item['line'] = int(file_match.group(4)) |
| 104 | else: |
| 105 | item['file'] = '<patch>' |
| 106 | item['line'] = int(file_match.group(1)) |
| 107 | elif no_file_match: |
| 108 | item['file'] = '<patch>' |
| 109 | else: |
| 110 | message_indent = ' ' |
| 111 | print('patman: failed to find file / line information:\n%s' % |
| 112 | (message_indent + message.replace('\n', '\n' + message_indent)), |
| 113 | file=sys.stderr) |
| 114 | |
| 115 | return item |
| 116 | |
| 117 | |
Simon Glass | d84e84a | 2022-01-29 14:14:06 -0700 | [diff] [blame] | 118 | def check_patch_parse(checkpatch_output, verbose=False): |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 119 | """Parse checkpatch.pl output |
| 120 | |
| 121 | Args: |
| 122 | checkpatch_output: string to parse |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 123 | verbose: True to print out every line of the checkpatch output as it is |
| 124 | parsed |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 125 | |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 126 | Returns: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 127 | namedtuple containing: |
| 128 | ok: False=failure, True=ok |
Simon Glass | 547cba6 | 2022-02-11 13:23:18 -0700 | [diff] [blame] | 129 | problems (list of problems): each a dict: |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 130 | 'type'; error or warning |
| 131 | 'msg': text message |
| 132 | 'file' : filename |
| 133 | 'line': line number |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 134 | errors: Number of errors |
| 135 | warnings: Number of warnings |
| 136 | checks: Number of checks |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 137 | lines: Number of lines |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 138 | stdout: checkpatch_output |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 139 | """ |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 140 | fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines', |
| 141 | 'stdout'] |
| 142 | result = collections.namedtuple('CheckPatchResult', fields) |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 143 | result.stdout = checkpatch_output |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 144 | result.ok = False |
Simon Glass | 39d6d84 | 2020-05-06 16:29:04 -0600 | [diff] [blame] | 145 | result.errors, result.warnings, result.checks = 0, 0, 0 |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 146 | result.lines = 0 |
| 147 | result.problems = [] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 148 | |
| 149 | # total: 0 errors, 0 warnings, 159 lines checked |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 150 | # or: |
| 151 | # total: 0 errors, 2 warnings, 7 checks, 473 lines checked |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 152 | emacs_stats = r'(?:[0-9]{4}.*\.patch )?' |
Simon Glass | 396168c | 2020-05-06 16:29:05 -0600 | [diff] [blame] | 153 | re_stats = re.compile(emacs_stats + |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 154 | r'total: (\d+) errors, (\d+) warnings, (\d+)') |
Simon Glass | 396168c | 2020-05-06 16:29:05 -0600 | [diff] [blame] | 155 | re_stats_full = re.compile(emacs_stats + |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 156 | r'total: (\d+) errors, (\d+) warnings, (\d+)' |
| 157 | r' checks, (\d+)') |
| 158 | re_ok = re.compile(r'.*has no obvious style problems') |
| 159 | re_bad = re.compile(r'.*has style problems, please review') |
| 160 | |
| 161 | # A blank line indicates the end of a message |
| 162 | for message in result.stdout.split('\n\n'): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 163 | if verbose: |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 164 | print(message) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 165 | |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 166 | # either find stats, the verdict, or delegate |
| 167 | match = re_stats_full.match(message) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 168 | if not match: |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 169 | match = re_stats.match(message) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 170 | if match: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 171 | result.errors = int(match.group(1)) |
| 172 | result.warnings = int(match.group(2)) |
| 173 | if len(match.groups()) == 4: |
| 174 | result.checks = int(match.group(3)) |
| 175 | result.lines = int(match.group(4)) |
| 176 | else: |
| 177 | result.lines = int(match.group(3)) |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 178 | elif re_ok.match(message): |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 179 | result.ok = True |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 180 | elif re_bad.match(message): |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 181 | result.ok = False |
Simon Glass | 7a54365 | 2020-05-06 16:29:09 -0600 | [diff] [blame] | 182 | else: |
Simon Glass | d84e84a | 2022-01-29 14:14:06 -0700 | [diff] [blame] | 183 | problem = check_patch_parse_one_message(message) |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 184 | if problem: |
| 185 | result.problems.append(problem) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 186 | |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 187 | return result |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 188 | |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 189 | |
Douglas Anderson | be4f271 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 190 | def check_patch(fname, verbose=False, show_types=False, use_tree=False): |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 191 | """Run checkpatch.pl on a file and parse the results. |
| 192 | |
| 193 | Args: |
| 194 | fname: Filename to check |
| 195 | verbose: True to print out every line of the checkpatch output as it is |
| 196 | parsed |
| 197 | show_types: Tell checkpatch to show the type (number) of each message |
Douglas Anderson | be4f271 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 198 | use_tree (bool): If False we'll pass '--no-tree' to checkpatch. |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 199 | |
| 200 | Returns: |
| 201 | namedtuple containing: |
| 202 | ok: False=failure, True=ok |
| 203 | problems: List of problems, each a dict: |
| 204 | 'type'; error or warning |
| 205 | 'msg': text message |
| 206 | 'file' : filename |
| 207 | 'line': line number |
| 208 | errors: Number of errors |
| 209 | warnings: Number of warnings |
| 210 | checks: Number of checks |
| 211 | lines: Number of lines |
| 212 | stdout: Full output of checkpatch |
| 213 | """ |
Simon Glass | d84e84a | 2022-01-29 14:14:06 -0700 | [diff] [blame] | 214 | chk = find_check_patch() |
Maxim Cournoyer | 6a407e1 | 2023-01-13 08:50:49 -0500 | [diff] [blame] | 215 | args = [chk] |
Douglas Anderson | be4f271 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 216 | if not use_tree: |
| 217 | args.append('--no-tree') |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 218 | if show_types: |
| 219 | args.append('--show-types') |
Simon Glass | 840be73 | 2022-01-29 14:14:05 -0700 | [diff] [blame] | 220 | output = command.output(*args, fname, raise_on_error=False) |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 221 | |
Simon Glass | d84e84a | 2022-01-29 14:14:06 -0700 | [diff] [blame] | 222 | return check_patch_parse(output, verbose) |
Evan Benn | ec7378b | 2021-04-01 13:49:30 +1100 | [diff] [blame] | 223 | |
| 224 | |
Simon Glass | d84e84a | 2022-01-29 14:14:06 -0700 | [diff] [blame] | 225 | def get_warning_msg(col, msg_type, fname, line, msg): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 226 | '''Create a message for a given file/line |
| 227 | |
| 228 | Args: |
| 229 | msg_type: Message type ('error' or 'warning') |
| 230 | fname: Filename which reports the problem |
| 231 | line: Line number where it was noticed |
| 232 | msg: Message to report |
| 233 | ''' |
| 234 | if msg_type == 'warning': |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 235 | msg_type = col.build(col.YELLOW, msg_type) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 236 | elif msg_type == 'error': |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 237 | msg_type = col.build(col.RED, msg_type) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 238 | elif msg_type == 'check': |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 239 | msg_type = col.build(col.MAGENTA, msg_type) |
Simon Glass | d2d136c | 2020-05-06 16:29:08 -0600 | [diff] [blame] | 240 | line_str = '' if line is None else '%d' % line |
| 241 | return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 242 | |
Douglas Anderson | be4f271 | 2022-07-19 14:56:27 -0700 | [diff] [blame] | 243 | def check_patches(verbose, args, use_tree): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 244 | '''Run the checkpatch.pl script on each patch''' |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 245 | error_count, warning_count, check_count = 0, 0, 0 |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 246 | col = terminal.Color() |
| 247 | |
Simon Glass | dc19cee | 2023-03-08 10:52:55 -0800 | [diff] [blame] | 248 | with concurrent.futures.ThreadPoolExecutor(max_workers=16) as executor: |
| 249 | futures = [] |
| 250 | for fname in args: |
| 251 | f = executor.submit(check_patch, fname, verbose, use_tree=use_tree) |
| 252 | futures.append(f) |
| 253 | |
| 254 | for fname, f in zip(args, futures): |
| 255 | result = f.result() |
| 256 | if not result.ok: |
| 257 | error_count += result.errors |
| 258 | warning_count += result.warnings |
| 259 | check_count += result.checks |
| 260 | print('%d errors, %d warnings, %d checks for %s:' % (result.errors, |
| 261 | result.warnings, result.checks, col.build(col.BLUE, fname))) |
| 262 | if (len(result.problems) != result.errors + result.warnings + |
| 263 | result.checks): |
| 264 | print("Internal error: some problems lost") |
| 265 | # Python seems to get confused by this |
| 266 | # pylint: disable=E1133 |
| 267 | for item in result.problems: |
| 268 | sys.stderr.write( |
| 269 | get_warning_msg(col, item.get('type', '<unknown>'), |
| 270 | item.get('file', '<unknown>'), |
| 271 | item.get('line', 0), item.get('msg', 'message'))) |
| 272 | print |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 273 | if error_count or warning_count or check_count: |
| 274 | 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] | 275 | color = col.GREEN |
| 276 | if warning_count: |
| 277 | color = col.YELLOW |
| 278 | if error_count: |
| 279 | color = col.RED |
Simon Glass | f45d374 | 2022-01-29 14:14:17 -0700 | [diff] [blame] | 280 | print(col.build(color, str % (error_count, warning_count, check_count))) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 281 | return False |
| 282 | return True |