function_call_termination_condition.cocci 1.7 KB

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