memcpy-assign.cocci 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Replace memcpy with struct assignment.
  3. //
  4. // Confidence: High
  5. // Copyright: (C) 2012 Peter Senna Tschudin, INRIA/LIP6. GPLv2.
  6. // URL: http://coccinelle.lip6.fr/
  7. // Comments:
  8. // Options: --no-includes --include-headers
  9. virtual patch
  10. virtual report
  11. virtual context
  12. virtual org
  13. @r1 depends on !patch@
  14. identifier struct_name;
  15. struct struct_name to;
  16. struct struct_name from;
  17. struct struct_name *top;
  18. struct struct_name *fromp;
  19. position p;
  20. @@
  21. memcpy@p(\(&(to)\|top\), \(&(from)\|fromp\), \(sizeof(to)\|sizeof(from)\|sizeof(struct struct_name)\|sizeof(*top)\|sizeof(*fromp)\))
  22. @script:python depends on report@
  23. p << r1.p;
  24. @@
  25. coccilib.report.print_report(p[0],"Replace memcpy with struct assignment")
  26. @depends on context@
  27. position r1.p;
  28. @@
  29. *memcpy@p(...);
  30. @script:python depends on org@
  31. p << r1.p;
  32. @@
  33. cocci.print_main("Replace memcpy with struct assignment",p)
  34. @depends on patch@
  35. identifier struct_name;
  36. struct struct_name to;
  37. struct struct_name from;
  38. @@
  39. (
  40. -memcpy(&(to), &(from), sizeof(to));
  41. +to = from;
  42. |
  43. -memcpy(&(to), &(from), sizeof(from));
  44. +to = from;
  45. |
  46. -memcpy(&(to), &(from), sizeof(struct struct_name));
  47. +to = from;
  48. )
  49. @depends on patch@
  50. identifier struct_name;
  51. struct struct_name to;
  52. struct struct_name *from;
  53. @@
  54. (
  55. -memcpy(&(to), from, sizeof(to));
  56. +to = *from;
  57. |
  58. -memcpy(&(to), from, sizeof(*from));
  59. +to = *from;
  60. |
  61. -memcpy(&(to), from, sizeof(struct struct_name));
  62. +to = *from;
  63. )
  64. @depends on patch@
  65. identifier struct_name;
  66. struct struct_name *to;
  67. struct struct_name from;
  68. @@
  69. (
  70. -memcpy(to, &(from), sizeof(*to));
  71. + *to = from;
  72. |
  73. -memcpy(to, &(from), sizeof(from));
  74. + *to = from;
  75. |
  76. -memcpy(to, &(from), sizeof(struct struct_name));
  77. + *to = from;
  78. )
  79. @depends on patch@
  80. identifier struct_name;
  81. struct struct_name *to;
  82. struct struct_name *from;
  83. @@
  84. (
  85. -memcpy(to, from, sizeof(*to));
  86. + *to = *from;
  87. |
  88. -memcpy(to, from, sizeof(*from));
  89. + *to = *from;
  90. |
  91. -memcpy(to, from, sizeof(struct struct_name));
  92. + *to = *from;
  93. )