boolinit.cocci 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /// Bool initializations should use true and false. Bool tests don't need
  2. /// comparisons. Based on contributions from Joe Perches, Rusty Russell
  3. /// and Bruce W Allan.
  4. ///
  5. // Confidence: High
  6. // Copyright: (C) 2012 Julia Lawall, INRIA/LIP6. GPLv2.
  7. // Copyright: (C) 2012 Gilles Muller, INRIA/LiP6. GPLv2.
  8. // URL: http://coccinelle.lip6.fr/
  9. // Options: --include-headers
  10. virtual patch
  11. virtual context
  12. virtual org
  13. virtual report
  14. @depends on patch@
  15. bool t;
  16. symbol true;
  17. symbol false;
  18. @@
  19. (
  20. - t == true
  21. + t
  22. |
  23. - true == t
  24. + t
  25. |
  26. - t != true
  27. + !t
  28. |
  29. - true != t
  30. + !t
  31. |
  32. - t == false
  33. + !t
  34. |
  35. - false == t
  36. + !t
  37. |
  38. - t != false
  39. + t
  40. |
  41. - false != t
  42. + t
  43. )
  44. @depends on patch disable is_zero, isnt_zero@
  45. bool t;
  46. @@
  47. (
  48. - t == 1
  49. + t
  50. |
  51. - t != 1
  52. + !t
  53. |
  54. - t == 0
  55. + !t
  56. |
  57. - t != 0
  58. + t
  59. )
  60. @depends on patch@
  61. bool b;
  62. @@
  63. (
  64. b =
  65. - 0
  66. + false
  67. |
  68. b =
  69. - 1
  70. + true
  71. )
  72. // ---------------------------------------------------------------------
  73. @r1 depends on !patch@
  74. bool t;
  75. position p;
  76. @@
  77. (
  78. * t@p == true
  79. |
  80. * true == t@p
  81. |
  82. * t@p != true
  83. |
  84. * true != t@p
  85. |
  86. * t@p == false
  87. |
  88. * false == t@p
  89. |
  90. * t@p != false
  91. |
  92. * false != t@p
  93. )
  94. @r2 depends on !patch disable is_zero, isnt_zero@
  95. bool t;
  96. position p;
  97. @@
  98. (
  99. * t@p == 1
  100. |
  101. * t@p != 1
  102. |
  103. * t@p == 0
  104. |
  105. * t@p != 0
  106. )
  107. @r3 depends on !patch@
  108. bool b;
  109. position p1,p2;
  110. constant c;
  111. @@
  112. (
  113. *b@p1 = 0
  114. |
  115. *b@p1 = 1
  116. |
  117. *b@p2 = c
  118. )
  119. @script:python depends on org@
  120. p << r1.p;
  121. @@
  122. cocci.print_main("WARNING: Comparison to bool",p)
  123. @script:python depends on org@
  124. p << r2.p;
  125. @@
  126. cocci.print_main("WARNING: Comparison of bool to 0/1",p)
  127. @script:python depends on org@
  128. p1 << r3.p1;
  129. @@
  130. cocci.print_main("WARNING: Assignment of bool to 0/1",p1)
  131. @script:python depends on org@
  132. p2 << r3.p2;
  133. @@
  134. cocci.print_main("ERROR: Assignment of bool to non-0/1 constant",p2)
  135. @script:python depends on report@
  136. p << r1.p;
  137. @@
  138. coccilib.report.print_report(p[0],"WARNING: Comparison to bool")
  139. @script:python depends on report@
  140. p << r2.p;
  141. @@
  142. coccilib.report.print_report(p[0],"WARNING: Comparison of bool to 0/1")
  143. @script:python depends on report@
  144. p1 << r3.p1;
  145. @@
  146. coccilib.report.print_report(p1[0],"WARNING: Assignment of bool to 0/1")
  147. @script:python depends on report@
  148. p2 << r3.p2;
  149. @@
  150. coccilib.report.print_report(p2[0],"ERROR: Assignment of bool to non-0/1 constant")