Skip to content

Commit 77a5e41

Browse files
authored
[ffigen] Fix cpp method filtering (#3696)
1 parent cca065c commit 77a5e41

14 files changed

Lines changed: 451 additions & 9 deletions

‎pkgs/ffigen/lib/src/code_generator/cpp_class.dart‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ enum CppMethodKind { constructor, method }
1818
/// A method or constructor belonging to a C++ class.
1919
class CppMethod extends AstNode with HasLocalScope {
2020
final Symbol name;
21+
final Symbol cBindingSymbol;
2122
final String originalName;
23+
2224
final Type returnType;
2325
final List<Parameter> parameters;
2426
final bool isConstant;
@@ -30,6 +32,7 @@ class CppMethod extends AstNode with HasLocalScope {
3032

3133
CppMethod({
3234
required this.name,
35+
required this.cBindingSymbol,
3336
required this.originalName,
3437
required this.returnType,
3538
required this.parameters,
@@ -43,7 +46,8 @@ class CppMethod extends AstNode with HasLocalScope {
4346

4447
CppMethod cloneForClass(CppClass targetClass, CppClass baseClass) {
4548
return CppMethod(
46-
name: Symbol(
49+
name: name.clone(),
50+
cBindingSymbol: Symbol(
4751
'${targetClass.originalName}_$originalName',
4852
SymbolKind.method,
4953
),
@@ -70,6 +74,7 @@ class CppMethod extends AstNode with HasLocalScope {
7074
void visitChildren(Visitor visitor) {
7175
super.visitChildren(visitor);
7276
visitor.visit(name);
77+
visitor.visit(cBindingSymbol);
7378
visitor.visit(returnType);
7479
visitor.visitAll(parameters);
7580
visitor.visit(originatingClass);
@@ -136,6 +141,10 @@ class CppClass extends BindingType with HasLocalScope {
136141
methods.add(cloned);
137142
}
138143

144+
void filterMethods(bool Function(CppMethod method) predicate) {
145+
methods.retainWhere(predicate);
146+
}
147+
139148
@override
140149
BindingString toBindingString(Writer w) {
141150
final s = StringBuffer();
@@ -257,7 +266,7 @@ class $name implements $implementsClause {
257266
''');
258267

259268
for (final ctor in constructors) {
260-
final glueName = ctor.name.name;
269+
final glueName = ctor.cBindingSymbol.name;
261270
final privateName = '_$glueName';
262271

263272
final dartParams = dartParamList(ctor.parameters);
@@ -284,7 +293,7 @@ class $name implements $implementsClause {
284293
}
285294

286295
for (final method in classMethods) {
287-
final glue = '_${method.name.name}';
296+
final glue = '_${method.cBindingSymbol.name}';
288297
final dartReturn = method.returnType.getDartType(ctx);
289298
final dartParams = dartParamList(method.parameters);
290299
final localVars = LocalVariables(method.localScope);
@@ -314,14 +323,14 @@ class $name implements $implementsClause {
314323

315324
if (method.isStatic) {
316325
s.write('''\
317-
static $dartReturn ${method.originalName}($dartParams) {
326+
static $dartReturn ${method.name}($dartParams) {
318327
$decls
319328
$callLine
320329
}
321330
''');
322331
} else {
323332
s.write('''\
324-
$dartReturn ${method.originalName}($dartParams) {
333+
$dartReturn ${method.name}($dartParams) {
325334
if (_ptr == $ffiPrefix.nullptr) {
326335
throw StateError('This object has already been disposed.');
327336
}
@@ -370,7 +379,7 @@ class $name implements $implementsClause {
370379
}
371380

372381
for (final method in methods) {
373-
final symbol = method.name.name;
382+
final symbol = method.cBindingSymbol.name;
374383
final glue = '_$symbol';
375384

376385
final cReturn = method.isConstructor
@@ -428,7 +437,7 @@ FFIGEN_EXPORT void ${name}_delete($originalName* self) {
428437

429438
final methodBindings = methods
430439
.map((method) {
431-
final symbol = method.name.name;
440+
final symbol = method.cBindingSymbol.name;
432441

433442
final String returnTypeString;
434443
final String params;

‎pkgs/ffigen/lib/src/context.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Context {
3030
late final compilerOpts = computeCompilerOpts(config: config, logger: logger);
3131
final Scope rootScope = Scope.createRoot('root');
3232
final Scope rootObjCScope = Scope.createRoot('objc_root');
33+
final Scope rootCppScope = Scope.createRoot('cpp_root');
3334
late final ExtraSymbols extraSymbols;
3435
final String tmpDir;
3536

‎pkgs/ffigen/lib/src/header_parser/parser.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ void _nameAllSymbols(Context context, Set<Binding> bindings) {
313313

314314
context.rootScope.fillNames();
315315
context.rootObjCScope.fillNames();
316+
context.rootCppScope.fillNames();
316317
}
317318

318319
ExtraSymbols _createExtraSymbols(Context context) {

‎pkgs/ffigen/lib/src/header_parser/sub_parsers/classdecl_parser.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ void _parseAnyMethod(
187187
logger.fine(' ++++ ${kind.name}: $methodName (const=$isConst)');
188188
methods.add(
189189
CppMethod(
190-
name: Symbol(symbol, SymbolKind.method),
190+
name: Symbol(methodName, SymbolKind.method),
191+
cBindingSymbol: Symbol(symbol, SymbolKind.method),
191192
originalName: methodName,
192193
returnType: returnType,
193194
parameters: parameters,

‎pkgs/ffigen/lib/src/visitor/apply_config_filters.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ApplyConfigFiltersVisitation extends Visitation {
3636
@override
3737
void visitCppClass(CppClass node) {
3838
if (context.config.cpp == null) return;
39+
node.filterMethods((m) => m.isIncluded);
3940
_visitImpl(node, node.isIncluded);
4041
}
4142

‎pkgs/ffigen/lib/src/visitor/find_symbols.dart‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ class FindSymbolsVisitation extends Visitation {
8585

8686
@override
8787
void visitCppMethod(CppMethod node) {
88+
context.rootCppScope.add(node.cBindingSymbol);
8889
currentScope.add(node.name);
89-
visitInsideScope(node, node.localScope);
90+
insideScope(node.localScope, () {
91+
visitor.visit(node.returnType);
92+
visitor.visitAll(node.parameters);
93+
visitor.visit(node.originatingClass);
94+
});
9095
}
9196

9297
@override
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:ffigen/ffigen.dart';
6+
7+
FfiGenerator getConfig(Uri packageRoot) {
8+
final testDir = packageRoot.resolve('test/native_cpp_test/');
9+
10+
return FfiGenerator(
11+
output: Output(
12+
dart: DartOutput(path: Uri.file('cpp_filter_rename_test_bindings.dart')),
13+
style: const NativeExternalBindings(assetId: 'package:ffigen/cpp_test'),
14+
),
15+
input: Input(entryPoints: [testDir.resolve('cpp_filter_rename_test.h')]),
16+
cpp: const Cpp(),
17+
visitors: [
18+
Visitor(
19+
cppClass: (node) {
20+
node.isIncluded = {
21+
'MyClass',
22+
'OtherClass',
23+
}.contains(node.originalName);
24+
if (node.originalName == 'MyClass') {
25+
node.name = 'MyWidget';
26+
}
27+
},
28+
cppMethod: (node) {
29+
if (node.parent.originalName == 'MyClass' &&
30+
node.originalName != 'myMethod') {
31+
node.isIncluded = false;
32+
}
33+
if (node.originalName == 'myMethod') {
34+
node.name = 'greet';
35+
}
36+
},
37+
),
38+
],
39+
);
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:ffi';
6+
import 'dart:io';
7+
8+
import 'package:path/path.dart' as path;
9+
import 'package:test/test.dart';
10+
11+
import '../test_utils.dart';
12+
import 'cpp_filter_rename_test_bindings.dart';
13+
14+
void main() {
15+
group('cpp_filter_rename_test', () {
16+
test('renamed class and method exist and are accessible', () {
17+
expect(MyWidget, isNotNull);
18+
final widget = MyWidget.fromPointer(nullptr);
19+
expect(widget.greet, isNotNull);
20+
});
21+
22+
test(
23+
'filtered out class and method are omitted from generated bindings',
24+
() {
25+
final bindingsFile = File(
26+
path.join(
27+
packagePathForTests,
28+
'test',
29+
'native_cpp_test',
30+
'cpp_filter_rename_test_bindings.dart',
31+
),
32+
);
33+
final content = bindingsFile.readAsStringSync();
34+
35+
expect(content, contains('class MyWidget'));
36+
expect(content, contains('void greet()'));
37+
38+
expect(content, isNot(contains('FilteredOutClass')));
39+
expect(content, isNot(contains('filteredMethod')));
40+
},
41+
);
42+
});
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Test header for filtering and renaming C++ classes and methods.
6+
7+
// This class will be included, and its Dart name will be renamed to 'MyWidget'.
8+
class MyClass {
9+
public:
10+
MyClass() {}
11+
// This method will be included (renamed to 'greet').
12+
void myMethod() {}
13+
// This method will be filtered out via visitor filter.
14+
void filteredMethod() {}
15+
};
16+
17+
// This class will be included unchanged.
18+
class OtherClass {
19+
public:
20+
OtherClass() {}
21+
void method() {}
22+
};
23+
24+
// This class will be excluded via the include filter.
25+
class FilteredOutClass {
26+
public:
27+
FilteredOutClass() {}
28+
void unused() {}
29+
};

0 commit comments

Comments
 (0)