[libcamera-devel,3/4] utils: checkstyle.py: Add include checker

Message ID 20191023135258.32256-3-laurent.pinchart@ideasonboard.com
State Accepted
Commit bb5f8cf4951fc5813d60ab806c57c5b5d713f38c
Headers show
Series
  • [libcamera-devel,1/4] Documentation: coding-style: Document usage of C compatibility headers
Related show

Commit Message

Laurent Pinchart Oct. 23, 2019, 1:52 p.m. UTC
---
 utils/checkstyle.py | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

Comments

Laurent Pinchart Oct. 23, 2019, 2:08 p.m. UTC | #1
This is missing a commit message, sorry :-S

Add an include checker to verify usage of the C compatibility headers.
  
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

On Wed, Oct 23, 2019 at 04:52:57PM +0300, Laurent Pinchart wrote:
> ---
>  utils/checkstyle.py | 32 ++++++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
> 
> diff --git a/utils/checkstyle.py b/utils/checkstyle.py
> index 42a96f6d6102..335e58f5fddf 100755
> --- a/utils/checkstyle.py
> +++ b/utils/checkstyle.py
> @@ -240,6 +240,38 @@ class StyleIssue(object):
>          self.msg = msg
>  
>  
> +class IncludeChecker(StyleChecker):
> +    patterns = ('*.cpp', '*.h')
> +
> +    headers = ('assert', 'ctype', 'errno', 'fenv', 'float', 'inttypes',
> +               'limits', 'locale', 'math', 'setjmp', 'signal', 'stdarg',
> +               'stddef', 'stdint', 'stdio', 'stdlib', 'string', 'time', 'uchar',
> +               'wchar', 'wctype')
> +    include_regex = re.compile('^#include <c([a-z]*)>')
> +
> +    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 = IncludeChecker.include_regex.match(line)
> +            if not match:
> +                continue
> +
> +            header = match.group(1)
> +            if header not in IncludeChecker.headers:
> +                continue
> +
> +            issues.append(StyleIssue(line_number, line,
> +                                     'C compatibility header <%s.h> is preferred' % header))
> +
> +        return issues
> +
> +
>  class LogCategoryChecker(StyleChecker):
>      log_regex = re.compile('\\bLOG\((Debug|Info|Warning|Error|Fatal)\)')
>      patterns = ('*.cpp',)
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel
Kieran Bingham Oct. 23, 2019, 2:10 p.m. UTC | #2
Hi Laurent,

On 23/10/2019 14:52, Laurent Pinchart wrote:

 ..insert commit log here.. ?

 ..insert SoB here..?

With the above populated,

Reviewed-by: <...insert tag here...>


> ---
>  utils/checkstyle.py | 32 ++++++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
> 
> diff --git a/utils/checkstyle.py b/utils/checkstyle.py
> index 42a96f6d6102..335e58f5fddf 100755
> --- a/utils/checkstyle.py
> +++ b/utils/checkstyle.py
> @@ -240,6 +240,38 @@ class StyleIssue(object):
>          self.msg = msg
>  
>  
> +class IncludeChecker(StyleChecker):
> +    patterns = ('*.cpp', '*.h')
> +
> +    headers = ('assert', 'ctype', 'errno', 'fenv', 'float', 'inttypes',
> +               'limits', 'locale', 'math', 'setjmp', 'signal', 'stdarg',
> +               'stddef', 'stdint', 'stdio', 'stdlib', 'string', 'time', 'uchar',
> +               'wchar', 'wctype')

Is this an exhaustive list? (defined by the spec?) or will there
potentially be later additions?

I expect it's a full list, but I don't know where it's referenced from.
Might be worth putting a reference to where the list was determined from
if it was extracted from somewhere...


> +    include_regex = re.compile('^#include <c([a-z]*)>')
> +
> +    def __init__(self, content):
> +        super().__init__()
> +        self.__content = content
> +

I expect this is fine, and I think I recall other checkers do this - but
/why/ do we store the content again for every checker, rather than parse
it line by line?




> +    def check(self, line_numbers):
> +        issues = []
> +
> +        for line_number in line_numbers:
> +            line = self.__content[line_number - 1]
> +            match = IncludeChecker.include_regex.match(line)
> +            if not match:
> +                continue
> +
> +            header = match.group(1)
> +            if header not in IncludeChecker.headers:
> +                continue
> +
> +            issues.append(StyleIssue(line_number, line,
> +                                     'C compatibility header <%s.h> is preferred' % header))
> +
> +        return issues
> +
> +
>  class LogCategoryChecker(StyleChecker):
>      log_regex = re.compile('\\bLOG\((Debug|Info|Warning|Error|Fatal)\)')
>      patterns = ('*.cpp',)
>

Patch

diff --git a/utils/checkstyle.py b/utils/checkstyle.py
index 42a96f6d6102..335e58f5fddf 100755
--- a/utils/checkstyle.py
+++ b/utils/checkstyle.py
@@ -240,6 +240,38 @@  class StyleIssue(object):
         self.msg = msg
 
 
+class IncludeChecker(StyleChecker):
+    patterns = ('*.cpp', '*.h')
+
+    headers = ('assert', 'ctype', 'errno', 'fenv', 'float', 'inttypes',
+               'limits', 'locale', 'math', 'setjmp', 'signal', 'stdarg',
+               'stddef', 'stdint', 'stdio', 'stdlib', 'string', 'time', 'uchar',
+               'wchar', 'wctype')
+    include_regex = re.compile('^#include <c([a-z]*)>')
+
+    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 = IncludeChecker.include_regex.match(line)
+            if not match:
+                continue
+
+            header = match.group(1)
+            if header not in IncludeChecker.headers:
+                continue
+
+            issues.append(StyleIssue(line_number, line,
+                                     'C compatibility header <%s.h> is preferred' % header))
+
+        return issues
+
+
 class LogCategoryChecker(StyleChecker):
     log_regex = re.compile('\\bLOG\((Debug|Info|Warning|Error|Fatal)\)')
     patterns = ('*.cpp',)