[ffigen] Bind C++ linkage functions and globals through extern "C" wrappers #3701
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.40) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 282 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Shallow Review Depth
shallow_review_depth
0.344
Diff Size
diff_size
0.338
Real PR Conversation Timeline
1 comments
"Review active on #3701"
general
weight: 0.35
liamappelbe
Description
In a C++ translation unit, free functions and globals declared outside an
extern "C"block have mangled symbols, but ffigen looked them up by their plain name. The lookup then failed at runtime.ffigen now determines each declaration's linkage while parsing. The innermost linkage spec wins, so
extern "C++"nested insideextern "C"counts as C++ linkage. For C++-linkage declarations, it writes anextern "C"wrapper into the generated.cppfile and binds to the wrapper:ffigen_<name>wrapper that forwards the call.ffigen_<name>accessor that returns the variable's address. Dart reads and writes the variable through that pointer, andexposeSymbolAddressworks too.Without C++ support there is no generated
.cppfor the wrappers, so ffigen logs a warning for each such declaration and keeps the old lookup.This also fixes how the generated C++ wrappers spell function pointer parameters: they got an extra
(int (*fn)(int)).Tests:
cpp_extern_c_testnow calls real native code covering each case. The newcpp_linkage_testchecks linkage detection with and without C++ support.Related Issues
Fixes #3666
PR Checklist
dart tool/ci.dart --alllocally and resolved all issues identified. This ensures the PR is formatted, has no lint errors, and ran all code generators. This applies to the packages part of the toplevelpubspec.yamlworkspace.CHANGELOG.mdfor the relevant packages. (Not needed for small changes such as doc typos).