Skip to content

Commit

Permalink
Migrate method filtering to the visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe committed Oct 29, 2024
1 parent 8a8603b commit 06d86a1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
18 changes: 16 additions & 2 deletions pkgs/ffigen/lib/src/code_generator/objc_methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import 'writer.dart';
final _logger = Logger('ffigen.code_generator.objc_methods');

mixin ObjCMethods {
final _methods = <String, ObjCMethod>{};
final _order = <String>[];
Map<String, ObjCMethod> _methods = <String, ObjCMethod>{};
List<String> _order = <String>[];

Iterable<ObjCMethod> get methods =>
_order.map((name) => _methods[name]).nonNulls;
Expand Down Expand Up @@ -97,6 +97,20 @@ mixin ObjCMethods {
parent: w.topLevelUniqueNamer);

void sortMethods() => _order.sort();

void filterMethods(bool predicate(ObjCMethod method)) {
final newOrder = <String>[];
final newMethods = <String, ObjCMethod>{};
for (final name in _order) {
final method = _methods[name];
if (method != null && predicate(method)) {
newMethods[name] = method;
newOrder.add(name);
}
}
_order = newOrder;
_methods = newMethods;
}
}

enum ObjCMethodKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ void _parseProperty(
return;
}

if (!config.objcInterfaces.shouldIncludeMember(itfDecl, fieldName)) {
return;
}

final dartDoc = getCursorDocComment(cursor);

final propertyAttributes =
Expand Down Expand Up @@ -221,10 +217,6 @@ ObjCMethod? parseObjCMethod(clang_types.CXCursor cursor, Declaration itfDecl,
return null;
}

if (!filters.shouldIncludeMember(itfDecl, methodName)) {
return null;
}

final method = ObjCMethod(
builtInFunctions: objCBuiltInFunctions,
originalName: methodName,
Expand Down
14 changes: 10 additions & 4 deletions pkgs/ffigen/lib/src/visitor/apply_config_filters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ class ApplyConfigFiltersVisitation extends Visitation {
_visitImpl(node, config.macroDecl);

@override
void visitObjCInterface(ObjCInterface node) =>
_visitImpl(node, config.objcInterfaces);
void visitObjCInterface(ObjCInterface node) {
node.filterMethods((method) =>
config.objcInterfaces.shouldIncludeMember(node, method.originalName));
_visitImpl(node, config.objcInterfaces);
}

@override
void visitObjCProtocol(ObjCProtocol node) =>
_visitImpl(node, config.objcProtocols);
void visitObjCProtocol(ObjCProtocol node) {
node.filterMethods((method) =>
config.objcProtocols.shouldIncludeMember(node, method.originalName));
_visitImpl(node, config.objcProtocols);
}

@override
void visitUnnamedEnumConstant(UnnamedEnumConstant node) =>
Expand Down
5 changes: 5 additions & 0 deletions pkgs/ffigen/test/native_objc_test/method_filtering_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ void main() {
expect(bindings, contains('includedProtocolMethod'));
expect(bindings, isNot(contains('excludedProtocolMethod')));
});

test('transitive deps', () {
expect(bindings, isNot(contains('TransitiveInterface')));
expect(bindings, isNot(contains('someTransitiveMethod')));
});
});
});
}
7 changes: 6 additions & 1 deletion pkgs/ffigen/test/native_objc_test/method_filtering_test.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

#import <Foundation/NSObject.h>

@interface TransitiveInterface : NSObject {}
+ (instancetype)someTransitiveMethod: (double)arg;
@end

@interface MethodFilteringTestInterface : NSObject {}
+ (instancetype)includedStaticMethod;
+ (instancetype)excludedStaticMethod;
- (instancetype)includedInstanceMethod: (int32_t)arg with: (int32_t)otherArg;
- (instancetype)excludedInstanceMethod: (int32_t)arg with: (int32_t)otherArg;
- (instancetype)excludedInstanceMethod: (int32_t)arg
with: (TransitiveInterface*)otherArg;
@property (assign) NSObject* includedProperty;
@property (assign) NSObject* excludedProperty;
@end
Expand Down

0 comments on commit 06d86a1

Please sign in to comment.