function_call_termination_condition.cocci 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. @r@
  35. type t;
  36. identifier f;
  37. identifier it;
  38. expression E1;
  39. position p;
  40. @@
  41. t it;
  42. ...
  43. for@p (it = E1; it < f(...); ...)
  44. {
  45. ...
  46. }
  47. @depends on r && context@
  48. identifier r.f;
  49. identifier r.it;
  50. expression r.E1;
  51. @@
  52. * for (it = E1; it < f(...); ...)
  53. {
  54. ...
  55. }
  56. @script:python depends on r && org@
  57. p << r.p;
  58. @@
  59. coccilib.org.print_todo(p[0], msg)
  60. @depends on r && patch@
  61. type r.t;
  62. expression r.E1, E2, E3;
  63. identifier r.it;
  64. position r.p;
  65. @@
  66. -for@p(it = E1; it < E3; E2)
  67. +t max = E3;
  68. +for(it = E1; i < max; E2)
  69. {
  70. ...
  71. }
  72. @script:python depends on r && report@
  73. p << r.p;
  74. @@
  75. coccilib.report.print_report(p[0], msg)