find_functions.cocci 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) 2020 Intel Corporation
  2. // SPDX-License-Identifer: Apache-2.0
  3. // In patch mode, patch all device instance to const (if not already).
  4. // In report mode:
  5. // Generate a q&d python database (a dict actually) of all the
  6. // declared zephyr functions. It will store each function name in 2
  7. // separate dicts: one storing all function having 1+ void* parameter
  8. // and one for all the other functions. It will store the positions
  9. // of the void* parameter in the first dict, and the actual number of
  10. // parameters in the second.
  11. // Then find_dev_usage.cocci can be used to verify if device instance
  12. // are not loosing their const qualifier.
  13. virtual patch
  14. virtual report
  15. ////////////////////
  16. // Initialization //
  17. ////////////////////
  18. @initialize:python
  19. depends on report
  20. @
  21. @@
  22. import pickle
  23. f_void = {}
  24. f_other = {}
  25. // Insert a function into right dict depending on parameters
  26. def insert_function(f, params):
  27. void_pos = []
  28. i = 0
  29. for prm in params:
  30. if prm.startswith("void *"):
  31. void_pos.append(i)
  32. i += 1
  33. if len(void_pos) != 0:
  34. f_void[f] = void_pos
  35. else:
  36. f_other[f] = i + 1
  37. ///////////
  38. // Rules //
  39. ///////////
  40. // Switch device instance to constant.
  41. @r_const_dev
  42. depends on patch
  43. disable optional_qualifier
  44. @
  45. @@
  46. -struct device *
  47. +const struct device *
  48. // Find function declarations
  49. @r_find_func_declare
  50. depends on report
  51. @
  52. identifier f;
  53. type ret_type;
  54. parameter list[nb_params] params;
  55. @@
  56. ret_type f(params);
  57. // Insert function declaration
  58. @script:python
  59. depends on report
  60. @
  61. f << r_find_func_declare.f;
  62. params << r_find_func_declare.params;
  63. @@
  64. insert_function(f, params)
  65. // Find function implementations and inlines
  66. // (maybe it should focus on static only?
  67. // but then first rule should not match statics.)
  68. @r_find_func_impl_inlines
  69. depends on report
  70. @
  71. identifier f;
  72. type ret_type;
  73. parameter list[nb_params] params;
  74. @@
  75. (
  76. ret_type f(params)
  77. {
  78. ...
  79. }
  80. |
  81. static inline ret_type f(params)
  82. {
  83. ...
  84. }
  85. )
  86. // Insert function implentations and inlines
  87. @script:python
  88. depends on report
  89. @
  90. f << r_find_func_impl_inlines.f;
  91. params << r_find_func_impl_inlines.params;
  92. @@
  93. insert_function(f, params)
  94. //////////////////
  95. // Finalization //
  96. //////////////////
  97. @finalize:python
  98. depends on report
  99. @
  100. @@
  101. with open("function_names.pickle", "wb") as f:
  102. data = {}
  103. data['f_void'] = f_void
  104. data['f_other'] = f_other
  105. pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)