All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] coccinelle: Add a script to find and remove unnecessary ifs with no body
@ 2013-11-02 14:32 ` Josh Triplett
  0 siblings, 0 replies; 14+ messages in thread
From: Josh Triplett @ 2013-11-02 14:32 UTC (permalink / raw)
  To: Julia Lawall, Gilles Muller, Nicolas Palix, Michal Marek,
	linux-kernel, cocci

This script matches code like:

if (foo) {}

and either eliminates it (if foo has no side effects) or replaces it
with foo (if foo has side effects).

Works perfectly in report mode; the results of patch mode are
semantically correct but may still benefit from further simplification.

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
---
 scripts/coccinelle/misc/unnecessary-if.cocci | 40 ++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 scripts/coccinelle/misc/unnecessary-if.cocci

diff --git a/scripts/coccinelle/misc/unnecessary-if.cocci b/scripts/coccinelle/misc/unnecessary-if.cocci
new file mode 100644
index 0000000..7ed721f
--- /dev/null
+++ b/scripts/coccinelle/misc/unnecessary-if.cocci
@@ -0,0 +1,40 @@
+/// Remove unnecessary ifs with no statements.
+//
+// Confidence: High
+// Options: --no-includes --include-headers
+
+virtual patch
+virtual report
+virtual context
+
+@r1 depends on patch@
+expression E, E2, f;
+@@
+(
+- if (
+      <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+>
+- ) {}
++ ;
+|
+- if (E) {}
+)
+
+@r2 depends on report || context@
+expression E, E2, f;
+position p;
+@@
+(
+- if@p (
+      <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+>
+- ) {}
++ ;
+|
+- if@p (E) {}
+)
+
+@script:python depends on report@
+p << r2.p;
+@@
+
+msg = "WARNING: Unnecessary if with no body."
+coccilib.report.print_report(p[0], msg)
-- 
1.8.4.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [Cocci] [PATCH] coccinelle: Add a script to find and remove unnecessary ifs with no body
@ 2013-11-02 14:32 ` Josh Triplett
  0 siblings, 0 replies; 14+ messages in thread
From: Josh Triplett @ 2013-11-02 14:32 UTC (permalink / raw)
  To: cocci

This script matches code like:

if (foo) {}

and either eliminates it (if foo has no side effects) or replaces it
with foo (if foo has side effects).

Works perfectly in report mode; the results of patch mode are
semantically correct but may still benefit from further simplification.

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
---
 scripts/coccinelle/misc/unnecessary-if.cocci | 40 ++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 scripts/coccinelle/misc/unnecessary-if.cocci

diff --git a/scripts/coccinelle/misc/unnecessary-if.cocci b/scripts/coccinelle/misc/unnecessary-if.cocci
new file mode 100644
index 0000000..7ed721f
--- /dev/null
+++ b/scripts/coccinelle/misc/unnecessary-if.cocci
@@ -0,0 +1,40 @@
+/// Remove unnecessary ifs with no statements.
+//
+// Confidence: High
+// Options: --no-includes --include-headers
+
+virtual patch
+virtual report
+virtual context
+
+ at r1 depends on patch@
+expression E, E2, f;
+@@
+(
+- if (
+      <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+>
+- ) {}
++ ;
+|
+- if (E) {}
+)
+
+ at r2 depends on report || context@
+expression E, E2, f;
+position p;
+@@
+(
+- if at p (
+      <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+>
+- ) {}
++ ;
+|
+- if at p (E) {}
+)
+
+ at script:python depends on report@
+p << r2.p;
+@@
+
+msg = "WARNING: Unnecessary if with no body."
+coccilib.report.print_report(p[0], msg)
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH] coccinelle: Add a script to find and remove unnecessary ifs with no body
  2013-11-02 14:32 ` [Cocci] " Josh Triplett
@ 2013-11-02 14:51   ` Julia Lawall
  -1 siblings, 0 replies; 14+ messages in thread
