Description
Currently, the OrtApi struct generator (tools/gen_ortapi.go) uses regex-based parsing to extract function pointers from the ONNX Runtime C header file. While this works for the current API, it has limitations:
- Fragile: Regex patterns may break with header formatting changes
- Limited: Only extracts function names, not full signatures
- Manual: Requires manual pattern updates for new C API patterns
Proposal
Migrate to using tree-sitter-c for parsing the C header files. This would enable:
- Robust parsing: Proper C AST parsing handles all valid C syntax
- Full signature extraction: Parse return types, parameter types, and names
- Auto-generate purego bindings: Generate not just struct definitions but also Go wrapper functions
- Better validation: Detect breaking changes in C API structure
Potential Implementation
// Pseudo-code example
func parseWithTreeSitter(headerPath string) []FunctionDecl {
// Use tree-sitter-c to parse the header
parser := treesitter.NewParser()
parser.SetLanguage(c.GetLanguage())
tree := parser.Parse(readFile(headerPath))
// Walk the AST to find OrtApi struct
// Extract function pointer declarations with full signatures
// Generate both struct and wrapper functions
}
Benefits
- Type safety: Generate typed wrappers instead of manual
purego.RegisterFunc calls
- Documentation: Extract and preserve C API comments
- Maintainability: Easier to update when ONNX Runtime evolves
- Correctness: Eliminate regex parsing bugs
Related
Implementation Notes
This is a non-trivial enhancement that should be done incrementally:
- First, implement tree-sitter parsing alongside existing regex approach
- Validate both produce identical results
- Switch to tree-sitter-only generation
- Add auto-generation of wrapper functions (optional)
References
Description
Currently, the OrtApi struct generator (
tools/gen_ortapi.go) uses regex-based parsing to extract function pointers from the ONNX Runtime C header file. While this works for the current API, it has limitations:Proposal
Migrate to using tree-sitter-c for parsing the C header files. This would enable:
Potential Implementation
Benefits
purego.RegisterFunccallsRelated
tools/gen_ortapi.goort/ortapi_generated.goImplementation Notes
This is a non-trivial enhancement that should be done incrementally:
References