Skip to content

[ffigen] Test C++ class pointers in more situations - #3745

Open
Manar-Elhabbal7 wants to merge 19 commits into
dart-lang:mainfrom
Manar-Elhabbal7:cpp-pointers-tests
Open

Manar-Elhabbal7 wants to merge 19 commits into
dart-lang:mainfrom
Manar-Elhabbal7:cpp-pointers-tests

Conversation

@Manar-Elhabbal7

@Manar-Elhabbal7 Manar-Elhabbal7 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

1. Code Generator (code_generator/)

  • Struct fields (compound.dart): Added support for C++ class pointers in struct fields by generating private storage fields (_field) and public Dart getters/setters that convert between the Dart wrapper class and the native pointer. Updated $allocate to accept the wrapper type.
  • Globals & Ownership (global.dart, type.dart, objc_*.dart): Added a generateRelease() hook so global variable setters only emit release code for types that require it (Objective-C). This prevents incorrect release calls for C++ pointers while maintaining proper cleanup for Objective-C blocks and objects.

2. Tests (test/native_cpp_test/)

  • Added tests verifying C++ class pointer (Animal*) support in:
    • Struct fields (AnimalOwner)
    • Top-level function arguments and return values
    • Global variables (globalAnimal)
    • Function pointer arguments and return values (callbacks, factory, processor, and transform).

Fixes: #3489

@Manar-Elhabbal7 Manar-Elhabbal7 changed the title [ffigen] Add tests for C++ class pointers in top-level functions and … [ffigen] Test C++ class pointers in more situations Oct 13, 2026
@Manar-Elhabbal7
Manar-Elhabbal7 marked this pull request as ready for review October 15, 2026 15:28
@Manar-Elhabbal7

Copy link
Copy Markdown
Contributor Author

Hello @liamappelbe PTAL

@liamappelbe
liamappelbe self-requested a review October 30, 2026 01:19
@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 22.50000% with 31 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.46%. Comparing base (27c74d1) to head (9b16f28).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pkgs/ffigen/lib/src/code_generator/compound.dart 39.13% 14 Missing ⚠️
pkgs/ffigen/lib/src/code_generator/global.dart 0.00% 3 Missing ⚠️
pkgs/ffigen/lib/src/code_generator/objc_block.dart 0.00% 2 Missing ⚠️
.../ffigen/lib/src/code_generator/objc_interface.dart 0.00% 2 Missing ⚠️
...s/ffigen/lib/src/code_generator/objc_nullable.dart 0.00% 2 Missing ⚠️
...s/ffigen/lib/src/code_generator/objc_protocol.dart 0.00% 2 Missing ⚠️
pkgs/ffigen/lib/src/code_generator/pointer.dart 0.00% 2 Missing ⚠️
pkgs/ffigen/lib/src/code_generator/type.dart 0.00% 2 Missing ⚠️
pkgs/ffigen/lib/src/code_generator/typealias.dart 0.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3745      +/-   ##
==========================================
- Coverage   86.19%   85.46%   -0.74%     
==========================================
  Files         324      324              
  Lines       21135    21081      -54     
