Skip to content

Commit 71104ea

Browse files
committed
Merge branch 'add-inspect-c-bindings-script' into 'dev'
Add scripts/inspect_c_bindings.py See merge request objectbox/objectbox-python!28
2 parents b752cfa + badfc9b commit 71104ea

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

scripts/inspect_c_bindings.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Script used to inspect differences between objectbox/lib/objectbox.h and c.py (e.g. missing function declarations)
2+
# Usage:
3+
# python inspect_c_bindings.py
4+
# Requirements:
5+
# - pycparser
6+
# - pycparser-fake-libc
7+
# - objectbox or project root in PYTHONPATH
8+
9+
from os import path
10+
from pycparser import c_ast, parse_file
11+
import pycparser_fake_libc
12+
import objectbox.c
13+
14+
script_dir = path.dirname(path.realpath(__file__))
15+
16+
17+
class FuncDeclVisitor(c_ast.NodeVisitor):
18+
def __init__(self):
19+
self.func_decls = set()
20+
21+
def visit_FuncDecl(self, node: c_ast.FuncDecl):
22+
# TODO declname is set in the return type (i.e. type field)?
23+
func_name = None
24+
if isinstance(node.type, c_ast.TypeDecl):
25+
func_name = node.type.declname
26+
elif isinstance(node.type, c_ast.PtrDecl):
27+
func_name = node.type.type.declname
28+
else:
29+
raise Exception(f"Unknown node type: {node.type}")
30+
self.func_decls.add(func_name)
31+
32+
33+
def _parse_header_file(filename):
34+
fake_libc_arg = "-I" + pycparser_fake_libc.directory
35+
36+
ast = parse_file(filename, use_cpp=True, cpp_args=fake_libc_arg) # use_cpp = Use C Pre Processor
37+
38+
visitor = FuncDeclVisitor()
39+
visitor.visit(ast)
40+
41+
num_missing = 0
42+
for func_decl in visitor.func_decls:
43+
if not hasattr(objectbox.c, func_decl):
44+
print(f"Missing function: {func_decl}")
45+
num_missing += 1
46+
47+
print(f"Missing {num_missing}/{len(visitor.func_decls)} function declarations in c.py")
48+
49+
50+
def _main():
51+
objectbox_h = path.join(script_dir, "../objectbox/lib/objectbox.h")
52+
if not path.exists(objectbox_h):
53+
raise Exception("File not found: objectbox/lib/objectbox.h")
54+
_parse_header_file(objectbox_h)
55+
56+
57+
if __name__ == "__main__":
58+
_main()

0 commit comments

Comments
 (0)