Merge tag 'block-pull-request' of https://gitlab.com/stefanha/qemu into staging
[qemu/armbru.git] / scripts / qom-cast-macro-clean-cocci-gen.py
blob2fa8438a146a1de2098452b5ec93a81b8fed5c6c
1 #!/usr/bin/env python3
3 # Generate a Coccinelle semantic patch to remove pointless QOM cast.
5 # Usage:
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 .
16 # SPDX-FileContributor: Philippe Mathieu-Daudé <philmd@linaro.org>
17 # SPDX-FileCopyrightText: 2023 Linaro Ltd.
18 # SPDX-License-Identifier: GPL-2.0-or-later
20 import re
21 import sys
23 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;
30 - {qom_cast_macro}(obj)
31 + obj
32 ''')
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+)\)',
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])