From: Julia Lawall @ 2013-11-02 14:51 UTC (permalink / raw)
  To: Josh Triplett
  Cc: Gilles Muller, Nicolas Palix, Michal Marek, linux-kernel, cocci

On Sat, 2 Nov 2013, Josh Triplett wrote:

> This script matches code like:
>
> if (foo) {}
>
> and either eliminates it (if foo has no side effects) or replaces it
> with foo (if foo has side effects).
>
> Works perfectly in report mode; the results of patch mode are
> semantically correct but may still benefit from further simplification.

Actually, I would get rid of the patch mode.  Looking through some of the
results, I don't see any where just deleting the code is the right thing
to do.  They all look like cases where the developer wanted to remember to
think more about the problem.

julia

>
> Signed-off-by: Josh Triplett <josh@joshtriplett.org>
> ---
>  scripts/coccinelle/misc/unnecessary-if.cocci | 40 ++++++++++++++++++++++++++++
>  1 file changed, 40 insertions(+)
>  create mode 100644 scripts/coccinelle/misc/unnecessary-if.cocci
>
> diff --git a/scripts/coccinelle/misc/unnecessary-if.cocci b/scripts/coccinelle/misc/unnecessary-if.cocci
> new file mode 100644
> index 0000000..7ed721f
> --- /dev/null
> +++ b/scripts/coccinelle/misc/unnecessary-if.cocci
> @@ -0,0 +1,40 @@
> +/// Remove unnecessary ifs with no statements.
> +//
> +// Confidence: High
> +// Options: --no-includes --include-headers
> +
> +virtual patch
> +virtual report
> +virtual context
> +
> +@r1 depends on patch@
> +expression E, E2, f;
> +@@
> +(
> +- if (
> +      <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+>
> +- ) {}
> ++ ;
> +|
> +- if (E) {}
> +)
> +
> +@r2 depends on report || context@
> +expression E, E2, f;
> +position p;
> +@@
> +(
> +- if@p (
> +      <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+>
> +- ) {}
> ++ ;
> +|
> +- if@p (E) {}
> +)
> +
> +@script:python depends on report@
> +p << r2.p;
> +@@
> +
> +msg = "WARNING: Unnecessary if with no body."
> +coccilib.report.print_report(p[0], msg)
> --
> 1.8.4.2
>
>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Cocci] [PATCH] coccinelle: Add a script to find and remove unnecessary ifs with no body
@ 2013-11-02 14:51   ` Julia Lawall
  0 siblings, 0 replies; 14+ messages in thread
From: Julia Lawall @ 2013-11-02 14:51 UTC (permalink / raw)
  To: cocci

On Sat, 2 Nov 2013, Josh Triplett wrote:

> This script matches code like:
>
> if (foo) {}
>
> and either eliminates it (if foo has no side effects) or replaces it
> with foo (if foo has side effects).
>
> Works perfectly in report mode; the results of patch mode are
> semantically correct but may still benefit from further simplification.

Actually, I would get rid of the patch mode.  Looking through some of the
results, I don't see any where just deleting the code is the right thing
to do.  They all look like cases where the developer wanted to remember to
think more about the problem.

julia

>
> Signed-off-by: Josh Triplett <josh@joshtriplett.org>
> ---
>  scripts/coccinelle/misc/unnecessary-if.cocci | 40 ++++++++++++++++++++++++++++
>  1 file changed, 40 insertions(+)
>  create mode 100644 scripts/coccinelle/misc/unnecessary-if.cocci
>
> diff --git a/scripts/coccinelle/misc/unnecessary-if.cocci b/scripts/coccinelle/misc/unnecessary-if.cocci
> new file mode 100644
> index 0000000..7ed721f
> --- /dev/null
> +++ b/scripts/coccinelle/misc/unnecessary-if.cocci
> @@ -0,0 +1,40 @@
> +/// Remove unnecessary ifs with no statements.
> +//
> +// Confidence: High
> +// Options: --no-includes --include-headers
> +
> +virtual patch
> +virtual report
> +virtual context
> +
> + at r1 depends on patch@
> +expression E, E2, f;
> +@@
> +(
> +- if (
> +      <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+>
> +- ) {}
> ++ ;
> +|
> +- if (E) {}
> +)
> +
> + at r2 depends on report || context@
> +expression E, E2, f;
> +position p;
> +@@
> +(
> +- if at p (
> +      <+... \(f(...)\|++E\|--E\|E++\|E--\|E=E2\) ...+>
> +- ) {}
> ++ ;
> +|
> +- if at p (E) {}
> +)
> +
> + at script:python depends on report@
> +p << r2.p;
> +@@
> +
> +msg = "WARNING: Unnecessary if with no body."
> +coccilib.report.print_report(p[0], msg)
> --
> 1.8.4.2
>
>

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] coccinelle: Add a script to find and remove unnecessary ifs with no body
  2013-11-02 14:51   ` [Cocci] " Julia Lawall
