[3/4] utils: checkstyle.py: Add a check for hex values
diff mbox series

Message ID 20240531121838.27643-4-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • utils: checkstyle.py: Miscellaneous improvements
Related show

Commit Message

Laurent Pinchart May 31, 2024, 12:18 p.m. UTC
libcamera uses lowercase hex values. Add a corresponding checker.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 utils/checkstyle.py | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

Comments

Paul Elder May 31, 2024, 1:18 p.m. UTC | #1
On Fri, May 31, 2024 at 03:18:37PM +0300, Laurent Pinchart wrote:
> libcamera uses lowercase hex values. Add a corresponding checker.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>

> ---
>  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')
Kieran Bingham May 31, 2024, 1:35 p.m. UTC | #2
Quoting Laurent Pinchart (2024-05-31 13:18:37)
> libcamera uses lowercase hex values. Add a corresponding checker.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> ---
>  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')
>  
> -- 
> Regards,
> 
> Laurent Pinchart
>

Patch
diff mbox series

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')