Debian Codegraph @

c/tools/codec-generator/find_specs.py

raw
1 import argparse
2 import json
3 import os
4 import re
5 import sys
6
7 from pycparser import c_ast, parse_file
8
9 from typing import Any, Dict, List, Set, Tuple, Union
10
11
12 def strip_quotes(arg: str) -> str:
13 if arg[0] != '"' or arg[-1:] != '"':
14 raise Exception(arg)
15 return arg[1:-1]
16
17
18 def find_function_calls(file: str, name: str, includes: List[str], defines: List[str]) -> List[List[Any]]:
19 class FillFinder(c_ast.NodeVisitor):
20 def __init__(self):
21 self._name = name
22 self.result = []
23
24 def visit_FuncCall(self, node):
25 if node.name.name == self._name:
26 r = []
27 for e in node.args.exprs:
28 r.append(e)
29 self.result.append(r)
30
31 include_args = [f'-I{d}' for d in includes]
32 define_args = [f'-D{d}' for d in defines]
33 ast = parse_file(file, use_cpp=True,
34 cpp_args=[
35 *include_args,
36 *define_args
37 ])
38 ff = FillFinder()
39 ff.visit(ast)
40 return ff.result
41
42
43 c_defines = ['GENERATE_CODEC_CODE',
44 'NDEBUG',
45 '__attribute__(X)=',
46 '__asm__(X)=',
47 '__inline=',
48 '__extension__=',
49 '__restrict=',
50 '__builtin_va_list=int']
51
52
53 def find_fill_specs(c_includes, pn_source):
54 amqp_calls = find_function_calls(os.path.join(pn_source, 'c/src/core/transport.c'), 'pn_fill_performative',
55 c_includes, c_defines)
56 sasl_calls = find_function_calls(os.path.join(pn_source, 'c/src/sasl/sasl.c'), 'pn_fill_performative',
57 c_includes, c_defines)
58 message_calls = find_function_calls(os.path.join(pn_source, 'c/src/core/message.c'), 'pn_data_fill',
59 c_includes, c_defines)
60 fill_spec_args = [c[1] for c in amqp_calls]
61 fill_spec_args += [c[1] for c in sasl_calls]
62 fill_spec_args += [c[1] for c in message_calls]
63 fill_specs: Set[str] = \
64 {strip_quotes(e.value) for e in fill_spec_args if type(e) is c_ast.Constant and e.type == 'string'}
65 return fill_specs
66
67
68 def find_scan_specs(c_includes, pn_source):
69 amqp_calls = find_function_calls(os.path.join(pn_source, 'c/src/core/transport.c'), 'pn_data_scan',
70 c_includes, c_defines)
71 message_calls = find_function_calls(os.path.join(pn_source, 'c/src/core/message.c'),'pn_data_scan',
72 c_includes, c_defines)
73 scan_spec_args = [c[1] for c in amqp_calls]
74 scan_spec_args += [c[1] for c in message_calls]
75 # Only try to generate code for constant strings
76 scan_specs: Set[str] = \
77 {strip_quotes(e.value) for e in scan_spec_args if type(e) is c_ast.Constant and e.type == 'string'}
78 return scan_specs
79
80
81 def main():
82 argparser = argparse.ArgumentParser(description='Find scan/fill specs in proton code for code generation')
83 argparser.add_argument('-o', '--output', help='output json file', type=str, required=True)
84 argparser.add_argument('-s', '--source', help='proton source directory', type=str, required=True)
85 argparser.add_argument('-b', '--build', help='proton build directory', type=str, required=True)
86
87 args = argparser.parse_args()
88
89 pn_source = args.source
90 pn_build = args.build
91 json_filename = args.output
92
93 c_includes = [f'{os.path.join(pn_build, "c/include")}',
94 f'{os.path.join(pn_build, "c/src")}']
95 fill_specs = find_fill_specs(c_includes, pn_source)
96 scan_specs = find_scan_specs(c_includes, pn_source)
97 with open(json_filename, 'w') as file:
98 json.dump({'fill_specs': sorted(list(fill_specs)), 'scan_specs': sorted(list(scan_specs))}, file, indent=2)
99
100
101 if __name__ == '__main__':
102 sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
103 sys.exit(main())