]> git.proxmox.com Git - mirror_qemu.git/blob - scripts/qom-cast-macro-clean-cocci-gen.py
Merge tag 'pull-request-2024-01-11' of https://gitlab.com/thuth/qemu into staging
[mirror_qemu.git] / scripts / qom-cast-macro-clean-cocci-gen.py
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
20 import re
21 import sys
22
23 assert len(sys.argv) > 0
24
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
34 patterns = [
35 r'DECLARE_INSTANCE_CHECKER\((\w+),\W*(\w+),\W*TYPE_\w+\)',
36 r'DECLARE_OBJ_CHECKERS\((\w+),\W*\w+,\W*(\w+),\W*TYPE_\w+\)',
37 r'OBJECT_DECLARE_TYPE\((\w+),\W*\w+,\W*(\w+)\)',
38 r'OBJECT_DECLARE_SIMPLE_TYPE\((\w+),\W*(\w+)\)',
39 r'INTERFACE_CHECK\((\w+),\W*\(\w+\),\W*TYPE_(\w+)\)',
40 ]
41
42 for fn in sys.argv[1:]:
43 try:
44 content = open(fn, 'rt').read()
45 except:
46 continue
47 for pattern in patterns:
48 for match in re.findall(pattern, content):
49 print_cocci_rule(match[0], match[1])