blob: 2915d97950d32c3920ab6e93523c2f031a4d1065 [file] [log] [blame]
Simon Glass0d24de92012-01-14 15:12:45 +00001# Copyright (c) 2011 The Chromium OS Authors.
2#
Wolfgang Denk1a459662013-07-08 09:37:19 +02003# SPDX-License-Identifier: GPL-2.0+
Simon Glass0d24de92012-01-14 15:12:45 +00004#
5
Simon Glassd29fe6e2013-03-26 13:09:39 +00006import collections
Simon Glass0d24de92012-01-14 15:12:45 +00007import command
8import gitutil
9import os
10import re
Vadim Bendebury99adf6e2013-01-09 16:00:10 +000011import sys
Simon Glass0d24de92012-01-14 15:12:45 +000012import terminal
13
14def FindCheckPatch():
Doug Andersond96ef372012-11-26 15:23:23 +000015 top_level = gitutil.GetTopLevel()
Simon Glass0d24de92012-01-14 15:12:45 +000016 try_list = [
17 os.getcwd(),
18 os.path.join(os.getcwd(), '..', '..'),
Doug Andersond96ef372012-11-26 15:23:23 +000019 os.path.join(top_level, 'tools'),
20 os.path.join(top_level, 'scripts'),
Simon Glass0d24de92012-01-14 15:12:45 +000021 '%s/bin' % os.getenv('HOME'),
22 ]
23 # Look in current dir
24 for path in try_list:
25 fname = os.path.join(path, 'checkpatch.pl')
26 if os.path.isfile(fname):
27 return fname
28
29 # Look upwwards for a Chrome OS tree
30 while not os.path.ismount(path):
31 fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files',
32 'scripts', 'checkpatch.pl')
33 if os.path.isfile(fname):
34 return fname
35 path = os.path.dirname(path)
Vadim Bendebury99adf6e2013-01-09 16:00:10 +000036
Masahiro Yamada31e21412014-08-16 00:59:26 +090037 sys.exit('Cannot find checkpatch.pl - please put it in your ' +
38 '~/bin directory or use --no-check')
Simon Glass0d24de92012-01-14 15:12:45 +000039
40def CheckPatch(fname, verbose=False):
41 """Run checkpatch.pl on a file.
42
43 Returns:
Simon Glassd29fe6e2013-03-26 13:09:39 +000044 namedtuple containing:
45 ok: False=failure, True=ok
Simon Glass0d24de92012-01-14 15:12:45 +000046 problems: List of problems, each a dict:
47 'type'; error or warning
48 'msg': text message
49 'file' : filename
50 'line': line number
Simon Glassd29fe6e2013-03-26 13:09:39 +000051 errors: Number of errors
52 warnings: Number of warnings
53 checks: Number of checks
Simon Glass0d24de92012-01-14 15:12:45 +000054 lines: Number of lines
Simon Glassd29fe6e2013-03-26 13:09:39 +000055 stdout: Full output of checkpatch
Simon Glass0d24de92012-01-14 15:12:45 +000056 """
Simon Glassd29fe6e2013-03-26 13:09:39 +000057 fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines',
58 'stdout']
59 result = collections.namedtuple('CheckPatchResult', fields)
60 result.ok = False
61 result.errors, result.warning, result.checks = 0, 0, 0
62 result.lines = 0
63 result.problems = []
Simon Glass0d24de92012-01-14 15:12:45 +000064 chk = FindCheckPatch()
Simon Glass0d24de92012-01-14 15:12:45 +000065 item = {}
Simon Glass785f1542016-07-25 18:59:00 -060066 result.stdout = command.Output(chk, '--no-tree', fname,
67 raise_on_error=False)
Simon Glass0d24de92012-01-14 15:12:45 +000068 #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
69 #stdout, stderr = pipe.communicate()
70
71 # total: 0 errors, 0 warnings, 159 lines checked
Simon Glassd29fe6e2013-03-26 13:09:39 +000072 # or:
73 # total: 0 errors, 2 warnings, 7 checks, 473 lines checked
Simon Glass0d24de92012-01-14 15:12:45 +000074 re_stats = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)')
Simon Glassd29fe6e2013-03-26 13:09:39 +000075 re_stats_full = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)'
76 ' checks, (\d+)')
Simon Glass0d24de92012-01-14 15:12:45 +000077 re_ok = re.compile('.*has no obvious style problems')
78 re_bad = re.compile('.*has style problems, please review')
79 re_error = re.compile('ERROR: (.*)')
80 re_warning = re.compile('WARNING: (.*)')
Simon Glassd29fe6e2013-03-26 13:09:39 +000081 re_check = re.compile('CHECK: (.*)')
Simon Glass0d24de92012-01-14 15:12:45 +000082 re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):')
83
Simon Glassd29fe6e2013-03-26 13:09:39 +000084 for line in result.stdout.splitlines():
Simon Glass0d24de92012-01-14 15:12:45 +000085 if verbose:
Paul Burtona920a172016-09-27 16:03:50 +010086 print(line)
Simon Glass0d24de92012-01-14 15:12:45 +000087
88 # A blank line indicates the end of a message
89 if not line and item:
Simon Glassd29fe6e2013-03-26 13:09:39 +000090 result.problems.append(item)
Simon Glass0d24de92012-01-14 15:12:45 +000091 item = {}
Simon Glassd29fe6e2013-03-26 13:09:39 +000092 match = re_stats_full.match(line)
93 if not match:
94 match = re_stats.match(line)
Simon Glass0d24de92012-01-14 15:12:45 +000095 if match:
Simon Glassd29fe6e2013-03-26 13:09:39 +000096 result.errors = int(match.group(1))
97 result.warnings = int(match.group(2))
98 if len(match.groups()) == 4:
99 result.checks = int(match.group(3))
100 result.lines = int(match.group(4))
101 else:
102 result.lines = int(match.group(3))
Simon Glass0d24de92012-01-14 15:12:45 +0000103 elif re_ok.match(line):
Simon Glassd29fe6e2013-03-26 13:09:39 +0000104 result.ok = True
Simon Glass0d24de92012-01-14 15:12:45 +0000105 elif re_bad.match(line):
Simon Glassd29fe6e2013-03-26 13:09:39 +0000106 result.ok = False
107 err_match = re_error.match(line)
108 warn_match = re_warning.match(line)
109 file_match = re_file.match(line)
110 check_match = re_check.match(line)
111 if err_match:
112 item['msg'] = err_match.group(1)
Simon Glass0d24de92012-01-14 15:12:45 +0000113 item['type'] = 'error'
Simon Glassd29fe6e2013-03-26 13:09:39 +0000114 elif warn_match:
115 item['msg'] = warn_match.group(1)
Simon Glass0d24de92012-01-14 15:12:45 +0000116 item['type'] = 'warning'
Simon Glassd29fe6e2013-03-26 13:09:39 +0000117 elif check_match:
118 item['msg'] = check_match.group(1)
119 item['type'] = 'check'
120 elif file_match:
121 item['file'] = file_match.group(1)
122 item['line'] = int(file_match.group(2))
Simon Glass0d24de92012-01-14 15:12:45 +0000123
Simon Glassd29fe6e2013-03-26 13:09:39 +0000124 return result
Simon Glass0d24de92012-01-14 15:12:45 +0000125
126def GetWarningMsg(col, msg_type, fname, line, msg):
127 '''Create a message for a given file/line
128
129 Args:
130 msg_type: Message type ('error' or 'warning')
131 fname: Filename which reports the problem
132 line: Line number where it was noticed
133 msg: Message to report
134 '''
135 if msg_type == 'warning':
136 msg_type = col.Color(col.YELLOW, msg_type)
137 elif msg_type == 'error':
138 msg_type = col.Color(col.RED, msg_type)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000139 elif msg_type == 'check':
140 msg_type = col.Color(col.MAGENTA, msg_type)
Simon Glass8aa41362017-01-17 16:52:23 -0700141 return '%s:%d: %s: %s\n' % (fname, line, msg_type, msg)
Simon Glass0d24de92012-01-14 15:12:45 +0000142
143def CheckPatches(verbose, args):
144 '''Run the checkpatch.pl script on each patch'''
Simon Glassd29fe6e2013-03-26 13:09:39 +0000145 error_count, warning_count, check_count = 0, 0, 0
Simon Glass0d24de92012-01-14 15:12:45 +0000146 col = terminal.Color()
147
148 for fname in args:
Simon Glassd29fe6e2013-03-26 13:09:39 +0000149 result = CheckPatch(fname, verbose)
150 if not result.ok:
151 error_count += result.errors
152 warning_count += result.warnings
153 check_count += result.checks
Paul Burtona920a172016-09-27 16:03:50 +0100154 print('%d errors, %d warnings, %d checks for %s:' % (result.errors,
155 result.warnings, result.checks, col.Color(col.BLUE, fname)))
Simon Glassd29fe6e2013-03-26 13:09:39 +0000156 if (len(result.problems) != result.errors + result.warnings +
157 result.checks):
Paul Burtona920a172016-09-27 16:03:50 +0100158 print("Internal error: some problems lost")
Simon Glassd29fe6e2013-03-26 13:09:39 +0000159 for item in result.problems:
Simon Glass8aa41362017-01-17 16:52:23 -0700160 sys.stderr.write(
161 GetWarningMsg(col, item.get('type', '<unknown>'),
Simon Glassafb9bf52012-09-27 15:33:46 +0000162 item.get('file', '<unknown>'),
Paul Burtona920a172016-09-27 16:03:50 +0100163 item.get('line', 0), item.get('msg', 'message')))
Simon Glassd29fe6e2013-03-26 13:09:39 +0000164 print
Paul Burtona920a172016-09-27 16:03:50 +0100165 #print(stdout)
Simon Glassd29fe6e2013-03-26 13:09:39 +0000166 if error_count or warning_count or check_count:
167 str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
Simon Glass0d24de92012-01-14 15:12:45 +0000168 color = col.GREEN
169 if warning_count:
170 color = col.YELLOW
171 if error_count:
172 color = col.RED
Paul Burtona920a172016-09-27 16:03:50 +0100173 print(col.Color(color, str % (error_count, warning_count, check_count)))
Simon Glass0d24de92012-01-14 15:12:45 +0000174 return False
175 return True