blob: 795b5193145e2d58b5682f25adc775f03d0dab83 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001# SPDX-License-Identifier: GPL-2.0+
Simon Glass26132882012-01-14 15:12:45 +00002# Copyright (c) 2011 The Chromium OS Authors.
3#
Simon Glass26132882012-01-14 15:12:45 +00004
Simon Glass0495abf2013-03-26 13:09:39 +00005import collections
Simon Glass26132882012-01-14 15:12:45 +00006import os
7import re
Vadim Bendeburyc549f082013-01-09 16:00:10 +00008import sys
Simon Glassa997ea52020-04-17 18:09:04 -06009
10from patman import command
11from patman import gitutil
12from patman import terminal
13from patman import tools
Simon Glass26132882012-01-14 15:12:45 +000014
15def FindCheckPatch():
Doug Anderson4baede02012-11-26 15:23:23 +000016 top_level = gitutil.GetTopLevel()
Simon Glass26132882012-01-14 15:12:45 +000017 try_list = [
18 os.getcwd(),
19 os.path.join(os.getcwd(), '..', '..'),
Doug Anderson4baede02012-11-26 15:23:23 +000020 os.path.join(top_level, 'tools'),
21 os.path.join(top_level, 'scripts'),
Simon Glass26132882012-01-14 15:12:45 +000022 '%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 Bendeburyc549f082013-01-09 16:00:10 +000037
Masahiro Yamada880828d2014-08-16 00:59:26 +090038 sys.exit('Cannot find checkpatch.pl - please put it in your ' +
39 '~/bin directory or use --no-check')
Simon Glass26132882012-01-14 15:12:45 +000040
41def CheckPatch(fname, verbose=False):
42 """Run checkpatch.pl on a file.
43
44 Returns:
Simon Glass0495abf2013-03-26 13:09:39 +000045 namedtuple containing:
46 ok: False=failure, True=ok
Simon Glass26132882012-01-14 15:12:45 +000047 problems: List of problems, each a dict:
48 'type'; error or warning
49 'msg': text message
50 'file' : filename
51 'line': line number
Simon Glass0495abf2013-03-26 13:09:39 +000052 errors: Number of errors
53 warnings: Number of warnings
54 checks: Number of checks
Simon Glass26132882012-01-14 15:12:45 +000055 lines: Number of lines
Simon Glass0495abf2013-03-26 13:09:39 +000056 stdout: Full output of checkpatch
Simon Glass26132882012-01-14 15:12:45 +000057 """
Simon Glass0495abf2013-03-26 13:09:39 +000058 fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines',
59 'stdout']
60 result = collections.namedtuple('CheckPatchResult', fields)
61 result.ok = False
62 result.errors, result.warning, result.checks = 0, 0, 0
63 result.lines = 0
64 result.problems = []
Simon Glass26132882012-01-14 15:12:45 +000065 chk = FindCheckPatch()
Simon Glass26132882012-01-14 15:12:45 +000066 item = {}
Simon Glassc55e0562016-07-25 18:59:00 -060067 result.stdout = command.Output(chk, '--no-tree', fname,
68 raise_on_error=False)
Simon Glass26132882012-01-14 15:12:45 +000069 #pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
70 #stdout, stderr = pipe.communicate()
71
72 # total: 0 errors, 0 warnings, 159 lines checked
Simon Glass0495abf2013-03-26 13:09:39 +000073 # or:
74 # total: 0 errors, 2 warnings, 7 checks, 473 lines checked
Simon Glass26132882012-01-14 15:12:45 +000075 re_stats = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)')
Simon Glass0495abf2013-03-26 13:09:39 +000076 re_stats_full = re.compile('total: (\\d+) errors, (\d+) warnings, (\d+)'
77 ' checks, (\d+)')
Simon Glass26132882012-01-14 15:12:45 +000078 re_ok = re.compile('.*has no obvious style problems')
79 re_bad = re.compile('.*has style problems, please review')
80 re_error = re.compile('ERROR: (.*)')
81 re_warning = re.compile('WARNING: (.*)')
Simon Glass0495abf2013-03-26 13:09:39 +000082 re_check = re.compile('CHECK: (.*)')
Simon Glass26132882012-01-14 15:12:45 +000083 re_file = re.compile('#\d+: FILE: ([^:]*):(\d+):')
84
Simon Glass0495abf2013-03-26 13:09:39 +000085 for line in result.stdout.splitlines():
Simon Glass26132882012-01-14 15:12:45 +000086 if verbose:
Paul Burtonc3931342016-09-27 16:03:50 +010087 print(line)
Simon Glass26132882012-01-14 15:12:45 +000088
89 # A blank line indicates the end of a message
90 if not line and item:
Simon Glass0495abf2013-03-26 13:09:39 +000091 result.problems.append(item)
Simon Glass26132882012-01-14 15:12:45 +000092 item = {}
Simon Glass0495abf2013-03-26 13:09:39 +000093 match = re_stats_full.match(line)
94 if not match:
95 match = re_stats.match(line)
Simon Glass26132882012-01-14 15:12:45 +000096 if match:
Simon Glass0495abf2013-03-26 13:09:39 +000097 result.errors = int(match.group(1))
98 result.warnings = int(match.group(2))
99 if len(match.groups()) == 4:
100 result.checks = int(match.group(3))
101 result.lines = int(match.group(4))
102 else:
103 result.lines = int(match.group(3))
Simon Glass26132882012-01-14 15:12:45 +0000104 elif re_ok.match(line):
Simon Glass0495abf2013-03-26 13:09:39 +0000105 result.ok = True
Simon Glass26132882012-01-14 15:12:45 +0000106 elif re_bad.match(line):
Simon Glass0495abf2013-03-26 13:09:39 +0000107 result.ok = False
108 err_match = re_error.match(line)
109 warn_match = re_warning.match(line)
110 file_match = re_file.match(line)
111 check_match = re_check.match(line)
112 if err_match:
113 item['msg'] = err_match.group(1)
Simon Glass26132882012-01-14 15:12:45 +0000114 item['type'] = 'error'
Simon Glass0495abf2013-03-26 13:09:39 +0000115 elif warn_match:
116 item['msg'] = warn_match.group(1)
Simon Glass26132882012-01-14 15:12:45 +0000117 item['type'] = 'warning'
Simon Glass0495abf2013-03-26 13:09:39 +0000118 elif check_match:
119 item['msg'] = check_match.group(1)
120 item['type'] = 'check'
121 elif file_match:
122 item['file'] = file_match.group(1)
123 item['line'] = int(file_match.group(2))
Simon Glass26132882012-01-14 15:12:45 +0000124
Simon Glass0495abf2013-03-26 13:09:39 +0000125 return result
Simon Glass26132882012-01-14 15:12:45 +0000126
127def GetWarningMsg(col, msg_type, fname, line, msg):
128 '''Create a message for a given file/line
129
130 Args:
131 msg_type: Message type ('error' or 'warning')
132 fname: Filename which reports the problem
133 line: Line number where it was noticed
134 msg: Message to report
135 '''
136 if msg_type == 'warning':
137 msg_type = col.Color(col.YELLOW, msg_type)
138 elif msg_type == 'error':
139 msg_type = col.Color(col.RED, msg_type)
Simon Glass0495abf2013-03-26 13:09:39 +0000140 elif msg_type == 'check':
141 msg_type = col.Color(col.MAGENTA, msg_type)
Simon Glassc004a5f2017-01-17 16:52:23 -0700142 return '%s:%d: %s: %s\n' % (fname, line, msg_type, msg)
Simon Glass26132882012-01-14 15:12:45 +0000143
144def CheckPatches(verbose, args):
145 '''Run the checkpatch.pl script on each patch'''
Simon Glass0495abf2013-03-26 13:09:39 +0000146 error_count, warning_count, check_count = 0, 0, 0
Simon Glass26132882012-01-14 15:12:45 +0000147 col = terminal.Color()
148
149 for fname in args:
Simon Glass0495abf2013-03-26 13:09:39 +0000150 result = CheckPatch(fname, verbose)
151 if not result.ok:
152 error_count += result.errors
153 warning_count += result.warnings
154 check_count += result.checks
Paul Burtonc3931342016-09-27 16:03:50 +0100155 print('%d errors, %d warnings, %d checks for %s:' % (result.errors,
156 result.warnings, result.checks, col.Color(col.BLUE, fname)))
Simon Glass0495abf2013-03-26 13:09:39 +0000157 if (len(result.problems) != result.errors + result.warnings +
158 result.checks):
Paul Burtonc3931342016-09-27 16:03:50 +0100159 print("Internal error: some problems lost")
Simon Glass0495abf2013-03-26 13:09:39 +0000160 for item in result.problems:
Simon Glassc004a5f2017-01-17 16:52:23 -0700161 sys.stderr.write(
162 GetWarningMsg(col, item.get('type', '<unknown>'),
Simon Glasseb1526c2012-09-27 15:33:46 +0000163 item.get('file', '<unknown>'),
Paul Burtonc3931342016-09-27 16:03:50 +0100164 item.get('line', 0), item.get('msg', 'message')))
Simon Glass0495abf2013-03-26 13:09:39 +0000165 print
Paul Burtonc3931342016-09-27 16:03:50 +0100166 #print(stdout)
Simon Glass0495abf2013-03-26 13:09:39 +0000167 if error_count or warning_count or check_count:
168 str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
Simon Glass26132882012-01-14 15:12:45 +0000169 color = col.GREEN
170 if warning_count:
171 color = col.YELLOW
172 if error_count:
173 color = col.RED
Paul Burtonc3931342016-09-27 16:03:50 +0100174 print(col.Color(color, str % (error_count, warning_count, check_count)))
Simon Glass26132882012-01-14 15:12:45 +0000175 return False
176 return True