@ 2013-11-02 15:05     ` Josh Triplett
  -1 siblings, 0 replies; 14+ messages in thread
From: Josh Triplett @ 2013-11-02 15:05 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Gilles Muller, Nicolas Palix, Michal Marek, linux-kernel, cocci

On Sat, Nov 02, 2013 at 03:51:50PM +0100, Julia Lawall wrote:
> On Sat, 2 Nov 2013, Josh Triplett wrote:
> 
> > This script matches code like:
> >
> > if (foo) {}
> >
> > and either eliminates it (if foo has no side effects) or replaces it
> > with foo (if foo has side effects).
> >
> > Works perfectly in report mode; the results of patch mode are
> > semantically correct but may still benefit from further simplification.
> 
> Actually, I would get rid of the patch mode.  Looking through some of the
> results, I don't see any where just deleting the code is the right thing
> to do.  They all look like cases where the developer wanted to remember to
> think more about the problem.

I observed several where the patch seemed entirely correct, with no
FIXME/TODO/XXX comments associated with the conditional.

Nonetheless, I don't mind removing the patch mode if you don't think
it's worthwhile.  I'll send a v2.

- Josh Triplett

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Cocci] [PATCH] coccinelle: Add a script to find and remove unnecessary ifs with no body
@ 2013-11-02 15:05     ` Josh Triplett
  0 siblings, 0 replies; 14+ messages in thread
From: Josh Triplett @ 2013-11-02 15:05 UTC (permalink / raw)
  To: cocci

On Sat, Nov 02, 2013 at 03:51:50PM +0100, Julia Lawall wrote:
> On Sat, 2 Nov 2013, Josh Triplett wrote:
> 
> > This script matches code like:
> >
> > if (foo) {}
> >
> > and either eliminates it (if foo has no side effects) or replaces it
> > with foo (if foo has side effects).
> >
> > Works perfectly in report mode; the results of patch mode are
> > semantically correct but may still benefit from further simplification.
> 
> Actually, I would get rid of the patch mode.  Looking through some of the
> results, I don't see any where just deleting the code is the right thing
> to do.  They all look like cases where the developer wanted to remember to
> think more about the problem.

I observed several where the patch seemed entirely correct, with no
FIXME/TODO/XXX comments associated with the conditional.

Nonetheless, I don't mind removing the patch mode if you don't think
it's worthwhile.  I'll send a v2.

- Josh Triplett

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH] coccinelle: Add a script to find and remove unnecessary ifs with no body
  2013-11-02 15:05     ` [Cocci] " Josh Triplett
@ 2013-11-02 15:09       ` Julia Lawall
  -1 siblings, 0 replies; 14+ messages in thread
From: Julia Lawall @ 2013-11-02 15:09 UTC (permalink / raw)
  To: Josh Triplett
  Cc: Gilles Muller, Nicolas Palix, Michal Marek, linux-kernel, cocci

