swap.cocci 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. ///
  3. /// Check for opencoded swap() implementation.
  4. ///
  5. // Confidence: High
  6. // Copyright: (C) 2021 Denis Efremov ISPRAS
  7. // Options: --no-includes --include-headers
  8. //
  9. // Keywords: swap
  10. //
  11. virtual patch
  12. virtual org
  13. virtual report
  14. virtual context
  15. @rvar depends on !patch@
  16. identifier tmp;
  17. expression a, b;
  18. type T;
  19. position p;
  20. @@
  21. (
  22. * T tmp;
  23. |
  24. * T tmp = 0;
  25. |
  26. * T *tmp = NULL;
  27. )
  28. ... when != tmp
  29. * tmp = a;
  30. * a = b;@p
  31. * b = tmp;
  32. ... when != tmp
  33. @r depends on !patch@
  34. identifier tmp;
  35. expression a, b;
  36. position p != rvar.p;
  37. @@
  38. * tmp = a;
  39. * a = b;@p
  40. * b = tmp;
  41. @rpvar depends on patch@
  42. identifier tmp;
  43. expression a, b;
  44. type T;
  45. @@
  46. (
  47. - T tmp;
  48. |
  49. - T tmp = 0;
  50. |
  51. - T *tmp = NULL;
  52. )
  53. ... when != tmp
  54. - tmp = a;
  55. - a = b;
  56. - b = tmp
  57. + swap(a, b)
  58. ;
  59. ... when != tmp
  60. @rp depends on patch@
  61. identifier tmp;
  62. expression a, b;
  63. @@
  64. - tmp = a;
  65. - a = b;
  66. - b = tmp
  67. + swap(a, b)
  68. ;
  69. @depends on patch && (rpvar || rp)@
  70. @@
  71. (
  72. for (...;...;...)
  73. - {
  74. swap(...);
  75. - }
  76. |
  77. while (...)
  78. - {
  79. swap(...);
  80. - }
  81. |
  82. if (...)
  83. - {
  84. swap(...);
  85. - }
  86. )
  87. @script:python depends on report@
  88. p << r.p;
  89. @@
  90. coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
  91. @script:python depends on org@
  92. p << r.p;
  93. @@
  94. coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")
  95. @script:python depends on report@
  96. p << rvar.p;
  97. @@
  98. coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
  99. @script:python depends on org@
  100. p << rvar.p;
  101. @@
  102. coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")