find_dev_usage.cocci 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (c) 2020 Intel Corporation
  2. // SPDX-License-Identifer: Apache-2.0
  3. // Uses a python database (a dict) to find where const struct device
  4. // variable are being used in zephyr functions and, if it's being in place
  5. // of a void*, it will print an ERROR for loosing the const qualifier.
  6. // If it's being used on an unknown functions from an external module such
  7. // as a HAL, it will print a WARNING in order to check if the const qualifier
  8. // is not lost.
  9. virtual report
  10. ////////////////////
  11. // Initialization //
  12. ////////////////////
  13. @initialize:python
  14. depends on report
  15. @
  16. @@
  17. import pickle
  18. def check_and_report(F, f, D, nb_args, p):
  19. if f in f_void and int(nb_args) in f_void[f]:
  20. msg = "ERROR: in {} calling {} param with {}, \
  21. loosing const qualifier, please wrap".format(F, f, D)
  22. coccilib.report.print_report(p[0], msg)
  23. elif f not in f_void and f not in f_other and not f.isupper():
  24. msg = "WARNING: in {} calling {} param with {}, \
  25. check if const qualifier is not lost".format(F, f, D)
  26. coccilib.report.print_report(p[0], msg)
  27. // Loading function data base
  28. with open("function_names.pickle", "rb") as f:
  29. data = pickle.load(f)
  30. f_void = data["f_void"]
  31. f_other = data["f_other"]
  32. ///////////
  33. // Rules //
  34. ///////////
  35. // Find usage of a device instance
  36. @r_find_dev_usage
  37. depends on report
  38. @
  39. local idexpression struct device *D;
  40. expression list[nb_args] args;
  41. identifier f;
  42. position p;
  43. @@
  44. f(args, D@p, ...)
  45. @script:python
  46. depends on r_find_dev_usage
  47. @
  48. f << r_find_dev_usage.f;
  49. D << r_find_dev_usage.D;
  50. nb_args << r_find_dev_usage.nb_args;
  51. p << r_find_dev_usage.p;
  52. @@
  53. check_and_report(p[0].current_element, f, D, nb_args, p)