> I observed several where the patch seemed entirely correct, with no
> FIXME/TODO/XXX comments associated with the conditional.
>
> Nonetheless, I don't mind removing the patch mode if you don't think
> it's worthwhile.  I'll send a v2.

I think it would be better without it.

thanks,
julia

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Cocci] [PATCH] coccinelle: Add a script to find and remove unnecessary ifs with no body
@ 2013-11-02 15:09       ` Julia Lawall
  0 siblings, 0 replies; 14+ messages in thread
From: Julia Lawall @ 2013-11-02 15:09 UTC (permalink / raw)
  To: cocci

> I observed several where the patch seemed entirely correct, with no
> FIXME/TODO/XXX comments associated with the conditional.
>
> Nonetheless, I don't mind removing the patch mode if you don't think
> it's worthwhile.  I'll send a v2.

I think it would be better without it.

thanks,
julia

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCHv2] coccinelle: Add a script to find unnecessary ifs with no body
  2013-11-02 14:51   ` [Cocci] " Julia Lawall
@ 2013-11-02 15:15     ` Josh Triplett
  -1 siblings, 0 replies; 14+ messages in thread
From: Josh Triplett @ 2013-11-02 15:15 UTC (permalink / raw)
  To: Julia Lawall
  Cc: Gilles Muller, Nicolas Palix, Michal Marek, linux-kernel, cocci

This script finds code like this, with an unnecessary if:

if (foo) {}

Provides report mode only, since patching often eliminates conditionals
that contain TODO comments or similar.

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
---

v2: Drop patch mode.

 scripts/coccinelle/misc/unnecessary-if.cocci | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
 create mode 100644 scripts/coccinelle/misc/unnecessary-if.cocci

diff --git a/scripts/coccinelle/misc/unnecessary-if.cocci b/scripts/coccinelle/misc/unnecessary-if.cocci
new file mode 100644
index 0000000..28ac895
--- /dev/null
+++ b/scripts/coccinelle/misc/unnecessary-if.cocci
@@ -0,0 +1,23 @@
+/// Find unnecessary ifs with no statements.
+//
+// Report mode only, since patch mode often deletes ifs that contain TODO
+// comments.
+//
+// Confidence: High
+// Options: --no-includes --include-headers
+
+virtual report
+virtual context
+
+@r depends on report || context@
+expression E;
+position p;
+@@
+if@p (E) {}
+
+@script:python depends on report@
+p << r.p;
+@@
+
+msg = "WARNING: Unnecessary if with no body."
+coccilib.report.print_report(p[0], msg)
-- 
1.8.4.2


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [Cocci] [PATCHv2] coccinelle: Add a script to find unnecessary ifs with no body
@ 2013-11-02 15:15     ` Josh Triplett
  0 siblings, 0 replies; 14+ messages in thread
From: Josh Triplett @ 2013-11-02 15:15 UTC (permalink / raw)
  To: cocci

This script finds code like this, with an unnecessary if:

if (foo) {}

Provides report mode only, since patching often eliminates conditionals
that contain TODO comments or similar.

Signed-off-by: Josh Triplett <josh@joshtriplett.org>
---

v2: Drop patch mode.

 scripts/coccinelle/misc/unnecessary-if.cocci | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
 create mode 100644 scripts/coccinelle/misc/unnecessary-if.cocci

