Skip to content

Commit

Permalink
More tests, and fix some edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe committed Feb 13, 2025
1 parent c29df4d commit 4f38030
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 16 deletions.
2 changes: 2 additions & 0 deletions pkgs/ffigen/lib/src/code_generator/objc_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ObjCInterface extends BindingType with ObjCMethods {
final protocols = <ObjCProtocol>[];
final categories = <ObjCCategory>[];
final subtypes = <ObjCInterface>[];
final bool unavailable;

@override
final ObjCBuiltInFunctions builtInFunctions;
Expand All @@ -34,6 +35,7 @@ class ObjCInterface extends BindingType with ObjCMethods {
String? lookupName,
super.dartDoc,
required this.builtInFunctions,
this.unavailable = false,
}) : lookupName = lookupName ?? originalName,
super(name: name ?? originalName) {
classObject = ObjCInternalGlobal('_class_$originalName',
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/lib/src/code_generator/objc_protocol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ObjCProtocol extends BindingType with ObjCMethods {
final ObjCInternalGlobal _protocolPointer;
late final ObjCInternalGlobal _conformsTo;
late final ObjCMsgSendFunc _conformsToMsgSend;
final bool unavailable;

// Filled by ListBindingsVisitation.
bool generateAsStub = false;
Expand All @@ -29,6 +30,7 @@ class ObjCProtocol extends BindingType with ObjCMethods {
String? lookupName,
super.dartDoc,
required this.builtInFunctions,
this.unavailable = false,
}) : lookupName = lookupName ?? originalName,
_protocolPointer = ObjCInternalGlobal(
'_protocol_$originalName',
Expand Down
3 changes: 3 additions & 0 deletions pkgs/ffigen/lib/src/header_parser/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ List<Binding> transformBindings(Config config, List<Binding> bindings) {
config, included, directlyIncluded),
included)
.directTransitives;
// print("\n\nDirectly Included:\n ${directlyIncluded.whereType<ObjCCategory>().map((p) => '${p.name}').join('\n ')}");
// print("\n\nTransitives:\n ${transitives.whereType<ObjCCategory>().map((p) => '${p.name}').join('\n ')}");
// print("\n\nDirect Transitives:\n ${directTransitives.whereType<ObjCCategory>().map((p) => '${p.name}').join('\n ')}");

final finalBindings = visit(
ListBindingsVisitation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ Type? parseObjCInterfaceDeclaration(clang_types.CXCursor cursor) {
final itfUsr = cursor.usr();
final itfName = cursor.spelling();
final decl = Declaration(usr: itfUsr, originalName: itfName);

final report = getApiAvailability(cursor);
if (report.availability == Availability.none) {
_logger.info('Omitting deprecated interface $itfName');
return null;
}

_logger.fine('++++ Adding ObjC interface: '
'Name: $itfName, ${cursor.completeStringRepr()}');
Expand All @@ -40,6 +35,7 @@ Type? parseObjCInterfaceDeclaration(clang_types.CXCursor cursor) {
dartDoc: getCursorDocComment(cursor,
fallbackComment: itfName, availability: report.dartDoc),
builtInFunctions: objCBuiltInFunctions,
unavailable: report.availability == Availability.none,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ ObjCProtocol? parseObjCProtocolDeclaration(clang_types.CXCursor cursor) {
}

final report = getApiAvailability(cursor);
if (report.availability == Availability.none) {
_logger.info('Omitting deprecated protocol $name');
return null;
}

_logger.fine('++++ Adding ObjC protocol: '
'Name: $name, ${cursor.completeStringRepr()}');
Expand All @@ -59,8 +55,9 @@ ObjCProtocol? parseObjCProtocolDeclaration(clang_types.CXCursor cursor) {
originalName: name,
name: config.objcProtocols.rename(decl),
lookupName: applyModulePrefix(name, config.protocolModule(decl)),
dartDoc: getCursorDocComment(cursor, availability: report.dartDoc),
dartDoc: getCursorDocComment(cursor, fallbackComment: name, availability: report.dartDoc),
builtInFunctions: objCBuiltInFunctions,
unavailable: report.availability == Availability.none,
);

// Make sure to add the protocol to the index before parsing the AST, to break
Expand Down
4 changes: 4 additions & 0 deletions pkgs/ffigen/lib/src/visitor/apply_config_filters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ApplyConfigFiltersVisitation extends Visitation {

@override
void visitObjCInterface(ObjCInterface node) {
if (node.unavailable) return;

node.filterMethods(
(m) => config.objcInterfaces.shouldIncludeMember(node, m.originalName));
_visitImpl(node, config.objcInterfaces);
Expand All @@ -61,6 +63,8 @@ class ApplyConfigFiltersVisitation extends Visitation {

@override
void visitObjCProtocol(ObjCProtocol node) {
if (node.unavailable) return;

node.filterMethods((m) {
// TODO(https://github.com/dart-lang/native/issues/1149): Support class
// methods on protocols if there's a use case. For now filter them. We
Expand Down
12 changes: 6 additions & 6 deletions pkgs/ffigen/lib/src/visitor/list_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ class ListBindingsVisitation extends Visitation {

@override
void visitObjCInterface(ObjCInterface node) {
if (!_visitImpl(
bool omit = node.unavailable || !_visitImpl(
node,
config.includeTransitiveObjCInterfaces
? _IncludeBehavior.configOrTransitive
: _IncludeBehavior.configOnly) &&
directTransitives.contains(node)) {
: _IncludeBehavior.configOnly);
if (omit && directTransitives.contains(node)) {
node.generateAsStub = true;
bindings.add(node);
}
Expand All @@ -80,12 +80,12 @@ class ListBindingsVisitation extends Visitation {

@override
void visitObjCProtocol(ObjCProtocol node) {
if (!_visitImpl(
bool omit = node.unavailable || !_visitImpl(
node,
config.includeTransitiveObjCProtocols
? _IncludeBehavior.configOrTransitive
: _IncludeBehavior.configOnly) &&
directTransitives.contains(node)) {
: _IncludeBehavior.configOnly);
if (omit && directTransitives.contains(node)) {
node.generateAsStub = true;
bindings.add(node);
}
Expand Down
Loading

0 comments on commit 4f38030

Please sign in to comment.