utils: checkstyle.py: Turn check() into a class method for all checkers
The check() method of StyleChecker subclasses are instance methods, while CommitChecker subclasses use class methods. This makes unified handling of checkers more complicated. Turn the StyleChecker check() method into a class method, passing it the contents to be checked directly. While at it, fix two style issues reported by checkstyle.py. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
parent
5f7bcd93fd
commit
b488a862df
1 changed files with 16 additions and 32 deletions
|
@ -598,15 +598,12 @@ class HexValueChecker(StyleChecker):
|
||||||
|
|
||||||
regex = re.compile(r'\b0[xX][0-9a-fA-F]+\b')
|
regex = re.compile(r'\b0[xX][0-9a-fA-F]+\b')
|
||||||
|
|
||||||
def __init__(self, content):
|
@classmethod
|
||||||
super().__init__()
|
def check(cls, content, line_numbers):
|
||||||
self.__content = content
|
|
||||||
|
|
||||||
def check(self, line_numbers):
|
|
||||||
issues = []
|
issues = []
|
||||||
|
|
||||||
for line_number in line_numbers:
|
for line_number in line_numbers:
|
||||||
line = self.__content[line_number - 1]
|
line = content[line_number - 1]
|
||||||
match = HexValueChecker.regex.search(line)
|
match = HexValueChecker.regex.search(line)
|
||||||
if not match:
|
if not match:
|
||||||
continue
|
continue
|
||||||
|
@ -630,15 +627,12 @@ class IncludeChecker(StyleChecker):
|
||||||
'cwchar', 'cwctype', 'math.h')
|
'cwchar', 'cwctype', 'math.h')
|
||||||
include_regex = re.compile(r'^#include <([a-z.]*)>')
|
include_regex = re.compile(r'^#include <([a-z.]*)>')
|
||||||
|
|
||||||
def __init__(self, content):
|
@classmethod
|
||||||
super().__init__()
|
def check(self, content, line_numbers):
|
||||||
self.__content = content
|
|
||||||
|
|
||||||
def check(self, line_numbers):
|
|
||||||
issues = []
|
issues = []
|
||||||
|
|
||||||
for line_number in line_numbers:
|
for line_number in line_numbers:
|
||||||
line = self.__content[line_number - 1]
|
line = content[line_number - 1]
|
||||||
match = IncludeChecker.include_regex.match(line)
|
match = IncludeChecker.include_regex.match(line)
|
||||||
if not match:
|
if not match:
|
||||||
continue
|
continue
|
||||||
|
@ -664,14 +658,11 @@ class LogCategoryChecker(StyleChecker):
|
||||||
log_regex = re.compile(r'\bLOG\((Debug|Info|Warning|Error|Fatal)\)')
|
log_regex = re.compile(r'\bLOG\((Debug|Info|Warning|Error|Fatal)\)')
|
||||||
patterns = ('*.cpp',)
|
patterns = ('*.cpp',)
|
||||||
|
|
||||||
def __init__(self, content):
|
@classmethod
|
||||||
super().__init__()
|
def check(cls, content, line_numbers):
|
||||||
self.__content = content
|
|
||||||
|
|
||||||
def check(self, line_numbers):
|
|
||||||
issues = []
|
issues = []
|
||||||
for line_number in line_numbers:
|
for line_number in line_numbers:
|
||||||
line = self.__content[line_number-1]
|
line = content[line_number - 1]
|
||||||
match = LogCategoryChecker.log_regex.search(line)
|
match = LogCategoryChecker.log_regex.search(line)
|
||||||
if not match:
|
if not match:
|
||||||
continue
|
continue
|
||||||
|
@ -685,14 +676,11 @@ class LogCategoryChecker(StyleChecker):
|
||||||
class MesonChecker(StyleChecker):
|
class MesonChecker(StyleChecker):
|
||||||
patterns = ('meson.build',)
|
patterns = ('meson.build',)
|
||||||
|
|
||||||
def __init__(self, content):
|
@classmethod
|
||||||
super().__init__()
|
def check(cls, content, line_numbers):
|
||||||
self.__content = content
|
|
||||||
|
|
||||||
def check(self, line_numbers):
|
|
||||||
issues = []
|
issues = []
|
||||||
for line_number in line_numbers:
|
for line_number in line_numbers:
|
||||||
line = self.__content[line_number-1]
|
line = content[line_number - 1]
|
||||||
pos = line.find('\t')
|
pos = line.find('\t')
|
||||||
if pos != -1:
|
if pos != -1:
|
||||||
issues.append(StyleIssue(line_number, [pos, pos], line,
|
issues.append(StyleIssue(line_number, [pos, pos], line,
|
||||||
|
@ -704,13 +692,10 @@ class ShellChecker(StyleChecker):
|
||||||
patterns = ('*.sh',)
|
patterns = ('*.sh',)
|
||||||
results_line_regex = re.compile(r'In - line ([0-9]+):')
|
results_line_regex = re.compile(r'In - line ([0-9]+):')
|
||||||
|
|
||||||
def __init__(self, content):
|
@classmethod
|
||||||
super().__init__()
|
def check(cls, content, line_numbers):
|
||||||
self.__content = content
|
|
||||||
|
|
||||||
def check(self, line_numbers):
|
|
||||||
issues = []
|
issues = []
|
||||||
data = ''.join(self.__content).encode('utf-8')
|
data = ''.join(content).encode('utf-8')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ret = subprocess.run(['shellcheck', '-Cnever', '-'],
|
ret = subprocess.run(['shellcheck', '-Cnever', '-'],
|
||||||
|
@ -934,9 +919,8 @@ def check_file(top_level, commit, filename, checkers):
|
||||||
# Check for code issues not related to formatting.
|
# Check for code issues not related to formatting.
|
||||||
issues = []
|
issues = []
|
||||||
for checker in StyleChecker.instances(filename, checkers):
|
for checker in StyleChecker.instances(filename, checkers):
|
||||||
checker = checker(after)
|
|
||||||
for hunk in commit_diff:
|
for hunk in commit_diff:
|
||||||
issues += checker.check(hunk.side('to').touched)
|
issues += checker.check(after, hunk.side('to').touched)
|
||||||
|
|
||||||
# Print the detected issues.
|
# Print the detected issues.
|
||||||
if len(issues) == 0 and len(formatted_diff) == 0:
|
if len(issues) == 0 and len(formatted_diff) == 0:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue