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 | |
| 41 | def CheckPatch(fname, verbose=False): |
| 42 | """Run checkpatch.pl on a file. |
| 43 | |
| 44 | Returns: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 45 | namedtuple containing: |
| 46 | ok: False=failure, True=ok |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 47 | problems: List of problems, each a dict: |
| 48 | 'type'; error or warning |
| 49 | 'msg': text message |
| 50 | 'file' : filename |
| 51 | 'line': line number |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 52 | errors: Number of errors |
| 53 | warnings: Number of warnings |
| 54 | checks: Number of checks |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 55 | lines: Number of lines |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 56 | stdout: Full output of checkpatch |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 57 | """ |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 58 | fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines', |
| 59 | 'stdout'] |
| 60 | result = collections.namedtuple('CheckPatchResult', fields) |
| 61 | result.ok = False |
Simon Glass | 39d6d84 | 2020-05-06 16:29:04 -0600 | [diff] [blame] | 62 | result.errors, result.warnings, result.checks = 0, 0, 0 |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 63 | result.lines = 0 |
| 64 | result.problems = [] |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 65 | chk = FindCheckPatch() |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 66 | item = {} |
Simon Glass | c55e056 | 2016-07-25 18:59:00 -0600 | [diff] [blame] | 67 | result.stdout = command.Output(chk, '--no-tree', fname, |
| 68 | raise_on_error=False) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 69 | #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 70 | #stdout, stderr = pipe.communicate() |
| 71 | |
| 72 | # total: 0 errors, 0 warnings, 159 lines checked |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 73 | # or: |
| 74 | # total: 0 errors, 2 warnings, 7 checks, 473 lines checked |
Simon Glass | 396168c | 2020-05-06 16:29:05 -0600 | [diff] [blame] | 75 | emacs_prefix = '(?:[0-9]{4}.*\.patch:[0-9]+: )?' |
| 76 | emacs_stats = '(?:[0-9]{4}.*\.patch )?' |
| 77 | re_stats = re.compile(emacs_stats + |
| 78 | 'total: (\\d+) errors, (\d+) warnings, (\d+)') |
| 79 | re_stats_full = re.compile(emacs_stats + |
| 80 | 'total: (\\d+) errors, (\d+) warnings, (\d+)' |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 81 | ' checks, (\d+)') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 82 | re_ok = re.compile('.*has no obvious style problems') |
| 83 | re_bad = re.compile('.*has style problems, please review') |
| 84 | re_error = re.compile('ERROR: (.*)') |
Simon Glass | 396168c | 2020-05-06 16:29:05 -0600 | [diff] [blame] | 85 | re_warning = re.compile(emacs_prefix + 'WARNING:(?:[A-Z_]+:)? (.*)') |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 86 | re_check = re.compile('CHECK: (.*)') |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 87 | re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):') |
Simon Glass | 9a690c4 | 2020-05-06 16:29:07 -0600 | [diff] [blame] | 88 | re_note = re.compile('NOTE: (.*)') |
| 89 | indent = ' ' * 6 |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 90 | for line in result.stdout.splitlines(): |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 91 | if verbose: |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 92 | print(line) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 93 | |
| 94 | # A blank line indicates the end of a message |
Simon Glass | 9eb74a9 | 2020-05-06 16:29:06 -0600 | [diff] [blame] | 95 | if not line: |
| 96 | if item: |
| 97 | result.problems.append(item) |
| 98 | item = {} |
| 99 | continue |
Simon Glass | 9a690c4 | 2020-05-06 16:29:07 -0600 | [diff] [blame] | 100 | if re_note.match(line): |
| 101 | continue |
| 102 | # Skip lines which quote code |
| 103 | if line.startswith(indent): |
| 104 | continue |
| 105 | # Skip code quotes and #<n> |
| 106 | if line.startswith('+') or line.startswith('#'): |
| 107 | continue |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 108 | match = re_stats_full.match(line) |
| 109 | if not match: |
| 110 | match = re_stats.match(line) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 111 | if match: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 112 | result.errors = int(match.group(1)) |
| 113 | result.warnings = int(match.group(2)) |
| 114 | if len(match.groups()) == 4: |
| 115 | result.checks = int(match.group(3)) |
| 116 | result.lines = int(match.group(4)) |
| 117 | else: |
| 118 | result.lines = int(match.group(3)) |
Simon Glass | 9eb74a9 | 2020-05-06 16:29:06 -0600 | [diff] [blame] | 119 | continue |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 120 | elif re_ok.match(line): |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 121 | result.ok = True |
Simon Glass | 9eb74a9 | 2020-05-06 16:29:06 -0600 | [diff] [blame] | 122 | continue |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 123 | elif re_bad.match(line): |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 124 | result.ok = False |
Simon Glass | 9eb74a9 | 2020-05-06 16:29:06 -0600 | [diff] [blame] | 125 | continue |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 126 | err_match = re_error.match(line) |
| 127 | warn_match = re_warning.match(line) |
| 128 | file_match = re_file.match(line) |
| 129 | check_match = re_check.match(line) |
Simon Glass | d2d136c | 2020-05-06 16:29:08 -0600 | [diff] [blame^] | 130 | subject_match = line.startswith('Subject:') |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 131 | if err_match: |
| 132 | item['msg'] = err_match.group(1) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 133 | item['type'] = 'error' |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 134 | elif warn_match: |
| 135 | item['msg'] = warn_match.group(1) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 136 | item['type'] = 'warning' |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 137 | elif check_match: |
| 138 | item['msg'] = check_match.group(1) |
| 139 | item['type'] = 'check' |
| 140 | elif file_match: |
| 141 | item['file'] = file_match.group(1) |
| 142 | item['line'] = int(file_match.group(2)) |
Simon Glass | d2d136c | 2020-05-06 16:29:08 -0600 | [diff] [blame^] | 143 | elif subject_match: |
| 144 | item['file'] = '<patch subject>' |
| 145 | item['line'] = None |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 146 | |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 147 | return result |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 148 | |
| 149 | def GetWarningMsg(col, msg_type, fname, line, msg): |
| 150 | '''Create a message for a given file/line |
| 151 | |
| 152 | Args: |
| 153 | msg_type: Message type ('error' or 'warning') |
| 154 | fname: Filename which reports the problem |
| 155 | line: Line number where it was noticed |
| 156 | msg: Message to report |
| 157 | ''' |
| 158 | if msg_type == 'warning': |
| 159 | msg_type = col.Color(col.YELLOW, msg_type) |
| 160 | elif msg_type == 'error': |
| 161 | msg_type = col.Color(col.RED, msg_type) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 162 | elif msg_type == 'check': |
| 163 | msg_type = col.Color(col.MAGENTA, msg_type) |
Simon Glass | d2d136c | 2020-05-06 16:29:08 -0600 | [diff] [blame^] | 164 | line_str = '' if line is None else '%d' % line |
| 165 | return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 166 | |
| 167 | def CheckPatches(verbose, args): |
| 168 | '''Run the checkpatch.pl script on each patch''' |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 169 | error_count, warning_count, check_count = 0, 0, 0 |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 170 | col = terminal.Color() |
| 171 | |
| 172 | for fname in args: |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 173 | result = CheckPatch(fname, verbose) |
| 174 | if not result.ok: |
| 175 | error_count += result.errors |
| 176 | warning_count += result.warnings |
| 177 | check_count += result.checks |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 178 | print('%d errors, %d warnings, %d checks for %s:' % (result.errors, |
| 179 | result.warnings, result.checks, col.Color(col.BLUE, fname))) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 180 | if (len(result.problems) != result.errors + result.warnings + |
| 181 | result.checks): |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 182 | print("Internal error: some problems lost") |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 183 | for item in result.problems: |
Simon Glass | c004a5f | 2017-01-17 16:52:23 -0700 | [diff] [blame] | 184 | sys.stderr.write( |
| 185 | GetWarningMsg(col, item.get('type', '<unknown>'), |
Simon Glass | eb1526c | 2012-09-27 15:33:46 +0000 | [diff] [blame] | 186 | item.get('file', '<unknown>'), |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 187 | item.get('line', 0), item.get('msg', 'message'))) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 188 | print |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 189 | #print(stdout) |
Simon Glass | 0495abf | 2013-03-26 13:09:39 +0000 | [diff] [blame] | 190 | if error_count or warning_count or check_count: |
| 191 | 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] | 192 | color = col.GREEN |
| 193 | if warning_count: |
| 194 | color = col.YELLOW |
| 195 | if error_count: |
| 196 | color = col.RED |
Paul Burton | c393134 | 2016-09-27 16:03:50 +0100 | [diff] [blame] | 197 | print(col.Color(color, str % (error_count, warning_count, check_count))) |
Simon Glass | 2613288 | 2012-01-14 15:12:45 +0000 | [diff] [blame] | 198 | return False |
| 199 | return True |