Skip to content

Commit

Permalink
checkpatch: introduce a flexible framework
Browse files Browse the repository at this point in the history
Developers wishing to add checks to checkpatch sift through an adhoc mess,
currently.  The process goes something like:
1. Figure out what to test in the patch
2. Write some code, quickly, that checks for that condition
3. Look through the statemachine to find where the check should go
4. ignore parts of the above and just throw something together

That worked fine for the initial development, but as interesting new tests
are developed, it is important to have a more flexible framework that lets
a developer just plug in a new test, easily.

This commit brings in a new framework that allows plugging in checks very
quickly.  Hook up the line-length test as an initial demonstration.

Signed-off-by: Aaron Conole <[email protected]>
Signed-off-by: Ben Pfaff <[email protected]>
  • Loading branch information
apconole authored and blp committed May 1, 2017
1 parent d0c961a commit 517f04a
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions utilities/checkpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,47 @@ def pointer_whitespace_check(line):
return __regex_ptr_declaration_missing_whitespace.search(line) is not None


def line_length_check(line):
"""Return TRUE if the line length is too long"""
if len(line) > 79:
return True
return False


checks = [
{'regex': None,
'match_name':
lambda x: not any([fmt in x for fmt in line_length_blacklist]),
'check': lambda x: line_length_check(x),
'print':
lambda x: print_warning("Line is greater than 79-characters long", x)}
]


def get_file_type_checks(filename):
"""Returns the list of checks for a file based on matching the filename
against regex."""
global checks
checkList = []
for check in checks:
if check['regex'] is None and check['match_name'] is None:
checkList.append(check)
if check['regex'] is not None and \
re.compile(check['regex']).search(filename) is not None:
checkList.append(check)
elif check['match_name'] is not None and check['match_name'](filename):
checkList.append(check)
return checkList


def run_checks(current_file, line, lineno):
"""Runs the various checks for the particular line. This will take
filename into account."""
for check in get_file_type_checks(current_file):
if check['check'](line):
check['print'](lineno)


def ovs_checkpatch_parse(text):
global print_file_name
lineno = 0
Expand All @@ -180,15 +221,10 @@ def ovs_checkpatch_parse(text):
re.I | re.M | re.S)
is_co_author = re.compile(r'(\s*(Co-authored-by: )(.*))$',
re.I | re.M | re.S)
skip_line_length_check = False

for line in text.decode().split('\n'):
if current_file != previous_file:
previous_file = current_file
if any([fmt in current_file for fmt in line_length_blacklist]):
skip_line_length_check = True
else:
skip_line_length_check = False

lineno = lineno + 1
if len(line) <= 0:
Expand Down Expand Up @@ -250,13 +286,10 @@ def ovs_checkpatch_parse(text):
print_line = True
print_warning("Line has non-spaces leading whitespace",
lineno)
run_checks(current_file, cmp_line, lineno)
if trailing_whitespace_or_crlf(cmp_line):
print_line = True
print_warning("Line has trailing whitespace", lineno)
if len(cmp_line) > 79 and not skip_line_length_check:
print_line = True
print_warning("Line is greater than 79-characters long",
lineno)
if not if_and_for_whitespace_checks(cmp_line):
print_line = True
print_error("Improper whitespace around control block",
Expand Down

0 comments on commit 517f04a

Please sign in to comment.