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 |
| 13 | from patman import tools |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 14 | |
| 15 | def FindCheckPatch(): |
Doug Anderson | 4baede0 | 2012-11-26 15:23:23 +0000 | [diff] [blame] | 16 | top_level = gitutil.GetTopLevel() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 17 | try_list = [ |
| 18 | os.getcwd(), |
| 19 | os.path.join(os.getcwd(), '..', '..'), |
Doug Anderson | 4baede0 | 2012-11-26 15:23:23 +0000 | [diff] [blame] | 20 | os.path.join(top_level, 'tools'), |
| 21 | os.path.join(top_level, 'scripts'), |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 22 | '%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 Bendebury | c549f08 | 2013-01-09 16:00:10 +0000 | [diff] [blame] | 37 | |
Masahiro Yamada | 880828d | 2014-08-16 00:59:26 +0900 | [diff] [blame] | 38 | sys.exit('Cannot find checkpatch.pl - please put it in your ' + |
| 39 | '~/bin directory or use --no-check') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 40 | |
Simon Glass | 08cfa41 | 2020-06-14 10:54:06 -0600 | [diff] [blame] | 41 | def CheckPatch(fname, verbose=False, show_types=False): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 42 | """Run checkpatch.pl on a file. |
| 43 | |
Simon Glass | 24725af | 2020-07-05 21:41:49 -0600 | [diff] [blame] | 44 | 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 Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 50 | Returns: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 51 | namedtuple containing: |
| 52 | ok: False=failure, True=ok |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 53 | problems: List of problems, each a dict: |
| 54 | 'type'; error or warning |
| 55 | 'msg': text message |
| 56 | 'file' : filename |
| 57 | 'line': line number |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 58 | errors: Number of errors |
| 59 | warnings: Number of warnings |
| 60 | checks: Number of checks |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 61 | lines: Number of lines |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 62 | stdout: Full output of checkpatch |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 63 | """ |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 64 | fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines', |
| 65 | 'stdout'] |
| 66 | result = collections.namedtuple('CheckPatchResult', fields) |
| 67 | result.ok = False |
Simon Glass | 39d6d84 | 2020-05-06 16:29:04 -0600 | [diff] [blame] | 68 | result.errors, result.warnings, result.checks = 0, 0, 0 |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 69 | result.lines = 0 |
| 70 | result.problems = [] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 71 | chk = FindCheckPatch() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 72 | item = {} |
Simon Glass | 08cfa41 | 2020-06-14 10:54:06 -0600 | [diff] [blame] | 73 | 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 Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 77 | #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 78 | #stdout, stderr = pipe.communicate() |
| 79 | |
| 80 | # total: 0 errors, 0 warnings, 159 lines checked |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 81 | # or: |
| 82 | # total: 0 errors, 2 warnings, 7 checks, 473 lines checked |
Simon Glass | 396168c | 2020-05-06 16:29:05 -0600 | [diff] [blame] | 83 | 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 Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 89 | ' checks, (\d+)') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 90 | re_ok = re.compile('.*has no obvious style problems') |
| 91 | re_bad = re.compile('.*has style problems, please review') |
Simon Glass | 08cfa41 | 2020-06-14 10:54:06 -0600 | [diff] [blame] | 92 | 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 Glass | 8cf2364 | 2020-10-29 21:46:33 -0600 | [diff] [blame] | 96 | re_file = re.compile('#(\d+): (FILE: ([^:]*):(\d+):)?') |
Simon Glass | 9a690c4 | 2020-05-06 16:29:07 -0600 | [diff] [blame] | 97 | re_note = re.compile('NOTE: (.*)') |
Simon Glass | 5c906fb | 2020-10-29 21:46:31 -0600 | [diff] [blame] | 98 | re_new_file = re.compile('new file mode .*') |
Simon Glass | 9a690c4 | 2020-05-06 16:29:07 -0600 | [diff] [blame] | 99 | indent = ' ' * 6 |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 100 | for line in result.stdout.splitlines(): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 101 | if verbose: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 102 | print(line) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 103 | |
| 104 | # A blank line indicates the end of a message |
Simon Glass | 9eb74a9 | 2020-05-06 16:29:06 -0600 | [diff] [blame] | 105 | if not line: |
| 106 | if item: |
| 107 | result.problems.append(item) |
| 108 | item = {} |
| 109 | continue |
Simon Glass | 9a690c4 | 2020-05-06 16:29:07 -0600 | [diff] [blame] | 110 | if re_note.match(line): |
| 111 | continue |
| 112 | # Skip lines which quote code |
| 113 | if line.startswith(indent): |
| 114 | continue |
Simon Glass | 5c906fb | 2020-10-29 21:46:31 -0600 | [diff] [blame] | 115 | # Skip code quotes |
| 116 | if line.startswith('+'): |
| 117 | continue |
| 118 | if re_new_file.match(line): |
Simon Glass | 9a690c4 | 2020-05-06 16:29:07 -0600 | [diff] [blame] | 119 | continue |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 120 | match = re_stats_full.match(line) |
| 121 | if not match: |
| 122 | match = re_stats.match(line) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 123 | if match: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 124 | 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 Glass | 9eb74a9 | 2020-05-06 16:29:06 -0600 | [diff] [blame] | 131 | continue |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 132 | elif re_ok.match(line): |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 133 | result.ok = True |
Simon Glass | 9eb74a9 | 2020-05-06 16:29:06 -0600 | [diff] [blame] | 134 | continue |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 135 | elif re_bad.match(line): |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 136 | result.ok = False |
Simon Glass | 9eb74a9 | 2020-05-06 16:29:06 -0600 | [diff] [blame] | 137 | continue |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 138 | 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 Glass | d2d136c | 2020-05-06 16:29:08 -0600 | [diff] [blame] | 142 | subject_match = line.startswith('Subject:') |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 143 | if err_match: |
Simon Glass | 08cfa41 | 2020-06-14 10:54:06 -0600 | [diff] [blame] | 144 | item['cptype'] = err_match.group(1) |
| 145 | item['msg'] = err_match.group(2) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 146 | item['type'] = 'error' |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 147 | elif warn_match: |
Simon Glass | 08cfa41 | 2020-06-14 10:54:06 -0600 | [diff] [blame] | 148 | item['cptype'] = warn_match.group(1) |
| 149 | item['msg'] = warn_match.group(2) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 150 | item['type'] = 'warning' |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 151 | elif check_match: |
Simon Glass | 08cfa41 | 2020-06-14 10:54:06 -0600 | [diff] [blame] | 152 | item['cptype'] = check_match.group(1) |
| 153 | item['msg'] = check_match.group(2) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 154 | item['type'] = 'check' |
| 155 | elif file_match: |
Simon Glass | 8cf2364 | 2020-10-29 21:46:33 -0600 | [diff] [blame] | 156 | 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 Glass | d2d136c | 2020-05-06 16:29:08 -0600 | [diff] [blame] | 163 | elif subject_match: |
| 164 | item['file'] = '<patch subject>' |
| 165 | item['line'] = None |
Simon Glass | 7a54365 | 2020-05-06 16:29:09 -0600 | [diff] [blame] | 166 | else: |
| 167 | print('bad line "%s", %d' % (line, len(line))) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 168 | |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 169 | return result |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 170 | |
| 171 | def 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 Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 184 | elif msg_type == 'check': |
| 185 | msg_type = col.Color(col.MAGENTA, msg_type) |
Simon Glass | d2d136c | 2020-05-06 16:29:08 -0600 | [diff] [blame] | 186 | line_str = '' if line is None else '%d' % line |
| 187 | return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 188 | |
| 189 | def CheckPatches(verbose, args): |
| 190 | '''Run the checkpatch.pl script on each patch''' |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 191 | error_count, warning_count, check_count = 0, 0, 0 |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 192 | col = terminal.Color() |
| 193 | |
| 194 | for fname in args: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 195 | 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 Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 200 | print('%d errors, %d warnings, %d checks for %s:' % (result.errors, |
| 201 | result.warnings, result.checks, col.Color(col.BLUE, fname))) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 202 | if (len(result.problems) != result.errors + result.warnings + |
| 203 | result.checks): |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 204 | print("Internal error: some problems lost") |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 205 | for item in result.problems: |
Simon Glass | c004a5f | 2017-01-17 16:52:23 -0700 | [diff] [blame] | 206 | sys.stderr.write( |
| 207 | GetWarningMsg(col, item.get('type', '<unknown>'), |
Simon Glass | eb1526c | 2012-09-27 15:33:46 +0000 | [diff] [blame] | 208 | item.get('file', '<unknown>'), |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 209 | item.get('line', 0), item.get('msg', 'message'))) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 210 | print |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 211 | #print(stdout) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 212 | if error_count or warning_count or check_count: |
| 213 | 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] | 214 | color = col.GREEN |
| 215 | if warning_count: |
| 216 | color = col.YELLOW |
| 217 | if error_count: |
| 218 | color = col.RED |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 219 | print(col.Color(color, str % (error_count, warning_count, check_count))) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 220 | return False |
| 221 | return True |