function_call_termination_condition.cocci 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. * This semantic patch does not automagically generate a patch, but still
  28. * points out that kind of code so that it can be fixed (if necesary) by
  29. * programmers.
  30. */
  31. /*
  32. * You may want to run spatch(1) with either -D report or -D org.
  33. * Otherwise, a context output will be generated.
  34. */
  35. virtual report
  36. virtual org
  37. @initialize:python depends on report || org@
  38. msg="Function call in the termination condition of a for loop"
  39. @r@
  40. identifier f;
  41. identifier it;
  42. expression E;
  43. position p;
  44. @@
  45. for (it = E; it < f@p(...); ...)
  46. {
  47. ...
  48. }
  49. @script:python depends on r && report@
  50. p << r.p;
  51. @@
  52. coccilib.report.print_report(p[0], msg)
  53. @script:python depends on r && org@
  54. p << r.p;
  55. @@
  56. msg="Function call in the termination condition of a for loop"
  57. coccilib.org.print_todo(p[0], msg)
  58. @depends on !report && !org && r@
  59. identifier r.f;
  60. identifier r.it;
  61. expression r.E;
  62. @@
  63. * for (it = E; it < f(...); ...)
  64. {
  65. ...
  66. }