ms_timeout.cocci 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (c) 2019-2020 Nordic Semiconductor ASA
  2. // SPDX-License-Identifer: Apache-2.0
  3. // Replace use of K_NO_WAIT and K_FOREVER in API that requires
  4. // timeouts be specified as integral milliseconds.
  5. //
  6. // These constants used to have the values 0 and -1 respectively; they
  7. // are now timeout values which are opaque non-integral values that
  8. // can't be converted to integers automatically. For K_NO_WAIT replace
  9. // with 0; for K_FOREVER replace with SYS_FOREVER_MS, the value of
  10. // which is -1.
  11. //
  12. // Options: --include-headers
  13. virtual patch
  14. virtual report
  15. // ** Handle millisecond timeout as the last parameter
  16. // Match identifer passed as timeout
  17. @match_fn_l1@
  18. identifier fn =~ "(?x)^
  19. (dmic_read
  20. |bt_buf_get_(rx|cmd_complete|evt)
  21. |bt_mesh_cfg_cli_timeout_set
  22. |isotp_(bind|recv(_net)?|send(_net)?(_ctx)?_buf)
  23. |tty_set_(tx|rx)_timeout
  24. |can_(write|recover)
  25. |uart_(tx|rx_enable)
  26. |dns_(resolve_name|get_addr_info)
  27. |net_config_init
  28. |net_ppp_ping
  29. |websocket_(send|recv)_msg
  30. )$";
  31. identifier T;
  32. @@
  33. fn(..., T);
  34. @report_fn_l1
  35. extends match_fn_l1
  36. depends on report
  37. @
  38. identifier K_NO_WAIT =~ "^K_NO_WAIT$";
  39. identifier K_FOREVER =~ "^K_FOREVER$";
  40. position p;
  41. @@
  42. fn@p(...,
  43. (
  44. K_NO_WAIT
  45. |
  46. K_FOREVER
  47. )
  48. )
  49. @script:python
  50. depends on report
  51. @
  52. fn << match_fn_l1.fn;
  53. T << match_fn_l1.T;
  54. p << report_fn_l1.p;
  55. @@
  56. msg = "WARNING: [msl1] replace constant {} with ms duration in {}".format(T, fn)
  57. coccilib.report.print_report(p[0], msg);
  58. @fix_fn_l1
  59. extends match_fn_l1
  60. depends on patch
  61. @
  62. identifier K_NO_WAIT =~ "^K_NO_WAIT$";
  63. identifier K_FOREVER =~ "^K_FOREVER$";
  64. @@
  65. fn(...,
  66. (
  67. - K_NO_WAIT
  68. + 0
  69. |
  70. - K_FOREVER
  71. + SYS_FOREVER_MS
  72. ))
  73. // ** Handle millisecond timeout as second from last parameter
  74. // Match identifer passed as timeout
  75. @match_fn_l2@
  76. identifier fn =~ "(?x)^
  77. (http_client_req
  78. |websocket_connect
  79. )$";
  80. expression L1;
  81. identifier T;
  82. @@
  83. fn(..., T, L1)
  84. @report_fn_l2
  85. extends match_fn_l2
  86. depends on report
  87. @
  88. identifier K_NO_WAIT =~ "^K_NO_WAIT$";
  89. identifier K_FOREVER =~ "^K_FOREVER$";
  90. expression X1;
  91. position p;
  92. @@
  93. fn@p(...,
  94. (
  95. K_NO_WAIT
  96. |
  97. K_FOREVER
  98. )
  99. , X1)
  100. @script:python
  101. depends on report
  102. @
  103. fn << match_fn_l2.fn;
  104. T << match_fn_l2.T;
  105. p << report_fn_l2.p;
  106. @@
  107. msg = "WARNING: [msl2] replace constant {} with ms duration in {}".format(T, fn)
  108. coccilib.report.print_report(p[0], msg);
  109. @fix_fn_l2
  110. extends match_fn_l2
  111. depends on patch
  112. @
  113. identifier K_NO_WAIT =~ "^K_NO_WAIT$";
  114. identifier K_FOREVER =~ "^K_FOREVER$";
  115. expression X1;
  116. @@
  117. fn(...,
  118. (
  119. - K_NO_WAIT
  120. + 0
  121. |
  122. - K_FOREVER
  123. + SYS_FOREVER_MS
  124. )
  125. , X1)
  126. // ** Handle millisecond timeout as third from last parameter
  127. // Match identifer passed as timeout
  128. @match_fn_l3@
  129. identifier fn =~ "(?x)^
  130. (can_send
  131. |lora_recv
  132. )$";
  133. expression L1;
  134. expression L2;
  135. identifier T;
  136. @@
  137. fn(..., T, L2, L1)
  138. @report_fn_l3
  139. extends match_fn_l3
  140. depends on report
  141. @
  142. identifier K_NO_WAIT =~ "^K_NO_WAIT$";
  143. identifier K_FOREVER =~ "^K_FOREVER$";
  144. position p;
  145. @@
  146. fn@p(...,
  147. (
  148. K_NO_WAIT
  149. |
  150. K_FOREVER
  151. )
  152. , L2, L1)
  153. @script:python
  154. depends on report
  155. @
  156. fn << match_fn_l3.fn;
  157. T << match_fn_l3.T;
  158. p << report_fn_l3.p;
  159. @@
  160. msg = "WARNING: [msl3] replace constant {} with ms duration in {}".format(T, fn)
  161. coccilib.report.print_report(p[0], msg);
  162. @fix_fn_l3
  163. extends match_fn_l3
  164. depends on patch
  165. @
  166. identifier K_NO_WAIT =~ "^K_NO_WAIT$";
  167. identifier K_FOREVER =~ "^K_FOREVER$";
  168. @@
  169. fn(...,
  170. (
  171. - K_NO_WAIT
  172. + 0
  173. |
  174. - K_FOREVER
  175. + SYS_FOREVER_MS
  176. )
  177. , L2, L1)