[1/4] utils: checkstyle.py: Refactor IncludeChecker
diff mbox series

Message ID 20240531121838.27643-2-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
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 <laurent.pinchart@ideasonboard.com>
---
 utils/checkstyle.py | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

Comments

Paul Elder May 31, 2024, 1:17 p.m. UTC | #1
On Fri, May 31, 2024 at 03:18:35PM +0300, Laurent Pinchart wrote:
> 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 <laurent.pinchart@ideasonboard.com>

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

> ---
>  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 <c([a-z]*)>')
> +    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
Kieran Bingham May 31, 2024, 1:33 p.m. UTC | #2
Quoting Laurent Pinchart (2024-05-31 13:18:35)
> 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 <laurent.pinchart@ideasonboard.com>

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

> ---
>  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 <c([a-z]*)>')
> +    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
>  
> -- 
> Regards,
> 
> Laurent Pinchart
>

Patch
diff mbox series

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 <c([a-z]*)>')
+    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