compare_const_fl.cocci 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /// Move constants to the right of binary operators.
  2. //# Depends on personal taste in some cases.
  3. ///
  4. // Confidence: Moderate
  5. // Copyright: (C) 2015 Copyright: (C) 2015 Julia Lawall, Inria. GPLv2.
  6. // URL: http://coccinelle.lip6.fr/
  7. // Options: --no-includes --include-headers
  8. virtual patch
  9. virtual context
  10. virtual org
  11. virtual report
  12. @r1 depends on patch && !context && !org && !report
  13. disable bitor_comm, neg_if_exp@
  14. constant c,c1;
  15. local idexpression i;
  16. expression e,e1,e2;
  17. binary operator b = {==,!=,&,|};
  18. type t;
  19. @@
  20. (
  21. c b (c1)
  22. |
  23. sizeof(t) b e1
  24. |
  25. sizeof e b e1
  26. |
  27. i b e1
  28. |
  29. c | e1 | e2 | ...
  30. |
  31. c | (e ? e1 : e2)
  32. |
  33. - c
  34. + e
  35. b
  36. - e
  37. + c
  38. )
  39. @r2 depends on patch && !context && !org && !report
  40. disable gtr_lss, gtr_lss_eq, not_int2@
  41. constant c,c1;
  42. expression e,e1,e2;
  43. binary operator b;
  44. binary operator b1 = {<,<=},b2 = {<,<=};
  45. binary operator b3 = {>,>=},b4 = {>,>=};
  46. local idexpression i;
  47. type t;
  48. @@
  49. (
  50. c b c1
  51. |
  52. sizeof(t) b e1
  53. |
  54. sizeof e b e1
  55. |
  56. (e1 b1 e) && (e b2 e2)
  57. |
  58. (e1 b3 e) && (e b4 e2)
  59. |
  60. i b e
  61. |
  62. - c < e
  63. + e > c
  64. |
  65. - c <= e
  66. + e >= c
  67. |
  68. - c > e
  69. + e < c
  70. |
  71. - c >= e
  72. + e <= c
  73. )
  74. // ----------------------------------------------------------------------------
  75. @r1_context depends on !patch && (context || org || report)
  76. disable bitor_comm, neg_if_exp exists@
  77. type t;
  78. binary operator b = {==,!=,&,|};
  79. constant c, c1;
  80. expression e, e1, e2;
  81. local idexpression i;
  82. position j0;
  83. @@
  84. (
  85. c b (c1)
  86. |
  87. sizeof(t) b e1
  88. |
  89. sizeof e b e1
  90. |
  91. i b e1
  92. |
  93. c | e1 | e2 | ...
  94. |
  95. c | (e ? e1 : e2)
  96. |
  97. * c@j0 b e
  98. )
  99. @r2_context depends on !patch && (context || org || report)
  100. disable gtr_lss, gtr_lss_eq, not_int2 exists@
  101. type t;
  102. binary operator b, b1 = {<,<=}, b2 = {<,<=}, b3 = {>,>=}, b4 = {>,>=};
  103. constant c, c1;
  104. expression e, e1, e2;
  105. local idexpression i;
  106. position j0;
  107. @@
  108. (
  109. c b c1
  110. |
  111. sizeof(t) b e1
  112. |
  113. sizeof e b e1
  114. |
  115. (e1 b1 e) && (e b2 e2)
  116. |
  117. (e1 b3 e) && (e b4 e2)
  118. |
  119. i b e
  120. |
  121. * c@j0 < e
  122. |
  123. * c@j0 <= e
  124. |
  125. * c@j0 > e
  126. |
  127. * c@j0 >= e
  128. )
  129. // ----------------------------------------------------------------------------
  130. @script:python r1_org depends on org@
  131. j0 << r1_context.j0;
  132. @@
  133. msg = "Move constant to right."
  134. coccilib.org.print_todo(j0[0], msg)
  135. @script:python r2_org depends on org@
  136. j0 << r2_context.j0;
  137. @@
  138. msg = "Move constant to right."
  139. coccilib.org.print_todo(j0[0], msg)
  140. // ----------------------------------------------------------------------------
  141. @script:python r1_report depends on report@
  142. j0 << r1_context.j0;
  143. @@
  144. msg = "Move constant to right."
  145. coccilib.report.print_report(j0[0], msg)
  146. @script:python r2_report depends on report@
  147. j0 << r2_context.j0;
  148. @@
  149. msg = "Move constant to right."
  150. coccilib.report.print_report(j0[0], msg)