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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. # SPDX-FileContributor: Philippe Mathieu-Daudé <philmd@linaro.org>
  17. # SPDX-FileCopyrightText: 2023 Linaro Ltd.
  18. # SPDX-License-Identifier: GPL-2.0-or-later
  19. import re
  20. import sys
  21. assert len(sys.argv) > 0
  22. def print_cocci_rule(qom_typedef, qom_cast_macro):
  23. print(f'''@@
  24. typedef {qom_typedef};
  25. {qom_typedef} *obj;
  26. @@
  27. - {qom_cast_macro}(obj)
  28. + obj
  29. ''')
  30. patterns = [
  31. r'DECLARE_INSTANCE_CHECKER\((\w+),\W*(\w+),\W*TYPE_\w+\)',
  32. r'DECLARE_OBJ_CHECKERS\((\w+),\W*\w+,\W*(\w+),\W*TYPE_\w+\)',
  33. r'OBJECT_DECLARE_TYPE\((\w+),\W*\w+,\W*(\w+)\)',
  34. r'OBJECT_DECLARE_SIMPLE_TYPE\((\w+),\W*(\w+)\)',
  35. r'INTERFACE_CHECK\((\w+),\W*\(\w+\),\W*TYPE_(\w+)\)',
  36. ]
  37. for fn in sys.argv[1:]:
  38. try:
  39. content = open(fn, 'rt').read()
  40. except:
  41. continue
  42. for pattern in patterns:
  43. for match in re.findall(pattern, content):
  44. print_cocci_rule(match[0], match[1])