diff mbox

[01/29] coccinelle: add a script to enforce qemu/osdep.h macros usage

Message ID 20170718061005.29518-2-f4bug@amsat.org
State New
Headers show

Commit Message

Philippe Mathieu-Daudé July 18, 2017, 6:09 a.m. UTC
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 scripts/coccinelle/use_osdep.cocci | 60 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
 create mode 100644 scripts/coccinelle/use_osdep.cocci

Comments

Eric Blake July 18, 2017, 3:18 p.m. UTC | #1
On 07/18/2017 01:09 AM, Philippe Mathieu-Daudé wrote:
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

Might be worth mentioning which macros in the commit message body, so
that a grep of 'git log' spots this commit easier.

> ---
>  scripts/coccinelle/use_osdep.cocci | 60 ++++++++++++++++++++++++++++++++++++++
>  1 file changed, 60 insertions(+)
>  create mode 100644 scripts/coccinelle/use_osdep.cocci
> 
> +// docker run --rm -v `pwd`:`pwd` -w `pwd` philmd/coccinelle \
> +//     --macro-file scripts/cocci-macro-file.h \
> +//     --sp-file scripts/coccinelle/add_osdep.cocci \
> +//     --keep-comments --in-place \
> +//     --use-gitgrep --dir .

Is '--use-gitgrep' a relatively new option to spatch (not present in my
F26 coccinelle-1.0.6-7), or is it a directive specific to the docker
coccinelle package you are using?  It's somewhat nice if there is a
clean separation between which arguments are for spatch and which are
for docker.

> +
> +// Use QEMU_IS_ALIGNED()
> +@@
> +typedef uintptr_t;
> +uintptr_t ptr;
> +expression n, m;
> +@@
> +(
> +-ptr % m == 0
> ++QEMU_PTR_IS_ALIGNED(ptr, m)
> +|
> +-n % m == 0
> ++QEMU_IS_ALIGNED(n, m)
> +)

Is it worth also trying to flag '(n & (m - 1)) == 0' (for when m is a
power of 2)?

> +
> +// Use QEMU_ALIGN_DOWN()
> +@@
> +expression n, m;
> +@@
> +-n / m * m
> ++QEMU_ALIGN_DOWN(n, m)

Is it worth also trying to catch (n >> m) << m?
diff mbox

Patch

diff --git a/scripts/coccinelle/use_osdep.cocci b/scripts/coccinelle/use_osdep.cocci
new file mode 100644
index 0000000000..356b36e358
--- /dev/null
+++ b/scripts/coccinelle/use_osdep.cocci
@@ -0,0 +1,60 @@ 
+// Use macros from qemu/osdep.h
+//
+// Copyright: (C) 2017 Philippe Mathieu-Daudé. GPLv2+.
+// Confidence: High
+// Options: --macro-file scripts/cocci-macro-file.h
+//
+// docker run --rm -v `pwd`:`pwd` -w `pwd` philmd/coccinelle \
+//     --macro-file scripts/cocci-macro-file.h \
+//     --sp-file scripts/coccinelle/add_osdep.cocci \
+//     --keep-comments --in-place \
+//     --use-gitgrep --dir .
+
+// Use QEMU_IS_ALIGNED()
+@@
+typedef uintptr_t;
+uintptr_t ptr;
+expression n, m;
+@@
+(
+-ptr % m == 0
++QEMU_PTR_IS_ALIGNED(ptr, m)
+|
+-n % m == 0
++QEMU_IS_ALIGNED(n, m)
+)
+
+// Use QEMU_ALIGN_DOWN()
+@@
+expression n, m;
+@@
+-n / m * m
++QEMU_ALIGN_DOWN(n, m)
+
+// Use ARRAY_SIZE()
+@@
+expression x;
+@@
+-sizeof(x) / sizeof((x)[0])
++ARRAY_SIZE(x)
+
+// Drop superfluous parenthesis
+@@
+expression n, m;
+@@
+(
+-(QEMU_IS_ALIGNED(n, m))
++QEMU_IS_ALIGNED(n, m)
+|
+-QEMU_IS_ALIGNED((n), m)
++QEMU_IS_ALIGNED(n, m)
+|
+-(QEMU_PTR_IS_ALIGNED(n, m))
++QEMU_PTR_IS_ALIGNED(n, m)
+|
+-QEMU_ALIGN_DOWN((n), m)
++QEMU_ALIGN_DOWN(n, m)
+|
+-(ARRAY_SIZE(n))
++ARRAY_SIZE(n)
+)