2018-05-06 21:58:06 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
2012-01-14 15:12:45 +00:00
|
|
|
# Copyright (c) 2011 The Chromium OS Authors.
|
|
|
|
#
|
|
|
|
|
2013-03-26 13:09:39 +00:00
|
|
|
import collections
|
2012-01-14 15:12:45 +00:00
|
|
|
import os
|
|
|
|
import re
|
2013-01-09 16:00:10 +00:00
|
|
|
import sys
|
2020-04-18 00:09:04 +00:00
|
|
|
|
|
|
|
from patman import command
|
|
|
|
from patman import gitutil
|
|
|
|
from patman import terminal
|
2021-04-01 02:49:30 +00:00
|
|
|
|
|
|
|
EMACS_PREFIX = r'(?:[0-9]{4}.*\.patch:[0-9]+: )?'
|
|
|
|
TYPE_NAME = r'([A-Z_]+:)?'
|
|
|
|
RE_ERROR = re.compile(r'ERROR:%s (.*)' % TYPE_NAME)
|
|
|
|
RE_WARNING = re.compile(EMACS_PREFIX + r'WARNING:%s (.*)' % TYPE_NAME)
|
|
|
|
RE_CHECK = re.compile(r'CHECK:%s (.*)' % TYPE_NAME)
|
|
|
|
RE_FILE = re.compile(r'#(\d+): (FILE: ([^:]*):(\d+):)?')
|
|
|
|
RE_NOTE = re.compile(r'NOTE: (.*)')
|
|
|
|
|
2012-01-14 15:12:45 +00:00
|
|
|
|
2022-01-29 21:14:06 +00:00
|
|
|
def find_check_patch():
|
2022-01-29 21:14:11 +00:00
|
|
|
top_level = gitutil.get_top_level()
|
2012-01-14 15:12:45 +00:00
|
|
|
try_list = [
|
|
|
|
os.getcwd(),
|
|
|
|
os.path.join(os.getcwd(), '..', '..'),
|
2012-11-26 15:23:23 +00:00
|
|
|
os.path.join(top_level, 'tools'),
|
|
|
|
os.path.join(top_level, 'scripts'),
|
2012-01-14 15:12:45 +00:00
|
|
|
'%s/bin' % os.getenv('HOME'),
|
|
|
|
]
|
|
|
|
# Look in current dir
|
|
|
|
for path in try_list:
|
|
|
|
fname = os.path.join(path, 'checkpatch.pl')
|
|
|
|
if os.path.isfile(fname):
|
|
|
|
return fname
|
|
|
|
|
|
|
|
# Look upwwards for a Chrome OS tree
|
|
|
|
while not os.path.ismount(path):
|
|
|
|
fname = os.path.join(path, 'src', 'third_party', 'kernel', 'files',
|
|
|
|
'scripts', 'checkpatch.pl')
|
|
|
|
if os.path.isfile(fname):
|
|
|
|
return fname
|
|
|
|
path = os.path.dirname(path)
|
2013-01-09 16:00:10 +00:00
|
|
|
|
2014-08-15 15:59:26 +00:00
|
|
|
sys.exit('Cannot find checkpatch.pl - please put it in your ' +
|
|
|
|
'~/bin directory or use --no-check')
|
2012-01-14 15:12:45 +00:00
|
|
|
|
2021-04-01 02:49:30 +00:00
|
|
|
|
2022-01-29 21:14:06 +00:00
|
|
|
def check_patch_parse_one_message(message):
|
2021-04-01 02:49:30 +00:00
|
|
|
"""Parse one checkpatch message
|
2012-01-14 15:12:45 +00:00
|
|
|
|
2020-07-06 03:41:49 +00:00
|
|
|
Args:
|
2021-04-01 02:49:30 +00:00
|
|
|
message: string to parse
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict:
|
|
|
|
'type'; error or warning
|
|
|
|
'msg': text message
|
|
|
|
'file' : filename
|
|
|
|
'line': line number
|
|
|
|
"""
|
|
|
|
|
|
|
|
if RE_NOTE.match(message):
|
|
|
|
return {}
|
|
|
|
|
|
|
|
item = {}
|
|
|
|
|
|
|
|
err_match = RE_ERROR.match(message)
|
|
|
|
warn_match = RE_WARNING.match(message)
|
|
|
|
check_match = RE_CHECK.match(message)
|
|
|
|
if err_match:
|
|
|
|
item['cptype'] = err_match.group(1)
|
|
|
|
item['msg'] = err_match.group(2)
|
|
|
|
item['type'] = 'error'
|
|
|
|
elif warn_match:
|
|
|
|
item['cptype'] = warn_match.group(1)
|
|
|
|
item['msg'] = warn_match.group(2)
|
|
|
|
item['type'] = 'warning'
|
|
|
|
elif check_match:
|
|
|
|
item['cptype'] = check_match.group(1)
|
|
|
|
item['msg'] = check_match.group(2)
|
|
|
|
item['type'] = 'check'
|
|
|
|
else:
|
|
|
|
message_indent = ' '
|
|
|
|
print('patman: failed to parse checkpatch message:\n%s' %
|
|
|
|
(message_indent + message.replace('\n', '\n' + message_indent)),
|
|
|
|
file=sys.stderr)
|
|
|
|
return {}
|
|
|
|
|
|
|
|
file_match = RE_FILE.search(message)
|
|
|
|
# some messages have no file, catch those here
|
|
|
|
no_file_match = any(s in message for s in [
|
|
|
|
'\nSubject:', 'Missing Signed-off-by: line(s)',
|
|
|
|
'does MAINTAINERS need updating'
|
|
|
|
])
|
|
|
|
|
|
|
|
if file_match:
|
|
|
|
err_fname = file_match.group(3)
|
|
|
|
if err_fname:
|
|
|
|
item['file'] = err_fname
|
|
|
|
item['line'] = int(file_match.group(4))
|
|
|
|
else:
|
|
|
|
item['file'] = '<patch>'
|
|
|
|
item['line'] = int(file_match.group(1))
|
|
|
|
elif no_file_match:
|
|
|
|
item['file'] = '<patch>'
|
|
|
|
else:
|
|
|
|
message_indent = ' '
|
|
|
|
print('patman: failed to find file / line information:\n%s' %
|
|
|
|
(message_indent + message.replace('\n', '\n' + message_indent)),
|
|
|
|
file=sys.stderr)
|
|
|
|
|
|
|
|
return item
|
|
|
|
|
|
|
|
|
2022-01-29 21:14:06 +00:00
|
|
|
def check_patch_parse(checkpatch_output, verbose=False):
|
2021-04-01 02:49:30 +00:00
|
|
|
"""Parse checkpatch.pl output
|
|
|
|
|
|
|
|
Args:
|
|
|
|
checkpatch_output: string to parse
|
2020-07-06 03:41:49 +00:00
|
|
|
verbose: True to print out every line of the checkpatch output as it is
|
|
|
|
parsed
|
|
|
|
|
2012-01-14 15:12:45 +00:00
|
|
|
Returns:
|
2013-03-26 13:09:39 +00:00
|
|
|
namedtuple containing:
|
|
|
|
ok: False=failure, True=ok
|
2022-02-11 20:23:18 +00:00
|
|
|
problems (list of problems): each a dict:
|
2012-01-14 15:12:45 +00:00
|
|
|
'type'; error or warning
|
|
|
|
'msg': text message
|
|
|
|
'file' : filename
|
|
|
|
'line': line number
|
2013-03-26 13:09:39 +00:00
|
|
|
errors: Number of errors
|
|
|
|
warnings: Number of warnings
|
|
|
|
checks: Number of checks
|
2012-01-14 15:12:45 +00:00
|
|
|
lines: Number of lines
|
2021-04-01 02:49:30 +00:00
|
|
|
stdout: checkpatch_output
|
2012-01-14 15:12:45 +00:00
|
|
|
"""
|
2013-03-26 13:09:39 +00:00
|
|
|
fields = ['ok', 'problems', 'errors', 'warnings', 'checks', 'lines',
|
|
|
|
'stdout']
|
|
|
|
result = collections.namedtuple('CheckPatchResult', fields)
|
2021-04-01 02:49:30 +00:00
|
|
|
result.stdout = checkpatch_output
|
2013-03-26 13:09:39 +00:00
|
|
|
result.ok = False
|
2020-05-06 22:29:04 +00:00
|
|
|
result.errors, result.warnings, result.checks = 0, 0, 0
|
2013-03-26 13:09:39 +00:00
|
|
|
result.lines = 0
|
|
|
|
result.problems = []
|
2012-01-14 15:12:45 +00:00
|
|
|
|
|
|
|
# total: 0 errors, 0 warnings, 159 lines checked
|
2013-03-26 13:09:39 +00:00
|
|
|
# or:
|
|
|
|
# total: 0 errors, 2 warnings, 7 checks, 473 lines checked
|
2021-04-01 02:49:30 +00:00
|
|
|
emacs_stats = r'(?:[0-9]{4}.*\.patch )?'
|
2020-05-06 22:29:05 +00:00
|
|
|
re_stats = re.compile(emacs_stats +
|
2021-04-01 02:49:30 +00:00
|
|
|
r'total: (\d+) errors, (\d+) warnings, (\d+)')
|
2020-05-06 22:29:05 +00:00
|
|
|
re_stats_full = re.compile(emacs_stats +
|
2021-04-01 02:49:30 +00:00
|
|
|
r'total: (\d+) errors, (\d+) warnings, (\d+)'
|
|
|
|
r' checks, (\d+)')
|
|
|
|
re_ok = re.compile(r'.*has no obvious style problems')
|
|
|
|
re_bad = re.compile(r'.*has style problems, please review')
|
|
|
|
|
|
|
|
# A blank line indicates the end of a message
|
|
|
|
for message in result.stdout.split('\n\n'):
|
2012-01-14 15:12:45 +00:00
|
|
|
if verbose:
|
2021-04-01 02:49:30 +00:00
|
|
|
print(message)
|
|
|
|
|
|
|
|
# either find stats, the verdict, or delegate
|
|
|
|
match = re_stats_full.match(message)
|
2013-03-26 13:09:39 +00:00
|
|
|
if not match:
|
2021-04-01 02:49:30 +00:00
|
|
|
match = re_stats.match(message)
|
2012-01-14 15:12:45 +00:00
|
|
|
if match:
|
2013-03-26 13:09:39 +00:00
|
|
|
result.errors = int(match.group(1))
|
|
|
|
result.warnings = int(match.group(2))
|
|
|
|
if len(match.groups()) == 4:
|
|
|
|
result.checks = int(match.group(3))
|
|
|
|
result.lines = int(match.group(4))
|
|
|
|
else:
|
|
|
|
result.lines = int(match.group(3))
|
2021-04-01 02:49:30 +00:00
|
|
|
elif re_ok.match(message):
|
2013-03-26 13:09:39 +00:00
|
|
|
result.ok = True
|
2021-04-01 02:49:30 +00:00
|
|
|
elif re_bad.match(message):
|
2013-03-26 13:09:39 +00:00
|
|
|
result.ok = False
|
2020-05-06 22:29:09 +00:00
|
|
|
else:
|
2022-01-29 21:14:06 +00:00
|
|
|
problem = check_patch_parse_one_message(message)
|
2021-04-01 02:49:30 +00:00
|
|
|
if problem:
|
|
|
|
result.problems.append(problem)
|
2012-01-14 15:12:45 +00:00
|
|
|
|
2013-03-26 13:09:39 +00:00
|
|
|
return result
|
2012-01-14 15:12:45 +00:00
|
|
|
|
2021-04-01 02:49:30 +00:00
|
|
|
|
2022-01-29 21:14:06 +00:00
|
|
|
def check_patch(fname, verbose=False, show_types=False):
|
2021-04-01 02:49:30 +00:00
|
|
|
"""Run checkpatch.pl on a file and parse the results.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
fname: Filename to check
|
|
|
|
verbose: True to print out every line of the checkpatch output as it is
|
|
|
|
parsed
|
|
|
|
show_types: Tell checkpatch to show the type (number) of each message
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
namedtuple containing:
|
|
|
|
ok: False=failure, True=ok
|
|
|
|
problems: List of problems, each a dict:
|
|
|
|
'type'; error or warning
|
|
|
|
'msg': text message
|
|
|
|
'file' : filename
|
|
|
|
'line': line number
|
|
|
|
errors: Number of errors
|
|
|
|
warnings: Number of warnings
|
|
|
|
checks: Number of checks
|
|
|
|
lines: Number of lines
|
|
|
|
stdout: Full output of checkpatch
|
|
|
|
"""
|
2022-01-29 21:14:06 +00:00
|
|
|
chk = find_check_patch()
|
2021-04-01 02:49:30 +00:00
|
|
|
args = [chk, '--no-tree']
|
|
|
|
if show_types:
|
|
|
|
args.append('--show-types')
|
2022-01-29 21:14:05 +00:00
|
|
|
output = command.output(*args, fname, raise_on_error=False)
|
2021-04-01 02:49:30 +00:00
|
|
|
|
2022-01-29 21:14:06 +00:00
|
|
|
return check_patch_parse(output, verbose)
|
2021-04-01 02:49:30 +00:00
|
|
|
|
|
|
|
|
2022-01-29 21:14:06 +00:00
|
|
|
def get_warning_msg(col, msg_type, fname, line, msg):
|
2012-01-14 15:12:45 +00:00
|
|
|
'''Create a message for a given file/line
|
|
|
|
|
|
|
|
Args:
|
|
|
|
msg_type: Message type ('error' or 'warning')
|
|
|
|
fname: Filename which reports the problem
|
|
|
|
line: Line number where it was noticed
|
|
|
|
msg: Message to report
|
|
|
|
'''
|
|
|
|
if msg_type == 'warning':
|
2022-01-29 21:14:17 +00:00
|
|
|
msg_type = col.build(col.YELLOW, msg_type)
|
2012-01-14 15:12:45 +00:00
|
|
|
elif msg_type == 'error':
|
2022-01-29 21:14:17 +00:00
|
|
|
msg_type = col.build(col.RED, msg_type)
|
2013-03-26 13:09:39 +00:00
|
|
|
elif msg_type == 'check':
|
2022-01-29 21:14:17 +00:00
|
|
|
msg_type = col.build(col.MAGENTA, msg_type)
|
2020-05-06 22:29:08 +00:00
|
|
|
line_str = '' if line is None else '%d' % line
|
|
|
|
return '%s:%s: %s: %s\n' % (fname, line_str, msg_type, msg)
|
2012-01-14 15:12:45 +00:00
|
|
|
|
2022-01-29 21:14:06 +00:00
|
|
|
def check_patches(verbose, args):
|
2012-01-14 15:12:45 +00:00
|
|
|
'''Run the checkpatch.pl script on each patch'''
|
2013-03-26 13:09:39 +00:00
|
|
|
error_count, warning_count, check_count = 0, 0, 0
|
2012-01-14 15:12:45 +00:00
|
|
|
col = terminal.Color()
|
|
|
|
|
|
|
|
for fname in args:
|
2022-01-29 21:14:06 +00:00
|
|
|
result = check_patch(fname, verbose)
|
2013-03-26 13:09:39 +00:00
|
|
|
if not result.ok:
|
|
|
|
error_count += result.errors
|
|
|
|
warning_count += result.warnings
|
|
|
|
check_count += result.checks
|
2016-09-27 15:03:50 +00:00
|
|
|
print('%d errors, %d warnings, %d checks for %s:' % (result.errors,
|
2022-01-29 21:14:17 +00:00
|
|
|
result.warnings, result.checks, col.build(col.BLUE, fname)))
|
2013-03-26 13:09:39 +00:00
|
|
|
if (len(result.problems) != result.errors + result.warnings +
|
|
|
|
result.checks):
|
2016-09-27 15:03:50 +00:00
|
|
|
print("Internal error: some problems lost")
|
2022-02-11 20:23:18 +00:00
|
|
|
# Python seems to get confused by this
|
|
|
|
# pylint: disable=E1133
|
2013-03-26 13:09:39 +00:00
|
|
|
for item in result.problems:
|
2017-01-17 23:52:23 +00:00
|
|
|
sys.stderr.write(
|
2022-01-29 21:14:06 +00:00
|
|
|
get_warning_msg(col, item.get('type', '<unknown>'),
|
2012-09-27 15:33:46 +00:00
|
|
|
item.get('file', '<unknown>'),
|
2016-09-27 15:03:50 +00:00
|
|
|
item.get('line', 0), item.get('msg', 'message')))
|
2013-03-26 13:09:39 +00:00
|
|
|
print
|
2016-09-27 15:03:50 +00:00
|
|
|
#print(stdout)
|
2013-03-26 13:09:39 +00:00
|
|
|
if error_count or warning_count or check_count:
|
|
|
|
str = 'checkpatch.pl found %d error(s), %d warning(s), %d checks(s)'
|
2012-01-14 15:12:45 +00:00
|
|
|
color = col.GREEN
|
|
|
|
if warning_count:
|
|
|
|
color = col.YELLOW
|
|
|
|
if error_count:
|
|
|
|
color = col.RED
|
2022-01-29 21:14:17 +00:00
|
|
|
print(col.build(color, str % (error_count, warning_count, check_count)))
|
2012-01-14 15:12:45 +00:00
|
|
|
return False
|
|
|
|
return True
|