blob: 34a3bd22b08d274b665d2fcf4c47fd17629a92dc [file] [log] [blame]
Simon Glass26132882012-01-14 15:12:45 +00001# Copyright (c) 2011 The Chromium OS Authors.
2#
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02003# SPDX-License-Identifier: GPL-2.0+
Simon Glass26132882012-01-14 15:12:45 +00004#
5
Simon Glass0495abf2013-03-26 13:09:39 +00006import collections
Simon Glass26132882012-01-14 15:12:45 +00007import command
8import gitutil
9import os
10import re
Vadim Bendeburyc549f082013-01-09 16:00:10 +000011import sys
Simon Glass26132882012-01-14 15:12:45 +000012import terminal
13
14def FindCheckPatch():
Doug Anderson4baede02012-11-26 15:23:23 +000015 top_level = gitutil.GetTopLevel()
Simon Glass26132882012-01-14 15:12:45 +000016 try_list = [
17 os.getcwd(),
18 os.path.join(os.getcwd(), '..', '..'),
Doug Anderson4baede02012-11-26 15:23:23 +000019 os.path.join(top_level, 'tools'),
20 os.path.join(top_level, 'scripts'),
Simon Glass26132882012-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 Bendeburyc549f082013-01-09 16:00:10 +000036
Masahiro Yamada880828d2014-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 Glass26132882012-01-14 15:12:45 +000039
40def CheckPatch(fname, verbose=False):
41 """Run checkpatch.pl on a file.
42
43 Returns:
Simon Glass0495abf2013-03-26 13:09:39 +000044 namedtuple containing:
45 ok: False=failure, True=ok
Simon Glass26132882012-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 Glass0495abf2013-03-26 13:09:39 +000051 errors: Number of errors
52 warnings: Number of warnings
53 checks: Number of checks
Simon Glass26132882012-01-14 15:12:45 +000054 lines: Number of lines
Simon Glass0495abf2013-03-26 13:09:39 +000055 stdout: Full output of checkpatch
Simon Glass26132882012-01-14 15:12:45 +000056 """
Simon Glass0495abf2013-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 Glass26132882012-01-14 15:12:45 +000064 chk = FindCheckPatch()
Simon Glass26132882012-01-14 15:12:45 +000065 item = {}
Simon Glass0495abf2013-03-26 13:09:39 +000066 result.stdout = command.Output(chk, '--no-tree', fname)
Simon Glass26132882012-01-14 15:12:45 +000067 #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
68 #stdout, stderr = pipe.communicate()
69
70 # total: 0 errors, 0 warnings, 159 lines checked
Simon Glass0495abf2013-03-26 13:09:39 +000071 # or:
72 # total: 0 errors, 2 warnings, 7 checks, 473 lines checked
Simon Glass26132882012-01-14 15:12:45 +000073 re_stats = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)')
Simon Glass0495abf2013-03-26 13:09:39 +000074 re_stats_full = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)'
75 ' checks, (\d+)')
Simon Glass26132882012-01-14 15:12:45 +000076 re_ok = re.compile('.*has no obvious style problems')
77 re_bad = re.compile('.*has style problems, please review')
78 re_error = re.compile('ERROR: (.*)')
79 re_warning = re.compile('WARNING: (.*)')
Simon Glass0495abf2013-03-26 13:09:39 +000080 re_check = re.compile('CHECK: (.*)')
Simon Glass26132882012-01-14 15:12:45 +000081 re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):')
82
Simon Glass0495abf2013-03-26 13:09:39 +000083 for line in result.stdout.splitlines():
Simon Glass26132882012-01-14 15:12:45 +000084 if verbose:
85 print line
86
87 # A blank line indicates the end of a message
88 if not line and item:
Simon Glass0495abf2013-03-26 13:09:39 +000089 result.problems.append(item)
Simon Glass26132882012-01-14 15:12:45 +000090 item = {}
Simon Glass0495abf2013-03-26 13:09:39 +000091 match = re_stats_full.match(line)
92 if not match:
93 match = re_stats.match(line)
Simon Glass26132882012-01-14 15:12:45 +000094 if match:
Simon Glass0495abf2013-03-26 13:09:39 +000095 result.errors = int(match.group(1))
96 result.warnings = int(match.group(2))
97 if len(match.groups()) == 4:
98 result.checks = int(match.group(3))
99 result.lines = int(match.group(4))
100 else:
101 result.lines = int(match.group(3))
Simon Glass26132882012-01-14 15:12:45 +0000102 elif re_ok.match(line):
Simon Glass0495abf2013-03-26 13:09:39 +0000103 result.ok = True
Simon Glass26132882012-01-14 15:12:45 +0000104 elif re_bad.match(line):
Simon Glass0495abf2013-03-26 13:09:39 +0000105 result.ok = False
106 err_match = re_error.match(line)
107 warn_match = re_warning.match(line)
108 file_match = re_file.match(line)
109 check_match = re_check.match(line)
110 if err_match:
111 item['msg'] = err_match.group(1)
Simon Glass26132882012-01-14 15:12:45 +0000112 item['type'] = 'error'
Simon Glass0495abf2013-03-26 13:09:39 +0000113 elif warn_match:
114 item['msg'] = warn_match.group(1)
Simon Glass26132882012-01-14 15:12:45 +0000115 item['type'] = 'warning'
Simon Glass0495abf2013-03-26 13:09:39 +0000116 elif check_match:
117 item['msg'] = check_match.group(1)
118 item['type'] = 'check'
119 elif file_match:
120 item['file'] = file_match.group(1)
121 item['line'] = int(file_match.group(2))
Simon Glass26132882012-01-14 15:12:45 +0000122
Simon Glass0495abf2013-03-26 13:09:39 +0000123 return result
Simon Glass26132882012-01-14 15:12:45 +0000124
125def GetWarningMsg(col, msg_type, fname, line, msg):
126 '''Create a message for a given file/line
127
128 Args:
129 msg_type: Message type ('error' or 'warning')
130 fname: Filename which reports the problem
131 line: Line number where it was noticed
132 msg: Message to report
133 '''
134 if msg_type == 'warning':
135 msg_type = col.Color(col.YELLOW, msg_type)
136 elif msg_type == 'error':
137 msg_type = col.Color(col.RED, msg_type)
Simon Glass0495abf2013-03-26 13:09:39 +0000138 elif msg_type == 'check':
139 msg_type = col.Color(col.MAGENTA, msg_type)
Simon Glass26132882012-01-14 15:12:45 +0000140 return '%s: %s,%d: %s' % (msg_type, fname, line, msg)
141
142def CheckPatches(verbose, args):
143 '''Run the checkpatch.pl script on each patch'''
Simon Glass0495abf2013-03-26 13:09:39 +0000144 error_count, warning_count, check_count = 0, 0, 0
Simon Glass26132882012-01-14 15:12:45 +0000145 col = terminal.Color()
146
147 for fname in args:
Simon Glass0495abf2013-03-26 13:09:39 +0000148 result = CheckPatch(fname, verbose)
149 if not result.ok:
150 error_count += result.errors
151 warning_count += result.warnings
152 check_count += result.checks
153 print '%d errors, %d warnings, %d checks for %s:' % (result.errors,
154 result.warnings, result.checks, col.Color(col.BLUE, fname))
155 if (len(result.problems) != result.errors + result.warnings +
156 result.checks):
Simon Glass26132882012-01-14 15:12:45 +0000157 print "Internal error: some problems lost"
Simon Glass0495abf2013-03-26 13:09:39 +0000158 for item in result.problems:
159 print GetWarningMsg(col, item.get('type', '<unknown>'),
Simon Glasseb1526c2012-09-27 15:33:46 +0000160 item.get('file', '<unknown>'),
Simon Glass0495abf2013-03-26 13:09:39 +0000161 item.get('line', 0), item.get('msg', 'message'))
162 print
Simon Glass26132882012-01-14 15:12:45 +0000163 #print stdout
Simon Glass0495abf2013-03-26 13:09:39 +0000164 if error_count or warning_count or check_count:
165 str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
Simon Glass26132882012-01-14 15:12:45 +0000166 color = col.GREEN
167 if warning_count:
168 color = col.YELLOW
169 if error_count:
170 color = col.RED
Simon Glass0495abf2013-03-26 13:09:39 +0000171 print col.Color(color, str % (error_count, warning_count, check_count))
Simon Glass26132882012-01-14 15:12:45 +0000172 return False
173 return True