qom-cast-macro-clean-cocci-gen.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python3
  2. #
  3. # Generate a Coccinelle semantic patch to remove pointless QOM cast.
  4. #
  5. # Usage:
  6. #
  7. # $ qom-cast-macro-clean-cocci-gen.py $(git ls-files) > qom_pointless_cast.cocci
  8. # $ spatch \
  9. # --macro-file scripts/cocci-macro-file.h \
  10. # --sp-file qom_pointless_cast.cocci \
  11. # --keep-comments \
  12. # --use-gitgrep \
  13. # --in-place \
  14. # --dir .
  15. #
  16. # Copyright (c) 2023 Linaro Ltd.
  17. #
  18. # Authors:
  19. # Philippe Mathieu-Daudé
  20. #
  21. # SPDX-License-Identifier: GPL-2.0-or-later
  22. import re
  23. import sys
  24. assert len(sys.argv) > 0
  25. def print_cocci_rule(qom_typedef, qom_cast_macro):
  26. print(f'''@@
  27. typedef {qom_typedef};
  28. {qom_typedef} *obj;
  29. @@
  30. - {qom_cast_macro}(obj)
  31. + obj
  32. ''')
  33. patterns = [
  34. r'DECLARE_INSTANCE_CHECKER\((\w+),\W*(\w+),\W*TYPE_\w+\)',
  35. r'DECLARE_OBJ_CHECKERS\((\w+),\W*\w+,\W*(\w+),\W*TYPE_\w+\)',
  36. r'OBJECT_DECLARE_TYPE\((\w+),\W*\w+,\W*(\w+)\)',
  37. r'OBJECT_DECLARE_SIMPLE_TYPE\((\w+),\W*(\w+)\)',
  38. r'INTERFACE_CHECK\((\w+),\W*\(\w+\),\W*TYPE_(\w+)\)',
  39. ]
  40. for fn in sys.argv[1:]:
  41. try:
  42. content = open(fn, 'rt').read()
  43. except:
  44. continue
  45. for pattern in patterns:
  46. for match in re.findall(pattern, content):
  47. print_cocci_rule(match[0], match[1])