diff --git a/scripts/coccinelle/misc/unnecessary-if.cocci b/scripts/coccinelle/misc/unnecessary-if.cocci
new file mode 100644
index 0000000..28ac895
--- /dev/null
+++ b/scripts/coccinelle/misc/unnecessary-if.cocci
@@ -0,0 +1,23 @@
+/// Find unnecessary ifs with no statements.
+//
+// Report mode only, since patch mode often deletes ifs that contain TODO
+// comments.
+//
+// Confidence: High
+// Options: --no-includes --include-headers
+
+virtual report
+virtual context
+
+ at r depends on report || context@
+expression E;
+position p;
+@@
+if at p (E) {}
+
+ at script:python depends on report@
+p << r.p;
+@@
+
+msg = "WARNING: Unnecessary if with no body."
+coccilib.report.print_report(p[0], msg)
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCHv2] coccinelle: Add a script to find unnecessary ifs with no body
  2013-11-02 15:15     ` [Cocci] " Josh Triplett
@ 2013-11-02 16:49       ` Julia Lawall
  -1 siblings, 0 replies; 14+ messages in thread
From: Julia Lawall @ 2013-11-02 16:49 UTC (permalink / raw)
  To: Josh Triplett
  Cc: Gilles Muller, Nicolas Palix, Michal Marek, linux-kernel, cocci

> +@r depends on report || context@
> +expression E;
> +position p;
> +@@
> +if@p (E) {}

There should be a * in front of the if, to support context mode.

julia

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Cocci] [PATCHv2] coccinelle: Add a script to find unnecessary ifs with no body
@ 2013-11-02 16:49       ` Julia Lawall
  0 siblings, 0 replies; 14+ messages in thread
From: Julia Lawall @ 2013-11-02 16:49 UTC (permalink / raw)
  To: cocci

> + at r depends on report || context@
> +expression E;
> +position p;
> +@@
> +if at p (E) {}

There should be a * in front of the if, to support context mode.

julia

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCHv2] coccinelle: Add a script to find unnecessary ifs with no body
  2013-11-02 16:49       ` [Cocci] " Julia Lawall
@ 2013-11-08 13:52         ` Michal Marek
  -1 siblings, 0 replies; 14+ messages in thread
From: Michal Marek @ 2013-11-08 13:52 UTC (permalink / raw)
  To: Josh Triplett
  Cc: Julia Lawall, Gilles Muller, Nicolas Palix, linux-kernel, cocci

On 2.11.2013 17:49, Julia Lawall wrote:
>> +@r depends on report || context@
>> +expression E;
>> +position p;
>> +@@
>> +if@p (E) {}
> 
> There should be a * in front of the if, to support context mode.

Josh, will you send a v3 with the above fix?

Thanks,
Michal


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Cocci] [PATCHv2] coccinelle: Add a script to find unnecessary ifs with no body
@ 2013-11-08 13:52         ` Michal Marek
  0 siblings, 0 replies; 14+ messages in thread
From: Michal Marek @ 2013-11-08 13:52 UTC (permalink / raw)
  To: cocci

On 2.11.2013 17:49, Julia Lawall wrote:
>> + at r depends on report || context@
>> +expression E;
>> +position p;
>> +@@
>> +if at p (E) {}
> 
> There should be a * in front of the if, to support context mode.

Josh, will you send a v3 with the above fix?

Thanks,
Michal

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2013-11-08 13:52 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-02 14:32 [PATCH] coccinelle: Add a script to find and remove unnecessary ifs with no body Josh Triplett
2013-11-02 14:32 ` [Cocci] " Josh Triplett
2013-11-02 14:51 ` Julia Lawall
2013-11-02 14:51   ` [Cocci] " Julia Lawall
2013-11-02 15:05   ` Josh Triplett
2013-11-02 15:05     ` [Cocci] " Josh Triplett
2013-11-02 15:09     ` Julia Lawall
2013-11-02 15:09       ` [Cocci] " Julia Lawall
2013-11-02 15:15   ` [PATCHv2] coccinelle: Add a script to find " Josh Triplett
2013-11-02 15:15     ` [Cocci] " Josh Triplett
2013-11-02 16:49     ` Julia Lawall
2013-11-02 16:49       ` [Cocci] " Julia Lawall
2013-11-08 13:52       ` Michal Marek
2013-11-08 13:52         ` [Cocci] " Michal Marek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.