diff mbox

[01/16] coccinelle: use macro DIV_ROUND_UP instead of (((n) + (d) - 1) /(d))

Message ID 1464712565-14857-2-git-send-email-lvivier@redhat.com
State New
Headers show

Commit Message

Laurent Vivier May 31, 2016, 4:35 p.m. UTC
sample from http://coccinellery.org/

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
 scripts/coccinelle/round.cocci | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 scripts/coccinelle/round.cocci

Comments

Eric Blake May 31, 2016, 5:12 p.m. UTC | #1
On 05/31/2016 10:35 AM, Laurent Vivier wrote:
> sample from http://coccinellery.org/
> 
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> ---
>  scripts/coccinelle/round.cocci | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
>  create mode 100644 scripts/coccinelle/round.cocci
> 
> diff --git a/scripts/coccinelle/round.cocci b/scripts/coccinelle/round.cocci
> new file mode 100644
> index 0000000..ed06773
> --- /dev/null
> +++ b/scripts/coccinelle/round.cocci
> @@ -0,0 +1,19 @@
> +// Use macro DIV_ROUND_UP instead of (((n) + (d) - 1) /(d))
> +@@
> +expression e1;
> +expression e2;
> +@@
> +(
> +- ((e1) + e2 - 1) / (e2)
> ++ DIV_ROUND_UP(e1,e2)

Should this also cover things like:

((e1) & e2) / (e2 + 1)

or with bit-shifts in place of division? I don't know how much
coccinelle can spot other issues (at least until I review the rest of
your series), but at any rate your patch looks like a good first start.
I also like the fact that we are committing the script into the repo, so
that we can reuse it to catch future additions of the open-coded forms.

Reviewed-by: Eric Blake <eblake@redhat.com>
Laurent Vivier May 31, 2016, 5:24 p.m. UTC | #2
On 31/05/2016 19:12, Eric Blake wrote:
> On 05/31/2016 10:35 AM, Laurent Vivier wrote:
>> sample from http://coccinellery.org/
>>
>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>> ---
>>  scripts/coccinelle/round.cocci | 19 +++++++++++++++++++
>>  1 file changed, 19 insertions(+)
>>  create mode 100644 scripts/coccinelle/round.cocci
>>
>> diff --git a/scripts/coccinelle/round.cocci b/scripts/coccinelle/round.cocci
>> new file mode 100644
>> index 0000000..ed06773
>> --- /dev/null
>> +++ b/scripts/coccinelle/round.cocci
>> @@ -0,0 +1,19 @@
>> +// Use macro DIV_ROUND_UP instead of (((n) + (d) - 1) /(d))
>> +@@
>> +expression e1;
>> +expression e2;
>> +@@
>> +(
>> +- ((e1) + e2 - 1) / (e2)
>> ++ DIV_ROUND_UP(e1,e2)
> 
> Should this also cover things like:
> 
> ((e1) & e2) / (e2 + 1)

It's true only if (e2 + 1) is a power of two... and I don't know if we
can check that with coccinelle... perhaps we can filter them manually then.

> or with bit-shifts in place of division? I don't know how much
> coccinelle can spot other issues (at least until I review the rest of
> your series), but at any rate your patch looks like a good first start.
> I also like the fact that we are committing the script into the repo, so
> that we can reuse it to catch future additions of the open-coded forms.
> 
> Reviewed-by: Eric Blake <eblake@redhat.com>
> 

Thanks,
Laurent
diff mbox

Patch

diff --git a/scripts/coccinelle/round.cocci b/scripts/coccinelle/round.cocci
new file mode 100644
index 0000000..ed06773
--- /dev/null
+++ b/scripts/coccinelle/round.cocci
@@ -0,0 +1,19 @@ 
+// Use macro DIV_ROUND_UP instead of (((n) + (d) - 1) /(d))
+@@
+expression e1;
+expression e2;
+@@
+(
+- ((e1) + e2 - 1) / (e2)
++ DIV_ROUND_UP(e1,e2)
+|
+- ((e1) + (e2 - 1)) / (e2)
++ DIV_ROUND_UP(e1,e2)
+)
+
+@@
+expression e1;
+expression e2;
+@@
+-(DIV_ROUND_UP(e1,e2))
++DIV_ROUND_UP(e1,e2)