|
| 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 | + |
| 5 | +from os import path |
| 6 | +from pycparser import c_ast, parse_file |
| 7 | +import pycparser_fake_libc |
| 8 | +import objectbox.c |
| 9 | + |
| 10 | +script_dir = path.dirname(path.realpath(__file__)) |
| 11 | + |
| 12 | + |
| 13 | +class FuncDeclVisitor(c_ast.NodeVisitor): |
| 14 | + def __init__(self): |
| 15 | + self.func_decls = set() |
| 16 | + |
| 17 | + def visit_FuncDecl(self, node: c_ast.FuncDecl): |
| 18 | + # TODO declname is set in the return type (i.e. type field)? |
| 19 | + func_name = None |
| 20 | + if isinstance(node.type, c_ast.TypeDecl): |
| 21 | + func_name = node.type.declname |
| 22 | + elif isinstance(node.type, c_ast.PtrDecl): |
| 23 | + func_name = node.type.type.declname |
| 24 | + else: |
| 25 | + raise Exception(f"Unknown node type: {node.type}") |
| 26 | + self.func_decls.add(func_name) |
| 27 | + |
| 28 | + |
| 29 | +def _parse_header_file(filename): |
| 30 | + fake_libc_arg = "-I" + pycparser_fake_libc.directory |
| 31 | + |
| 32 | + ast = parse_file(filename, use_cpp=True, cpp_args=fake_libc_arg) # use_cpp = Use C Pre Processor |
| 33 | + |
| 34 | + visitor = FuncDeclVisitor() |
| 35 | + visitor.visit(ast) |
| 36 | + |
| 37 | + num_missing = 0 |
| 38 | + for func_decl in visitor.func_decls: |
| 39 | + if not hasattr(objectbox.c, func_decl): |
| 40 | + print(f"Missing function: {func_decl}") |
| 41 | + num_missing += 1 |
| 42 | + |
| 43 | + print(f"Missing {num_missing}/{len(visitor.func_decls)} function declarations in c.py") |
| 44 | + |
| 45 | + |
| 46 | +def _main(): |
| 47 | + objectbox_h = path.join(script_dir, "../objectbox/lib/objectbox.h") |
| 48 | + if not path.exists(objectbox_h): |
| 49 | + raise Exception("File not found: objectbox/lib/objectbox.h") |
| 50 | + _parse_header_file(objectbox_h) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + _main() |
0 commit comments