==========================================
- Hits        18218    18017     -201     
- Misses       2917     3064     +147     
Flag Coverage Δ
native_pkgs_macos 81.91% <ø> (ø)
native_pkgs_ubuntu 69.34% <ø> (ø)
native_pkgs_windows 72.29% <ø> (ø)
objective_c 61.77% <22.50%> (-0.18%) ⬇️
swiftgen 80.41% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
code_assets 95.31% <ø> (ø)
data_assets 91.56% <ø> (ø)
ffi 100.00% <ø> (ø)
ffigen 89.99% <22.50%> (-1.85%) ⬇️
hooks 86.40% <ø> (ø)
hooks_runner 90.89% <ø> (ø)
jni 65.67% <ø> (-2.02%) ⬇️
jni_flutter ∅ <ø> (∅)
jni_util 25.00% <ø> (ø)
jnigen 82.72% <ø> (ø)
json_syntax_generator 42.01% <ø> (ø)
native_test_helpers 85.71% <ø> (ø)
native_toolchain_c 96.71% <ø> (ø)
objective_c 81.43% <ø> (ø)
pub_formats ∅ <ø> (∅)
record_use 81.48% <ø> (ø)
snippet_tool 66.23% <ø> (ø)
swift2objc 92.95% <ø> (ø)
swiftgen 80.41% <ø> (ø)
test_case_selector 97.28% <ø> (ø)
Files with missing lines Coverage Δ
pkgs/ffigen/lib/src/code_generator/objc_block.dart 99.34% <0.00%> (-0.66%) ⬇️
.../ffigen/lib/src/code_generator/objc_interface.dart 98.49% <0.00%> (-1.51%) ⬇️
...s/ffigen/lib/src/code_generator/objc_nullable.dart 87.23% <0.00%> (-3.88%) ⬇️
...s/ffigen/lib/src/code_generator/objc_protocol.dart 92.81% <0.00%> (-1.04%) ⬇️
pkgs/ffigen/lib/src/code_generator/pointer.dart 90.00% <0.00%> (-1.22%) ⬇️
pkgs/ffigen/lib/src/code_generator/type.dart 79.48% <0.00%> (-2.10%) ⬇️
pkgs/ffigen/lib/src/code_generator/typealias.dart 90.56% <0.00%> (-1.75%) ⬇️
pkgs/ffigen/lib/src/code_generator/global.dart 71.56% <0.00%> (-28.44%) ⬇️
pkgs/ffigen/lib/src/code_generator/compound.dart 83.87% <39.13%> (-10.05%) ⬇️

... and 18 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Manar-Elhabbal7

Manar-Elhabbal7 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor Author

I think the test failures are not related to my code changes? This show is related to Mac and Windows

@liamappelbe

Copy link
Copy Markdown
Contributor

I think the test failures are not related to my code changes? This show is related to Mac and Windows

For the mac failures, you made a lot of changes to things outside pure C++ support. I wouldn't be surprised if you did accidentally break an ObjC test. Your changes to global.dart look directly related to the mac failures. Not sure what's going on with the windows failures, you might need to investigate.

@Manar-Elhabbal7

Copy link
Copy Markdown
Contributor Author

I think the test failures are not related to my code changes? This show is related to Mac and Windows

For the mac failures, you made a lot of changes to things outside pure C++ support. I wouldn't be surprised if you did accidentally break an ObjC test. Your changes to global.dart look directly related to the mac failures. Not sure what's going on with the windows failures, you might need to investigate.

Yeah, Thanks alot @liamappelbe for the review i read the log and fixed it . it should now work well

@liamappelbe liamappelbe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you summarize the changes you had to make in code_generator/ in the PR description? From the PR description, readers would assume you only added more tests. You'll also need a changelog entry for those changes.

int id;
};

extern "C" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these cases all in extern "C"? C++ mode is supposed to support generating bindings for raw C++ functions, outside extern "C".

@Manar-Elhabbal7 Manar-Elhabbal7 Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. I tested removing extern "C", but the tests fail with undefined symbol: createAnimalPointer.

image

Without using extern "C", the Cpp compiler mangles the top-level function names. While ffigen generates wrapper glue for Cpp class methods, it doesn't generate glue for standalone top-level Cpp functions so dart will not find them at runtime

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. That's a bug we should fix too, but let's defer that to a later PR. Filed #3820

@Manar-Elhabbal7

Copy link
Copy Markdown
Contributor Author

@liamappelbe

Can you summarize the changes you had to make in code_generator/ in the PR description? From the PR description, readers would assume you only added more tests. You'll also need a changelog entry for those changes.

done

@liamappelbe

Copy link
Copy Markdown
Contributor

windows bot failures are unrelated.

@liamappelbe liamappelbe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good. Just some minor comments.

Can you also add tests to memory_edge_cases_test.dart or somewhere else to test that the new flows (struct fields, top level function args and returns, globals) have correct memory management?

@Manar-Elhabbal7

Copy link
Copy Markdown
Contributor Author

I think it better to make squash to the commits , once i am sure everything works as expected I will do :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ffigen] Test C++ class pointers in more situations

2 participants