Regexp based Member renaming (#66)

Closes #28 
- Added Member renaming using `member-rename` subkey in declarations.
- Renames struct/enum members and function parameter names using regexp/full matching.
- Added/Updated renaming tests.
14 files changed
tree: 87212bf50f6f83fb9e2706d48cc2a20a8b716d28
  1. .gitignore
  2. .travis.yml
  7. analysis_options.yaml
  8. bin/
  9. example/
  10. lib/
  11. pubspec.yaml
  12. test/
  13. third_party/
  14. tool/

Build Status

Experimental binding generator for FFI bindings.


For some header file example.h:

int sum(int a, int b);

Add configurations to Pubspec File:

  output: 'generated_bindings.dart'
      - 'example.h'

Output (generated_bindings.dart).

class NativeLibrary {
  final DynamicLibrary _dylib;

  NativeLibrary(DynamicLibrary dynamicLibrary) : _dylib = dynamicLibrary;

  int sum(int a, int b) {
    _sum ??= _dylib.lookupFunction<_c_sum, _dart_sum>('sum');
    return _sum(a, b);
  _dart_sum _sum;;
typedef _c_sum = ffi.Int32 Function(Int32 a, Int32 b);
typedef _dart_sum = int Function(int a,int b);

Using this package

  • Add this package as dev_dependency in your pubspec.yaml.
  • Setup for use (see Setup).
  • Configurations must be provided in pubspec.yaml or in a custom YAML file (see configurations).
  • Run the tool- pub run ffigen.


package:ffigen uses LLVM. Install LLVM (9+) in the following way.


  1. Install libclangdev - sudo apt-get install libclang-dev.


  1. Install Visual Studio with C++ development support.
  2. Install LLVM or winget install -e --id LLVM.LLVM.


  1. Install Xcode.
  2. Install LLVM - brew install llvm.


Configurations can be provided in 2 ways-

  1. In the project's pubspec.yaml file under the key ffigen.
  2. Via a custom YAML file, then specify this file while running - pub run ffigen --config config.yaml

The following configuration options are available-


Fixed size array‘s in structs aren’t currently supported by Dart. However we provide a workaround, using which array items can now be accessed using [] operator.

Here's a C structure from libclang-

typedef struct {
  unsigned long long data[3];
} CXFileUniqueID;

The generated code is -

class CXFileUniqueID extends ffi.Struct {
  int _unique_data_item_0;
  int _unique_data_item_1;
  int _unique_data_item_2;

  /// Helper for array `data`.
  ArrayHelper_CXFileUniqueID_data_level0 get data =>
      ArrayHelper_CXFileUniqueID_data_level0(this, [3], 0, 0);

/// Helper for array `data` in struct `CXFileUniqueID`.
class ArrayHelper_CXFileUniqueID_data_level0 {
  final CXFileUniqueID _struct;
  final List<int> dimensions;
  final int level;
  final int _absoluteIndex;
  int get length => dimensions[level];
      this._struct, this.dimensions, this.level, this._absoluteIndex);
  void _checkBounds(int index) {
    if (index >= length || index < 0) {
      throw RangeError(
          'Dimension $level: index not in range 0..${length} exclusive.');

  int operator [](int index) {
    switch (_absoluteIndex + index) {
      case 0:
        return _struct._unique_data_item_0;
      case 1:
        return _struct._unique_data_item_1;
      case 2:
        return _struct._unique_data_item_2;
        throw Exception('Invalid Array Helper generated.');

  void operator []=(int index, int value) {
    switch (_absoluteIndex + index) {
      case 0:
        _struct._unique_data_item_0 = value;
      case 1:
        _struct._unique_data_item_1 = value;
      case 2:
        _struct._unique_data_item_2 = value;
        throw Exception('Invalid Array Helper generated.');


  1. Multi OS support for types such as long. Issue #7
  2. Function's passing/returning structs by value are skipped. Issue #3
  3. Structs containing structs will have all their members removed. Issue #4

Trying out examples

  1. cd examples/<example_u_want_to_run>, Run pub get.
  2. Run pub run ffigen.

Running Tests

  1. Run setup to build the LLVM wrapper - pub run ffigen:setup.
  2. Dynamic library for some tests also need to be built before running the examples.
  3. cd test/native_test.
  4. Run dart build_test_dylib.dart.

Run tests from the root of the package with pub run test.