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 | 2613288 | 2012-01-14 15:12:45 +0000 | [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: (.*)') |
| 98 | indent = ' ' * 6 |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 99 | for line in result.stdout.splitlines(): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 100 | if verbose: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 101 | print(line) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 102 | |
| 103 | # A blank line indicates the end of a message |
Simon Glass | 9eb74a9 | 2020-05-06 16:29:06 -0600 | [diff] [blame] | 104 | if not line: |
| 105 | if item: |
| 106 | result.problems.append(item) |
| 107 | item = {} |
| 108 | continue |
Simon Glass | 9a690c4 | 2020-05-06 16:29:07 -0600 | [diff] [blame] | 109 | 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 Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 117 | match = re_stats_full.match(line) |
| 118 | if not match: |
| 119 | match = re_stats.match(line) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 120 | if match: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 121 | 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 Glass | 9eb74a9 | 2020-05-06 16:29:06 -0600 | [diff] [blame] | 128 | continue |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 129 | elif re_ok.match(line): |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 130 | result.ok = True |
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_bad.match(line): |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 133 | result.ok = False |
Simon Glass | 9eb74a9 | 2020-05-06 16:29:06 -0600 | [diff] [blame] | 134 | continue |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 135 | 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 Glass | d2d136c | 2020-05-06 16:29:08 -0600 | [diff] [blame] | 139 | subject_match = line.startswith('Subject:') |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 140 | if err_match: |
Simon Glass | 08cfa41 | 2020-06-14 10:54:06 -0600 | [diff] [blame] | 141 | item['cptype'] = err_match.group(1) |
| 142 | item['msg'] = err_match.group(2) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 143 | item['type'] = 'error' |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 144 | elif warn_match: |
Simon Glass | 08cfa41 | 2020-06-14 10:54:06 -0600 | [diff] [blame] | 145 | item['cptype'] = warn_match.group(1) |
| 146 | item['msg'] = warn_match.group(2) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 147 | item['type'] = 'warning' |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 148 | elif check_match: |
Simon Glass | 08cfa41 | 2020-06-14 10:54:06 -0600 | [diff] [blame] | 149 | item['cptype'] = check_match.group(1) |
| 150 | item['msg'] = check_match.group(2) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 151 | item['type'] = 'check' |
| 152 | elif file_match: |
| 153 | item['file'] = file_match.group(1) |
| 154 | item['line'] = int(file_match.group(2)) |
Simon Glass | d2d136c | 2020-05-06 16:29:08 -0600 | [diff] [blame] | 155 | elif subject_match: |
| 156 | item['file'] = '<patch subject>' |
| 157 | item['line'] = None |
Simon Glass | 7a54365 | 2020-05-06 16:29:09 -0600 | [diff] [blame] | 158 | else: |
| 159 | print('bad line "%s", %d' % (line, len(line))) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 160 | |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 161 | return result |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 162 | |
| 163 | def 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 Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 176 | elif msg_type == 'check': |
| 177 | msg_type = col.Color(col.MAGENTA, msg_type) |
Simon Glass | d2d136c | 2020-05-06 16:29:08 -0600 | [diff] [blame] | 178 | line_str = '' if line is None else '%d' % line |
| 179 | return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 180 | |
| 181 | def CheckPatches(verbose, args): |
| 182 | '''Run the checkpatch.pl script on each patch''' |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 183 | error_count, warning_count, check_count = 0, 0, 0 |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 184 | col = terminal.Color() |
| 185 | |
| 186 | for fname in args: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 187 | 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 Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 192 | print('%d errors, %d warnings, %d checks for %s:' % (result.errors, |
| 193 | result.warnings, result.checks, col.Color(col.BLUE, fname))) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 194 | if (len(result.problems) != result.errors + result.warnings + |
| 195 | result.checks): |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 196 | print("Internal error: some problems lost") |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 197 | for item in result.problems: |
Simon Glass | c004a5f | 2017-01-17 16:52:23 -0700 | [diff] [blame] | 198 | sys.stderr.write( |
| 199 | GetWarningMsg(col, item.get('type', '<unknown>'), |
Simon Glass | eb1526c | 2012-09-27 15:33:46 +0000 | [diff] [blame] | 200 | item.get('file', '<unknown>'), |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 201 | item.get('line', 0), item.get('msg', 'message'))) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 202 | print |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 203 | #print(stdout) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 204 | if error_count or warning_count or check_count: |
| 205 | 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] | 206 | color = col.GREEN |
| 207 | if warning_count: |
| 208 | color = col.YELLOW |
| 209 | if error_count: |
| 210 | color = col.RED |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 211 | print(col.Color(color, str % (error_count, warning_count, check_count))) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 212 | return False |
| 213 | return True |