1/// Simplify a trivial if-return sequence. Possibly combine with a 2/// preceding function call. 3// 4// Confidence: High 5// Copyright: (C) 2014 Julia Lawall, INRIA/LIP6. GPLv2. 6// Copyright: (C) 2014 Gilles Muller, INRIA/LiP6. GPLv2. 7// URL: http://coccinelle.lip6.fr/ 8// Comments: 9// Options: --no-includes --include-headers 10 11virtual patch 12virtual context 13virtual org 14virtual report 15 16@r depends on patch@ 17local idexpression e; 18identifier i,f,fn; 19@@ 20 21fn(...) { <... 22- e@i = 23+ return 24 f(...); 25-if (i != 0) return i; 26-return 0; 27...> } 28 29@depends on patch@ 30identifier r.i; 31type t; 32@@ 33 34-t i; 35 ... when != i 36 37@depends on patch@ 38expression e; 39@@ 40 41-if (e != 0) 42 return e; 43-return 0; 44 45// ----------------------------------------------------------------------- 46 47@s1 depends on context || org || report@ 48local idexpression e; 49identifier i,f,fn; 50position p,p1,p2; 51@@ 52 53fn(...) { <... 54* e@i@p = f(...); 55 if (\(i@p1 != 0\|i@p2 < 0\)) 56 return i; 57 return 0; 58...> } 59 60@s2 depends on context || org || report forall@ 61identifier s1.i; 62type t; 63position q,s1.p; 64expression e,f; 65@@ 66 67* t i@q; 68 ... when != i 69 e@p = f(...); 70 71@s3 depends on context || org || report@ 72expression e; 73position p1!=s1.p1; 74position p2!=s1.p2; 75@@ 76 77*if (\(e@p1 != 0\|e@p2 < 0\)) 78 return e; 79 return 0; 80 81// ----------------------------------------------------------------------- 82 83@script:python depends on org@ 84p << s1.p; 85p1 << s1.p1; 86q << s2.q; 87@@ 88 89cocci.print_main("decl",q) 90cocci.print_secs("use",p) 91cocci.include_match(False) 92 93@script:python depends on org@ 94p << s1.p; 95p2 << s1.p2; 96q << s2.q; 97@@ 98 99cocci.print_main("decl",q) 100cocci.print_secs("use with questionable test",p) 101cocci.include_match(False) 102 103@script:python depends on org@ 104p << s1.p; 105p1 << s1.p1; 106@@ 107 108cocci.print_main("use",p) 109 110@script:python depends on org@ 111p << s1.p; 112p2 << s1.p2; 113@@ 114 115cocci.print_main("use with questionable test",p) 116 117@script:python depends on org@ 118p << s3.p1; 119@@ 120 121cocci.print_main("test",p) 122 123@script:python depends on org@ 124p << s3.p2; 125@@ 126 127cocci.print_main("questionable test",p) 128 129// ----------------------------------------------------------------------- 130 131@script:python depends on report@ 132p << s1.p; 133p1 << s1.p1; 134q << s2.q; 135@@ 136 137msg = "WARNING: end returns can be simpified and declaration on line %s can be dropped" % (q[0].line) 138coccilib.report.print_report(p[0],msg) 139cocci.include_match(False) 140 141@script:python depends on report@ 142p << s1.p; 143p1 << s1.p1; 144q << s2.q 145; 146@@ 147 148msg = "WARNING: end returns may be simpified if negative or 0 value and declaration on line %s can be dropped" % (q[0].line) 149coccilib.report.print_report(p[0],msg) 150cocci.include_match(False) 151 152@script:python depends on report@ 153p << s1.p; 154p1 << s1.p1; 155@@ 156 157msg = "WARNING: end returns can be simpified" 158coccilib.report.print_report(p[0],msg) 159 160@script:python depends on report@ 161p << s1.p; 162p2 << s1.p2; 163@@ 164 165msg = "WARNING: end returns can be simpified if negative or 0 value" 166coccilib.report.print_report(p[0],msg) 167 168@script:python depends on report@ 169p << s3.p1; 170@@ 171 172msg = "WARNING: end returns can be simpified" 173coccilib.report.print_report(p[0],msg) 174 175@script:python depends on report@ 176p << s3.p2; 177@@ 178 179msg = "WARNING: end returns can be simpified if tested value is negative or 0" 180coccilib.report.print_report(p[0],msg) 181