From patchwork Fri May 31 12:18:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 20166 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 6B955BDE6B for ; Fri, 31 May 2024 12:18:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0C509634BC; Fri, 31 May 2024 14:18:57 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="LxKVPtFY"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BE99861A46 for ; Fri, 31 May 2024 14:18:53 +0200 (CEST) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id BF5A111A3 for ; Fri, 31 May 2024 14:18:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1717157929; bh=NLtLXJjZSN7auouiRWIkJcgqCnCtwD6FHbaPlDwokgs=; h=From:To:Subject:Date:In-Reply-To:References:From; b=LxKVPtFYsJ0ID2t6qF9619NxckqrvmYljuYL5LDmspVlMhR8ZJrSaM8mb+NyGSc+D YAdfIYkvT4uNkDdbcz05nb8T2ebJ34UeFiRMGubwJMxmS0WqYG8v5poZMED7egK6sH MYvsY4bdogLMFafvw/9sGkJ6y8Tjwl37/T4CnqL0= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH 1/4] utils: checkstyle.py: Refactor IncludeChecker Date: Fri, 31 May 2024 15:18:35 +0300 Message-ID: <20240531121838.27643-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240531121838.27643-1-laurent.pinchart@ideasonboard.com> References: <20240531121838.27643-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The IncludeCheck warns when code uses C++ standard library headers where corresponding C compatibility headers are preferred. We have an exception to that rule for math.h, where cmath is prefered. In order to prepare for extending checkstyle.py to enforce that rule, refactor the way the IncludeChecker identifies headers. No functional change is intended. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Reviewed-by: Kieran Bingham --- utils/checkstyle.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index 4e287b2e0053..518be0897db5 100755 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -565,11 +565,11 @@ class StyleIssue(object): class IncludeChecker(StyleChecker): patterns = ('*.cpp', '*.h') - headers = ('assert', 'ctype', 'errno', 'fenv', 'float', 'inttypes', - 'limits', 'locale', 'setjmp', 'signal', 'stdarg', 'stddef', - 'stdint', 'stdio', 'stdlib', 'string', 'time', 'uchar', 'wchar', - 'wctype') - include_regex = re.compile(r'^#include ') + headers = ('cassert', 'cctype', 'cerrno', 'cfenv', 'cfloat', 'cinttypes', + 'climits', 'clocale', 'csetjmp', 'csignal', 'cstdarg', 'cstddef', + 'cstdint', 'cstdio', 'cstdlib', 'cstring', 'ctime', 'cuchar', + 'cwchar', 'cwctype') + include_regex = re.compile(r'^#include <([a-z.]*)>') def __init__(self, content): super().__init__() @@ -588,8 +588,9 @@ class IncludeChecker(StyleChecker): if header not in IncludeChecker.headers: continue + header = header[1:] + '.h' issues.append(StyleIssue(line_number, line, - 'C compatibility header <%s.h> is preferred' % header)) + 'C compatibility header <%s> is preferred' % header)) return issues From patchwork Fri May 31 12:18:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 20167 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 40DBBC32C8 for ; Fri, 31 May 2024 12:19:00 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A08EC634BF; Fri, 31 May 2024 14:18:59 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="pSYUZMUf"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1B8EF61A46 for ; Fri, 31 May 2024 14:18:55 +0200 (CEST) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2294F11A3 for ; Fri, 31 May 2024 14:18:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1717157930; bh=SUPECAxwvJDvRobZb/B9YJ9K3KpS0L2nzLsr6y8W1Wo=; h=From:To:Subject:Date:In-Reply-To:References:From; b=pSYUZMUf9nt60Izzy79OqQj/tByYrcjQTpUuGBj1fl/igC86FoaZHIb3N1vmor8N4 rJctmcABHjpq5ca3kYSAamt/9Q+uDWAOSbwyKHHgbLykQQ89H/XXVVaiQmn/Qr6efL hBxLf5jfA12vXH1YKzaY6klB0DX7ixiMuSKwDtLs= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH 2/4] utils: checkstyle.py: Extend IncludeChecker to cover math.h Date: Fri, 31 May 2024 15:18:36 +0300 Message-ID: <20240531121838.27643-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240531121838.27643-1-laurent.pinchart@ideasonboard.com> References: <20240531121838.27643-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" math.h is an exception to the C compatibility header rule, as we prefer using cmath. Extend the IncludeCheck to warn about it. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Reviewed-by: Kieran Bingham Tested-by: Milan Zamazal --- utils/checkstyle.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index 518be0897db5..f3604299de94 100755 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -568,7 +568,7 @@ class IncludeChecker(StyleChecker): headers = ('cassert', 'cctype', 'cerrno', 'cfenv', 'cfloat', 'cinttypes', 'climits', 'clocale', 'csetjmp', 'csignal', 'cstdarg', 'cstddef', 'cstdint', 'cstdio', 'cstdlib', 'cstring', 'ctime', 'cuchar', - 'cwchar', 'cwctype') + 'cwchar', 'cwctype', 'math.h') include_regex = re.compile(r'^#include <([a-z.]*)>') def __init__(self, content): @@ -588,9 +588,15 @@ class IncludeChecker(StyleChecker): if header not in IncludeChecker.headers: continue - header = header[1:] + '.h' + if header.endswith('.h'): + header_type = 'C++' + header = 'c' + header[:-2] + else: + header_type = 'C compatibility' + header = header[1:] + '.h' + issues.append(StyleIssue(line_number, line, - 'C compatibility header <%s> is preferred' % header)) + f'{header_type} header <{header}> is preferred')) return issues From patchwork Fri May 31 12:18:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 20168 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 16293BDE6B for ; Fri, 31 May 2024 12:19:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 26665634BB; Fri, 31 May 2024 14:19:01 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="nJmGtGAJ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 71458634B6 for ; Fri, 31 May 2024 14:18:56 +0200 (CEST) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 78A3611A3 for ; Fri, 31 May 2024 14:18:51 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1717157931; bh=/grH61Oa58Xd/XOJhnfvS+wRUgiDdZb2p+iyyZJZDyU=; h=From:To:Subject:Date:In-Reply-To:References:From; b=nJmGtGAJrqQV7VczXjdwbKnsrPhhc6eDBuUAbfmQZ9Z9paiQ3rW6218AFwnusCCid ykOa/2KWbJ/X6gxWLXwqEl3YLZd448OBoD3kX8a29a+FywpyFCptFpJo/svZza3V/u Is603j9kBowGp0qv7CyxDnVACQbsXO5Z8QsVV/fM= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH 3/4] utils: checkstyle.py: Add a check for hex values Date: Fri, 31 May 2024 15:18:37 +0300 Message-ID: <20240531121838.27643-4-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240531121838.27643-1-laurent.pinchart@ideasonboard.com> References: <20240531121838.27643-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" libcamera uses lowercase hex values. Add a corresponding checker. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Reviewed-by: Kieran Bingham --- utils/checkstyle.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index f3604299de94..77d6f39011c1 100755 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -562,6 +562,34 @@ class StyleIssue(object): self.msg = msg +class HexValueChecker(StyleChecker): + patterns = ('*.c', '*.cpp', '*.h') + + regex = re.compile(r'\b0[xX][0-9a-fA-F]+\b') + + def __init__(self, content): + super().__init__() + self.__content = content + + def check(self, line_numbers): + issues = [] + + for line_number in line_numbers: + line = self.__content[line_number - 1] + match = HexValueChecker.regex.search(line) + if not match: + continue + + value = match.group(0) + if value == value.lower(): + continue + + issues.append(StyleIssue(line_number, line, + f'Use lowercase hex constant {value.lower()}')) + + return issues + + class IncludeChecker(StyleChecker): patterns = ('*.cpp', '*.h') From patchwork Fri May 31 12:18:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 20169 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 160B0C32C8 for ; Fri, 31 May 2024 12:19:05 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 96582634BF; Fri, 31 May 2024 14:19:04 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="v/Ma/zOk"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D6547634BA for ; Fri, 31 May 2024 14:18:57 +0200 (CEST) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id C67B62D31 for ; Fri, 31 May 2024 14:18:52 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1717157933; bh=dMuaUzS9Cdw7lJy89TgbPuxOPHvtN+1WWwwbR1Oa6K0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=v/Ma/zOkdil7hbKIjjSgg21zT+6diipD0jZRxG8JA7pgk57QgOwrV7UdizWWv0tm4 8RWz9tepIS37/JccesSAXAl5YWRyscdqKUtxRF01L3m+zOFj4w6SsSoH9vCHn0PBqB aZZdpGSjoq2ZqakxZ3n3b50+gdjGIAnd16LgIPRE= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH 4/4] utils: checkstyle.py: Show location of coding style issue within line Date: Fri, 31 May 2024 15:18:38 +0300 Message-ID: <20240531121838.27643-5-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240531121838.27643-1-laurent.pinchart@ideasonboard.com> References: <20240531121838.27643-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The issue checkers display the line number and line content of each offending line, but don't show the location of the issue within a line. Improve checkstyle by adding a marker that points to the exact location. Signed-off-by: Laurent Pinchart Reviewed-by: Paul Elder Reviewed-by: Kieran Bingham --- utils/checkstyle.py | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/utils/checkstyle.py b/utils/checkstyle.py index 77d6f39011c1..ff885aa98d65 100755 --- a/utils/checkstyle.py +++ b/utils/checkstyle.py @@ -556,8 +556,9 @@ class StyleChecker(metaclass=ClassRegistry): class StyleIssue(object): - def __init__(self, line_number, line, msg): + def __init__(self, line_number, position, line, msg): self.line_number = line_number + self.position = position self.line = line self.msg = msg @@ -584,7 +585,7 @@ class HexValueChecker(StyleChecker): if value == value.lower(): continue - issues.append(StyleIssue(line_number, line, + issues.append(StyleIssue(line_number, match.span(0), line, f'Use lowercase hex constant {value.lower()}')) return issues @@ -623,7 +624,7 @@ class IncludeChecker(StyleChecker): header_type = 'C compatibility' header = header[1:] + '.h' - issues.append(StyleIssue(line_number, line, + issues.append(StyleIssue(line_number, match.span(1), line, f'{header_type} header <{header}> is preferred')) return issues @@ -641,10 +642,12 @@ class LogCategoryChecker(StyleChecker): issues = [] for line_number in line_numbers: line = self.__content[line_number-1] - if not LogCategoryChecker.log_regex.search(line): + match = LogCategoryChecker.log_regex.search(line) + if not match: continue - issues.append(StyleIssue(line_number, line, 'LOG() should use categories')) + issues.append(StyleIssue(line_number, match.span(1), line, + 'LOG() should use categories')) return issues @@ -660,8 +663,10 @@ class MesonChecker(StyleChecker): issues = [] for line_number in line_numbers: line = self.__content[line_number-1] - if line.find('\t') != -1: - issues.append(StyleIssue(line_number, line, 'meson.build should use spaces for indentation')) + pos = line.find('\t') + if pos != -1: + issues.append(StyleIssue(line_number, [pos, pos], line, + 'meson.build should use spaces for indentation')) return issues @@ -681,7 +686,7 @@ class Pep8Checker(StyleChecker): ret = subprocess.run(['pycodestyle', '--ignore=E501', '-'], input=data, stdout=subprocess.PIPE) except FileNotFoundError: - issues.append(StyleIssue(0, None, 'Please install pycodestyle to validate python additions')) + issues.append(StyleIssue(0, None, None, 'Please install pycodestyle to validate python additions')) return issues results = ret.stdout.decode('utf-8').splitlines() @@ -693,7 +698,7 @@ class Pep8Checker(StyleChecker): if line_number in line_numbers: line = self.__content[line_number - 1] - issues.append(StyleIssue(line_number, line, msg)) + issues.append(StyleIssue(line_number, None, line, msg)) return issues @@ -714,7 +719,7 @@ class ShellChecker(StyleChecker): ret = subprocess.run(['shellcheck', '-Cnever', '-'], input=data, stdout=subprocess.PIPE) except FileNotFoundError: - issues.append(StyleIssue(0, None, 'Please install shellcheck to validate shell script additions')) + issues.append(StyleIssue(0, None, None, 'Please install shellcheck to validate shell script additions')) return issues results = ret.stdout.decode('utf-8').splitlines() @@ -727,11 +732,8 @@ class ShellChecker(StyleChecker): line = results[nr + 1] msg = results[nr + 2] - # Determined, but not yet used - position = msg.find('^') + 1 - if line_number in line_numbers: - issues.append(StyleIssue(line_number, line, msg)) + issues.append(StyleIssue(line_number, None, line, msg)) return issues @@ -973,6 +975,15 @@ def check_file(top_level, commit, filename, checkers): print('%s+%s%s' % (Colours.fg(Colours.Yellow), issue.line.rstrip(), Colours.reset())) + if issue.position is not None: + # Align the position marker by using the original line with + # all characters by tabs replaced with spaces. This ensures + # proper alignment regardless of how the code is indented. + start = issue.position[0] + prefix = ''.join([c if c == '\t' else ' ' for c in issue.line[:start]]) + length = issue.position[1] - start - 1 + print(' ' + prefix + '^' + '~' * length) + return len(formatted_diff) + len(issues)