function_call_termination_condition.cocci 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * StarPU --- Runtime system for heterogeneous multicore architectures.
  3. *
  4. * Copyright (C) 2012 inria
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * It is a bad idea to write code such as :
  19. *
  20. * for (i = 0; i < foo(...); i++) { ... }
  21. *
  22. * Indeed, foo will be called every time we enter the loop. This would be better :
  23. *
  24. * unsigned int max = foo(...);
  25. * for (i = 0; i < max; i++) { ... }
  26. *
  27. */
  28. virtual context
  29. virtual org
  30. virtual patch
  31. virtual report
  32. @initialize:python depends on report || org@
  33. msg="Function call in the termination condition of a for loop"
  34. from re import sub
  35. orgmsg = sub(r'(%[a-z])', r'=\1=', msg)
  36. @r@
  37. type t;
  38. identifier f;
  39. identifier it;
  40. expression E1;
  41. position p;
  42. @@
  43. t it;
  44. ...
  45. for@p (it = E1; it < f(...); ...)
  46. {
  47. ...
  48. }
  49. @depends on r && context@
  50. identifier r.f;
  51. identifier r.it;
  52. expression r.E1;
  53. @@
  54. * for (it = E1; it < f(...); ...)
  55. {
  56. ...
  57. }
  58. @script:python depends on r && org@
  59. p << r.p;
  60. @@
  61. coccilib.org.print_todo(p[0], orgmsg)
  62. @depends on r && patch@
  63. expression r.E1, E2, E3;
  64. identifier r.it;
  65. position r.p;
  66. @@
  67. -for@p(it = E1; it < E3; E2)
  68. +max = E3;
  69. +for(it = E1; it < max; E2)
  70. {
  71. ...
  72. }
  73. @script:python depends on r && report@
  74. p << r.p;
  75. @@
  76. coccilib.report.print_report(p[0], msg)