simple_return.cocci 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. virtual patch
  11. virtual context
  12. virtual org
  13. virtual report
  14. @r depends on patch@
  15. local idexpression e;
  16. identifier i,f,fn;
  17. @@
  18. fn(...) { <...
  19. - e@i =
  20. + return
  21. f(...);
  22. -if (i != 0) return i;
  23. -return 0;
  24. ...> }
  25. @depends on patch@
  26. identifier r.i;
  27. type t;
  28. @@
  29. -t i;
  30. ... when != i
  31. @depends on patch@
  32. expression e;
  33. @@
  34. -if (e != 0)
  35. return e;
  36. -return 0;
  37. // -----------------------------------------------------------------------
  38. @s1 depends on context || org || report@
  39. local idexpression e;
  40. identifier i,f,fn;
  41. position p,p1,p2;
  42. @@
  43. fn(...) { <...
  44. * e@i@p = f(...);
  45. if (\(i@p1 != 0\|i@p2 < 0\))
  46. return i;
  47. return 0;
  48. ...> }
  49. @s2 depends on context || org || report forall@
  50. identifier s1.i;
  51. type t;
  52. position q,s1.p;
  53. expression e,f;
  54. @@
  55. * t i@q;
  56. ... when != i
  57. e@p = f(...);
  58. @s3 depends on context || org || report@
  59. expression e;
  60. position p1!=s1.p1;
  61. position p2!=s1.p2;
  62. @@
  63. *if (\(e@p1 != 0\|e@p2 < 0\))
  64. return e;
  65. return 0;
  66. // -----------------------------------------------------------------------
  67. @script:python depends on org@
  68. p << s1.p;
  69. p1 << s1.p1;
  70. q << s2.q;
  71. @@
  72. cocci.print_main("decl",q)
  73. cocci.print_secs("use",p)
  74. cocci.include_match(False)
  75. @script:python depends on org@
  76. p << s1.p;
  77. p2 << s1.p2;
  78. q << s2.q;
  79. @@
  80. cocci.print_main("decl",q)
  81. cocci.print_secs("use with questionable test",p)
  82. cocci.include_match(False)
  83. @script:python depends on org@
  84. p << s1.p;
  85. p1 << s1.p1;
  86. @@
  87. cocci.print_main("use",p)
  88. @script:python depends on org@
  89. p << s1.p;
  90. p2 << s1.p2;
  91. @@
  92. cocci.print_main("use with questionable test",p)
  93. @script:python depends on org@
  94. p << s3.p1;
  95. @@
  96. cocci.print_main("test",p)
  97. @script:python depends on org@
  98. p << s3.p2;
  99. @@
  100. cocci.print_main("questionable test",p)
  101. // -----------------------------------------------------------------------
  102. @script:python depends on report@
  103. p << s1.p;
  104. p1 << s1.p1;
  105. q << s2.q;
  106. @@
  107. msg = "WARNING: end returns can be simpified and declaration on line %s can be dropped" % (q[0].line)
  108. coccilib.report.print_report(p[0],msg)
  109. cocci.include_match(False)
  110. @script:python depends on report@
  111. p << s1.p;
  112. p1 << s1.p1;
  113. q << s2.q
  114. ;
  115. @@
  116. msg = "WARNING: end returns may be simpified if negative or 0 value and declaration on line %s can be dropped" % (q[0].line)
  117. coccilib.report.print_report(p[0],msg)
  118. cocci.include_match(False)
  119. @script:python depends on report@
  120. p << s1.p;
  121. p1 << s1.p1;
  122. @@
  123. msg = "WARNING: end returns can be simpified"
  124. coccilib.report.print_report(p[0],msg)
  125. @script:python depends on report@
  126. p << s1.p;
  127. p2 << s1.p2;
  128. @@
  129. msg = "WARNING: end returns can be simpified if negative or 0 value"
  130. coccilib.report.print_report(p[0],msg)
  131. @script:python depends on report@
  132. p << s3.p1;
  133. @@
  134. msg = "WARNING: end returns can be simpified"
  135. coccilib.report.print_report(p[0],msg)
  136. @script:python depends on report@
  137. p << s3.p2;
  138. @@
  139. msg = "WARNING: end returns can be simpified if tested value is negative or 0"
  140. coccilib.report.print_report(p[0],msg)