From patchwork Tue Feb 12 22:25:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 559 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 76C0D610B3 for ; Tue, 12 Feb 2019 23:26:04 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (dfj612yhrgyx302h3jwwy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:ce28:277f:58d7:3ca4]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 074412CF for ; Tue, 12 Feb 2019 23:26:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1550010364; bh=s5RcWrbP6c/JBJcuZKr46kbXrpc7Ng+YEfBig0CGDPo=; h=From:To:Subject:Date:In-Reply-To:References:From; b=cvkxfDA4H4bSe/9cAkjplUN1VFQm5bJLhqXdTuhKTieU1/lpZJKFTLJV4M46GFkjF PGExiPeerhh5oykiZCca64KE8Rt3ZzxvNyAOEpK1ImOx+KTJM1lYtJ+GcnVSk34m6B UtA91XebWuykEWbi9rVDltIB3EupWz3eNI3Gfn7s= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Wed, 13 Feb 2019 00:25:57 +0200 Message-Id: <20190212222557.8898-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190212222557.8898-1-laurent.pinchart@ideasonboard.com> References: <20190212222557.8898-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 2/2] utils: checkstyle: Catch LOG() usage without an explicit category X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Feb 2019 22:26:04 -0000 Add support for checkers not related to code formatting to the checkstyle.py script, and create a first checker that catches usage of the LOG() macro without an explicit category. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- utils/checkstyle.py | 55 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index f3005d1fbb52..9abd2687b1f9 100755 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -146,7 +146,7 @@ class DiffHunk(object): s += Colours.reset() s += '\n' - return s + return s[:-1] def append(self, line): if line[0] == ' ': @@ -229,6 +229,29 @@ available_formatters = { # Style checking # +class LogCategoryChecker(object): + log_regex = re.compile('\\bLOG\((Debug|Info|Warning|Error|Fatal)\)') + + def __init__(self, content): + self.__content = content + + def check(self, line_numbers): + issues = [] + for line_number in line_numbers: + line = self.__content[line_number-1] + if not LogCategoryChecker.log_regex.search(line): + continue + + issues.append([line_number, line, 'LOG() should use categories']) + + return issues + + +available_checkers = { + 'log_category': LogCategoryChecker, +} + + def check_file(top_level, commit, filename, formatters): # Extract the line numbers touched by the commit. diff = subprocess.run(['git', 'diff', '%s~..%s' % (commit, commit), '--', @@ -260,20 +283,36 @@ def check_file(top_level, commit, filename, formatters): formatted = formatted.splitlines(True) diff = difflib.unified_diff(after, formatted) - # Split the diff in hunks, recording line number ranges for each hunk. + # Split the diff in hunks, recording line number ranges for each hunk, and + # filter out hunks that are not touched by the commit. formatted_diff = parse_diff(diff) - - # Filter out hunks that are not touched by the commit. formatted_diff = [hunk for hunk in formatted_diff if hunk.intersects(lines)] - if len(formatted_diff) == 0: + + # Check for code issues not related to formatting. + issues = [] + for checker in available_checkers: + checker = available_checkers[checker](after) + for hunk in commit_diff: + issues += checker.check(hunk.side('to').touched) + + # Print the detected issues. + if len(issues) == 0 and len(formatted_diff) == 0: return 0 print('%s---' % Colours.fg(Colours.Red), filename) print('%s+++' % Colours.fg(Colours.Green), filename) - for hunk in formatted_diff: - print(hunk) - return len(formatted_diff) + if len(formatted_diff): + for hunk in formatted_diff: + print(hunk) + + if len(issues): + issues.sort() + for issue in issues: + print('%s#%u: %s' % (Colours.fg(Colours.Yellow), issue[0], issue[2])) + print('+%s%s' % (issue[1].rstrip(), Colours.reset())) + + return len(formatted_diff) + len(issues) def check_style(top_level, commit, formatters):