{"id":20169,"url":"https://patchwork.libcamera.org/api/patches/20169/?format=json","web_url":"https://patchwork.libcamera.org/patch/20169/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20240531121838.27643-5-laurent.pinchart@ideasonboard.com>","date":"2024-05-31T12:18:38","name":"[4/4] utils: checkstyle.py: Show location of coding style issue within line","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"eccbb32888c5fe0487fffd142d42f0c3bf5c213a","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/20169/mbox/","series":[{"id":4346,"url":"https://patchwork.libcamera.org/api/series/4346/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4346","date":"2024-05-31T12:18:34","name":"utils: checkstyle.py: Miscellaneous improvements","version":1,"mbox":"https://patchwork.libcamera.org/series/4346/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/20169/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/20169/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 160B0C32C8\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 31 May 2024 12:19:05 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 96582634BF;\n\tFri, 31 May 2024 14:19:04 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id D6547634BA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 31 May 2024 14:18:57 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id C67B62D31\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 31 May 2024 14:18:52 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"v/Ma/zOk\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1717157933;\n\tbh=dMuaUzS9Cdw7lJy89TgbPuxOPHvtN+1WWwwbR1Oa6K0=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=v/Ma/zOkdil7hbKIjjSgg21zT+6diipD0jZRxG8JA7pgk57QgOwrV7UdizWWv0tm4\n\t8RWz9tepIS37/JccesSAXAl5YWRyscdqKUtxRF01L3m+zOFj4w6SsSoH9vCHn0PBqB\n\taZZdpGSjoq2ZqakxZ3n3b50+gdjGIAnd16LgIPRE=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Subject":"[PATCH 4/4] utils: checkstyle.py: Show location of coding style\n\tissue 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","Content-Transfer-Encoding":"8bit","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The issue checkers display the line number and line content of each\noffending line, but don't show the location of the issue within a line.\nImprove checkstyle by adding a marker that points to the exact location.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n utils/checkstyle.py | 39 +++++++++++++++++++++++++--------------\n 1 file changed, 25 insertions(+), 14 deletions(-)","diff":"diff --git a/utils/checkstyle.py b/utils/checkstyle.py\nindex 77d6f39011c1..ff885aa98d65 100755\n--- a/utils/checkstyle.py\n+++ b/utils/checkstyle.py\n@@ -556,8 +556,9 @@ class StyleChecker(metaclass=ClassRegistry):\n \n \n class StyleIssue(object):\n-    def __init__(self, line_number, line, msg):\n+    def __init__(self, line_number, position, line, msg):\n         self.line_number = line_number\n+        self.position = position\n         self.line = line\n         self.msg = msg\n \n@@ -584,7 +585,7 @@ class HexValueChecker(StyleChecker):\n             if value == value.lower():\n                 continue\n \n-            issues.append(StyleIssue(line_number, line,\n+            issues.append(StyleIssue(line_number, match.span(0), line,\n                                      f'Use lowercase hex constant {value.lower()}'))\n \n         return issues\n@@ -623,7 +624,7 @@ class IncludeChecker(StyleChecker):\n                 header_type = 'C compatibility'\n                 header = header[1:] + '.h'\n \n-            issues.append(StyleIssue(line_number, line,\n+            issues.append(StyleIssue(line_number, match.span(1), line,\n                                      f'{header_type} header <{header}> is preferred'))\n \n         return issues\n@@ -641,10 +642,12 @@ class LogCategoryChecker(StyleChecker):\n         issues = []\n         for line_number in line_numbers:\n             line = self.__content[line_number-1]\n-            if not LogCategoryChecker.log_regex.search(line):\n+            match = LogCategoryChecker.log_regex.search(line)\n+            if not match:\n                 continue\n \n-            issues.append(StyleIssue(line_number, line, 'LOG() should use categories'))\n+            issues.append(StyleIssue(line_number, match.span(1), line,\n+                                     'LOG() should use categories'))\n \n         return issues\n \n@@ -660,8 +663,10 @@ class MesonChecker(StyleChecker):\n         issues = []\n         for line_number in line_numbers:\n             line = self.__content[line_number-1]\n-            if line.find('\\t') != -1:\n-                issues.append(StyleIssue(line_number, line, 'meson.build should use spaces for indentation'))\n+            pos = line.find('\\t')\n+            if pos != -1:\n+                issues.append(StyleIssue(line_number, [pos, pos], line,\n+                                         'meson.build should use spaces for indentation'))\n         return issues\n \n \n@@ -681,7 +686,7 @@ class Pep8Checker(StyleChecker):\n             ret = subprocess.run(['pycodestyle', '--ignore=E501', '-'],\n                                  input=data, stdout=subprocess.PIPE)\n         except FileNotFoundError:\n-            issues.append(StyleIssue(0, None, 'Please install pycodestyle to validate python additions'))\n+            issues.append(StyleIssue(0, None, None, 'Please install pycodestyle to validate python additions'))\n             return issues\n \n         results = ret.stdout.decode('utf-8').splitlines()\n@@ -693,7 +698,7 @@ class Pep8Checker(StyleChecker):\n \n             if line_number in line_numbers:\n                 line = self.__content[line_number - 1]\n-                issues.append(StyleIssue(line_number, line, msg))\n+                issues.append(StyleIssue(line_number, None, line, msg))\n \n         return issues\n \n@@ -714,7 +719,7 @@ class ShellChecker(StyleChecker):\n             ret = subprocess.run(['shellcheck', '-Cnever', '-'],\n                                  input=data, stdout=subprocess.PIPE)\n         except FileNotFoundError:\n-            issues.append(StyleIssue(0, None, 'Please install shellcheck to validate shell script additions'))\n+            issues.append(StyleIssue(0, None, None, 'Please install shellcheck to validate shell script additions'))\n             return issues\n \n         results = ret.stdout.decode('utf-8').splitlines()\n@@ -727,11 +732,8 @@ class ShellChecker(StyleChecker):\n             line = results[nr + 1]\n             msg = results[nr + 2]\n \n-            # Determined, but not yet used\n-            position = msg.find('^') + 1\n-\n             if line_number in line_numbers:\n-                issues.append(StyleIssue(line_number, line, msg))\n+                issues.append(StyleIssue(line_number, None, line, msg))\n \n         return issues\n \n@@ -973,6 +975,15 @@ def check_file(top_level, commit, filename, checkers):\n                 print('%s+%s%s' % (Colours.fg(Colours.Yellow), issue.line.rstrip(),\n                                    Colours.reset()))\n \n+                if issue.position is not None:\n+                    # Align the position marker by using the original line with\n+                    # all characters by tabs replaced with spaces. This ensures\n+                    # proper alignment regardless of how the code is indented.\n+                    start = issue.position[0]\n+                    prefix = ''.join([c if c == '\\t' else ' ' for c in issue.line[:start]])\n+                    length = issue.position[1] - start - 1\n+                    print(' ' + prefix + '^' + '~' * length)\n+\n     return len(formatted_diff) + len(issues)\n \n \n","prefixes":["4/4"]}