[libcamera-devel,v2,1/5] utils: checkstyle.py: Add d-pointer formatter
diff mbox series

Message ID 20201020014005.12783-2-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • libcamera: Implement d-pointer design pattern
Related show

Commit Message

Laurent Pinchart Oct. 20, 2020, 1:40 a.m. UTC
Add a formatter to ensure consistent naming of 'd' and 'o' variables
related to the d-pointer design pattern, as implemented by the
Extensible class. The formatter also ensures that the pointer is always
const. const-correctness issues related to the data pointed to will be
caught by the compiler, and thus don't need to be checked here.

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

Comments

Niklas Söderlund Oct. 20, 2020, 10:59 a.m. UTC | #1
Hi Laurent,

Thanks for your work.

On 2020-10-20 04:40:01 +0300, Laurent Pinchart wrote:
> Add a formatter to ensure consistent naming of 'd' and 'o' variables
> related to the d-pointer design pattern, as implemented by the
> Extensible class. The formatter also ensures that the pointer is always
> const. const-correctness issues related to the data pointed to will be
> caught by the compiler, and thus don't need to be checked here.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  utils/checkstyle.py | 32 ++++++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
> 
> diff --git a/utils/checkstyle.py b/utils/checkstyle.py
> index d5dc26c0f666..7225cac47a4e 100755
> --- a/utils/checkstyle.py
> +++ b/utils/checkstyle.py
> @@ -481,6 +481,38 @@ class DoxygenFormatter(Formatter):
>          return '\n'.join(lines)
>  
>  
> +class DPointerFormatter(Formatter):
> +    # Ensure consistent naming of variables related to the d-pointer design
> +    # pattern.
> +    patterns = ('*.cpp', '*.h')
> +
> +    # The clang formatter runs first, we can thus rely on appropriate coding
> +    # style.
> +    declare_regex = re.compile(r'^(\t*)(const )?([a-zA-Z0-9_]+) \*( ?const )?([a-zA-Z0-9_]+) = (LIBCAMERA_[DO]_PTR)\(([a-zA-Z0-9_]+)\);$')
> +
> +    @classmethod
> +    def format(cls, filename, data):
> +        lines = []
> +
> +        for line in data.split('\n'):
> +            match = cls.declare_regex.match(line)
> +            if match:
> +                indent = match.group(1) or ''
> +                const = match.group(2) or ''
> +                macro = match.group(6)
> +                klass = match.group(7)
> +                if macro == 'LIBCAMERA_D_PTR':
> +                    var = 'Private *const d'
> +                else:
> +                    var = f'{klass} *const o'
> +
> +                line = f'{indent}{const}{var} = {macro}({klass});'
> +
> +            lines.append(line)
> +
> +        return '\n'.join(lines)
> +
> +
>  class IncludeOrderFormatter(Formatter):
>      patterns = ('*.cpp', '*.h')
>  
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch
diff mbox series

diff --git a/utils/checkstyle.py b/utils/checkstyle.py
index d5dc26c0f666..7225cac47a4e 100755
--- a/utils/checkstyle.py
+++ b/utils/checkstyle.py
@@ -481,6 +481,38 @@  class DoxygenFormatter(Formatter):
         return '\n'.join(lines)
 
 
+class DPointerFormatter(Formatter):
+    # Ensure consistent naming of variables related to the d-pointer design
+    # pattern.
+    patterns = ('*.cpp', '*.h')
+
+    # The clang formatter runs first, we can thus rely on appropriate coding
+    # style.
+    declare_regex = re.compile(r'^(\t*)(const )?([a-zA-Z0-9_]+) \*( ?const )?([a-zA-Z0-9_]+) = (LIBCAMERA_[DO]_PTR)\(([a-zA-Z0-9_]+)\);$')
+
+    @classmethod
+    def format(cls, filename, data):
+        lines = []
+
+        for line in data.split('\n'):
+            match = cls.declare_regex.match(line)
+            if match:
+                indent = match.group(1) or ''
+                const = match.group(2) or ''
+                macro = match.group(6)
+                klass = match.group(7)
+                if macro == 'LIBCAMERA_D_PTR':
+                    var = 'Private *const d'
+                else:
+                    var = f'{klass} *const o'
+
+                line = f'{indent}{const}{var} = {macro}({klass});'
+
+            lines.append(line)
+
+        return '\n'.join(lines)
+
+
 class IncludeOrderFormatter(Formatter):
     patterns = ('*.cpp', '*.h')