function_call_termination_condition.cocci 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. type r.t;
  64. expression r.E1, E2, E3;
  65. identifier r.it;
  66. position r.p;
  67. @@
  68. -for@p(it = E1; it < E3; E2)
  69. +t max = E3;
  70. +for(it = E1; it < max; E2)
  71. {
  72. ...
  73. }
  74. @script:python depends on r && report@
  75. p << r.p;
  76. @@
  77. coccilib.report.print_report(p[0], msg)