setup_timer.cocci 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /// Use setup_timer function instead of initializing timer with the function
  2. /// and data fields
  3. // Confidence: High
  4. // Copyright: (C) 2016 Vaishali Thakkar, Oracle. GPLv2
  5. // Options: --no-includes --include-headers
  6. // Keywords: init_timer, setup_timer
  7. virtual patch
  8. virtual context
  9. virtual org
  10. virtual report
  11. @match_immediate_function_data_after_init_timer
  12. depends on patch && !context && !org && !report@
  13. expression e, func, da;
  14. @@
  15. -init_timer (&e);
  16. +setup_timer (&e, func, da);
  17. (
  18. -e.function = func;
  19. -e.data = da;
  20. |
  21. -e.data = da;
  22. -e.function = func;
  23. )
  24. @match_function_and_data_after_init_timer
  25. depends on patch && !context && !org && !report@
  26. expression e1, e2, e3, e4, e5, a, b;
  27. @@
  28. -init_timer (&e1);
  29. +setup_timer (&e1, a, b);
  30. ... when != a = e2
  31. when != b = e3
  32. (
  33. -e1.function = a;
  34. ... when != b = e4
  35. -e1.data = b;
  36. |
  37. -e1.data = b;
  38. ... when != a = e5
  39. -e1.function = a;
  40. )
  41. @r1 exists@
  42. identifier f;
  43. position p;
  44. @@
  45. f(...) { ... when any
  46. init_timer@p(...)
  47. ... when any
  48. }
  49. @r2 exists@
  50. identifier g != r1.f;
  51. struct timer_list t;
  52. expression e8;
  53. @@
  54. g(...) { ... when any
  55. t.data = e8
  56. ... when any
  57. }
  58. // It is dangerous to use setup_timer if data field is initialized
  59. // in another function.
  60. @script:python depends on r2@
  61. p << r1.p;
  62. @@
  63. cocci.include_match(False)
  64. @r3 depends on patch && !context && !org && !report@
  65. expression e6, e7, c;
  66. position r1.p;
  67. @@
  68. -init_timer@p (&e6);
  69. +setup_timer (&e6, c, 0UL);
  70. ... when != c = e7
  71. -e6.function = c;
  72. // ----------------------------------------------------------------------------
  73. @match_immediate_function_data_after_init_timer_context
  74. depends on !patch && (context || org || report)@
  75. expression da, e, func;
  76. position j0, j1, j2;
  77. @@
  78. * init_timer@j0 (&e);
  79. (
  80. * e@j1.function = func;
  81. * e@j2.data = da;
  82. |
  83. * e@j1.data = da;
  84. * e@j2.function = func;
  85. )
  86. @match_function_and_data_after_init_timer_context
  87. depends on !patch &&
  88. !match_immediate_function_data_after_init_timer_context &&
  89. (context || org || report)@
  90. expression a, b, e1, e2, e3, e4, e5;
  91. position j0, j1, j2;
  92. @@
  93. * init_timer@j0 (&e1);
  94. ... when != a = e2
  95. when != b = e3
  96. (
  97. * e1@j1.function = a;
  98. ... when != b = e4
  99. * e1@j2.data = b;
  100. |
  101. * e1@j1.data = b;
  102. ... when != a = e5
  103. * e1@j2.function = a;
  104. )
  105. @r3_context depends on !patch &&
  106. !match_immediate_function_data_after_init_timer_context &&
  107. !match_function_and_data_after_init_timer_context &&
  108. (context || org || report)@
  109. expression c, e6, e7;
  110. position r1.p;
  111. position j0, j1;
  112. @@
  113. * init_timer@j0@p (&e6);
  114. ... when != c = e7
  115. * e6@j1.function = c;
  116. // ----------------------------------------------------------------------------
  117. @script:python match_immediate_function_data_after_init_timer_org
  118. depends on org@
  119. j0 << match_immediate_function_data_after_init_timer_context.j0;
  120. j1 << match_immediate_function_data_after_init_timer_context.j1;
  121. j2 << match_immediate_function_data_after_init_timer_context.j2;
  122. @@
  123. msg = "Use setup_timer function."
  124. coccilib.org.print_todo(j0[0], msg)
  125. coccilib.org.print_link(j1[0], "")
  126. coccilib.org.print_link(j2[0], "")
  127. @script:python match_function_and_data_after_init_timer_org depends on org@
  128. j0 << match_function_and_data_after_init_timer_context.j0;
  129. j1 << match_function_and_data_after_init_timer_context.j1;
  130. j2 << match_function_and_data_after_init_timer_context.j2;
  131. @@
  132. msg = "Use setup_timer function."
  133. coccilib.org.print_todo(j0[0], msg)
  134. coccilib.org.print_link(j1[0], "")
  135. coccilib.org.print_link(j2[0], "")
  136. @script:python r3_org depends on org@
  137. j0 << r3_context.j0;
  138. j1 << r3_context.j1;
  139. @@
  140. msg = "Use setup_timer function."
  141. coccilib.org.print_todo(j0[0], msg)
  142. coccilib.org.print_link(j1[0], "")
  143. // ----------------------------------------------------------------------------
  144. @script:python match_immediate_function_data_after_init_timer_report
  145. depends on report@
  146. j0 << match_immediate_function_data_after_init_timer_context.j0;
  147. j1 << match_immediate_function_data_after_init_timer_context.j1;
  148. @@
  149. msg = "Use setup_timer function for function on line %s." % (j1[0].line)
  150. coccilib.report.print_report(j0[0], msg)
  151. @script:python match_function_and_data_after_init_timer_report depends on report@
  152. j0 << match_function_and_data_after_init_timer_context.j0;
  153. j1 << match_function_and_data_after_init_timer_context.j1;
  154. @@
  155. msg = "Use setup_timer function for function on line %s." % (j1[0].line)
  156. coccilib.report.print_report(j0[0], msg)
  157. @script:python r3_report depends on report@
  158. j0 << r3_context.j0;
  159. j1 << r3_context.j1;
  160. @@
  161. msg = "Use setup_timer function for function on line %s." % (j1[0].line)
  162. coccilib.report.print_report(j0[0], msg)