[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20131102143236.GA18084@leaf>
Date: Sat, 2 Nov 2013 07:32:37 -0700
From: Josh Triplett <josh@...htriplett.org>
To: Julia Lawall <Julia.Lawall@...6.fr>,
Gilles Muller <Gilles.Muller@...6.fr>,
Nicolas Palix <nicolas.palix@...g.fr>,
Michal Marek <mmarek@...e.cz>, linux-kernel@...r.kernel.org,
cocci@...teme.lip6.fr
Subject: [PATCH] coccinelle: Add a script to find and remove unnecessary ifs
with no body
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@...htriplett.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) {}
+)
+
+@...ipt: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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists