diff mbox series

[02/88] cocci: add more g_new() transformations

Message ID 20171006235023.11952-3-f4bug@amsat.org
State New
Headers show
Series use g_new() family of functions | expand

Commit Message

Philippe Mathieu-Daudé Oct. 6, 2017, 11:48 p.m. UTC
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 scripts/coccinelle/g_new.cocci | 73 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

Comments

Markus Armbruster Oct. 9, 2017, 7:28 a.m. UTC | #1
The commit message should explain the additional transformations briefly
and informally.  In particular, why they're a good idea.  Same for PATCH
3-5.

Philippe Mathieu-Daudé <f4bug@amsat.org> writes:

> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
>  scripts/coccinelle/g_new.cocci | 73 ++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 73 insertions(+)
>
> diff --git a/scripts/coccinelle/g_new.cocci b/scripts/coccinelle/g_new.cocci
> index 1e57685a6b..c767325a21 100644
> --- a/scripts/coccinelle/g_new.cocci
> +++ b/scripts/coccinelle/g_new.cocci
> @@ -80,6 +80,49 @@ expression p, n;
>  -g_try_realloc(p, sizeof(T) * (n))
>  +g_try_renew(T, p, n)
>  
> +@@
> +type T;
> +expression n;
> +@@
> +(
> +-g_malloc_n(n, sizeof(T))
> ++g_new(T, n)
> +|
> +-g_malloc0_n(n, sizeof(T))
> ++g_new0(T, n)
> +|
> +-g_try_malloc_n(n, sizeof(T))
> ++g_try_new(T, n)
> +|
> +-g_try_malloc0_n(n, sizeof(T))
> ++g_try_new0(T, n)
> +)
> +
> +@@
> +type T;
> +identifier m;
> +@@
> +T *m;
> +...
> +(
> +-m = g_malloc(sizeof(*m));
> ++m = g_new(T, 1);
> +|
> +-m = g_malloc0(sizeof(*m));
> ++m = g_new0(T, 1);
> +|
> +-m = g_try_malloc(sizeof(*m));
> ++m = g_try_new(T, 1);
> +|
> +-m = g_try_malloc0(sizeof(*m));
> ++m = g_try_new0(T, 1);
> +)
> +
> +////////////////////////////////////////

Please scratch this line.  Overly elaborate comment decoration.

> +//
> +// last transformations: cleanups
> +//
> +
>  // drop superfluous cast
>  @@
>  type T;
> @@ -99,3 +142,33 @@ expression p, n;
>  @@
>  -(T *)g_renew(T, p, n)
>  +g_renew(T, p, n)
> +@@
> +type T;
> +expression n;
> +@@
> +(
> +-(T *)g_try_new(T, n)
> ++g_try_new(T, n)
> +|
> +-(T *)g_try_new0(T, n)
> ++g_try_new0(T, n)
> +)
> +
> +// drop superfluous parenthesis
> +@@
> +type T;
> +expression c;
> +@@
> +(
> +-g_new(T, (c))
> ++g_new(T, c)
> +|
> +-g_try_new(T, (c))
> ++g_try_new(T, c)
> +|
> +-g_new0(T, (c))
> ++g_new0(T, c)
> +|
> +-g_try_new0(T, (c))
> ++g_try_new0(T, c)
> +)
diff mbox series

Patch

diff --git a/scripts/coccinelle/g_new.cocci b/scripts/coccinelle/g_new.cocci
index 1e57685a6b..c767325a21 100644
--- a/scripts/coccinelle/g_new.cocci
+++ b/scripts/coccinelle/g_new.cocci
@@ -80,6 +80,49 @@  expression p, n;
 -g_try_realloc(p, sizeof(T) * (n))
 +g_try_renew(T, p, n)
 
+@@
+type T;
+expression n;
+@@
+(
+-g_malloc_n(n, sizeof(T))
++g_new(T, n)
+|
+-g_malloc0_n(n, sizeof(T))
++g_new0(T, n)
+|
+-g_try_malloc_n(n, sizeof(T))
++g_try_new(T, n)
+|
+-g_try_malloc0_n(n, sizeof(T))
++g_try_new0(T, n)
+)
+
+@@
+type T;
+identifier m;
+@@
+T *m;
+...
+(
+-m = g_malloc(sizeof(*m));
++m = g_new(T, 1);
+|
+-m = g_malloc0(sizeof(*m));
++m = g_new0(T, 1);
+|
+-m = g_try_malloc(sizeof(*m));
++m = g_try_new(T, 1);
+|
+-m = g_try_malloc0(sizeof(*m));
++m = g_try_new0(T, 1);
+)
+
+////////////////////////////////////////
+//
+// last transformations: cleanups
+//
+
 // drop superfluous cast
 @@
 type T;
@@ -99,3 +142,33 @@  expression p, n;
 @@
 -(T *)g_renew(T, p, n)
 +g_renew(T, p, n)
+@@
+type T;
+expression n;
+@@
+(
+-(T *)g_try_new(T, n)
++g_try_new(T, n)
+|
+-(T *)g_try_new0(T, n)
++g_try_new0(T, n)
+)
+
+// drop superfluous parenthesis
+@@
+type T;
+expression c;
+@@
+(
+-g_new(T, (c))
++g_new(T, c)
+|
+-g_try_new(T, (c))
++g_try_new(T, c)
+|
+-g_new0(T, (c))
++g_new0(T, c)
+|
+-g_try_new0(T, (c))
++g_try_new0(T, c)
+)