Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ffigen] Fix decl_decl_collision_test.dart #1899

Merged
merged 6 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/ffigen/lib/src/code_generator/imports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ final objcPkgImport = LibraryImport(
'objc', 'package:objective_c/objective_c.dart',
importPathWhenImportedByPackageObjC: '../objective_c.dart');
final self = LibraryImport('self', '');
final allLibraries = [ffiImport, ffiPkgImport, objcPkgImport, self];

final voidType = ImportedType(ffiImport, 'Void', 'void', 'void');

Expand Down
2 changes: 1 addition & 1 deletion pkgs/ffigen/lib/src/code_generator/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Library {
required List<Binding> bindings,
String? header,
bool generateForPackageObjectiveC = false,
List<LibraryImport>? libraryImports,
List<LibraryImport> libraryImports = const <LibraryImport>[],
bool silenceEnumWarning = false,
List<String> nativeEntryPoints = const <String>[],
}) {
Expand Down
22 changes: 10 additions & 12 deletions pkgs/ffigen/lib/src/code_generator/writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Writer {
required this.noLookUpBindings,
required String className,
required this.nativeAssetId,
List<LibraryImport>? additionalImports,
List<LibraryImport> additionalImports = const <LibraryImport>[],
this.classDocComment,
this.header,
required this.generateForPackageObjectiveC,
Expand All @@ -153,17 +153,15 @@ class Writer {
);

/// Library imports prefix should be unique unique among all names.
if (additionalImports != null) {
for (final lib in additionalImports) {
lib.prefix = _resolveNameConflict(
name: lib.prefix,
makeUnique: allLevelsUniqueNamer,
markUsed: [
_initialWrapperLevelUniqueNamer,
_initialTopLevelUniqueNamer
],
);
}
for (final lib in [...additionalImports, ...allLibraries]) {
lib.prefix = _resolveNameConflict(
name: lib.prefix,
makeUnique: allLevelsUniqueNamer,
markUsed: [
_initialWrapperLevelUniqueNamer,
_initialTopLevelUniqueNamer
],
);
}

/// [_lookupFuncIdentifier] should be unique in top level.
Expand Down
6 changes: 4 additions & 2 deletions pkgs/ffigen/lib/src/header_parser/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:io';
import 'package:collection/collection.dart';
import 'package:ffi/ffi.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';

import '../code_generator.dart';
import '../code_generator/utils.dart';
Expand All @@ -33,7 +34,7 @@ Library parse(Config config) {

return Library.fromConfig(
config: config,
bindings: _transformBindings(config, parseToBindings(config)),
bindings: transformBindings(config, parseToBindings(config)),
);
}

Expand Down Expand Up @@ -170,7 +171,8 @@ List<String> _findObjectiveCSysroot() {
return [];
}

List<Binding> _transformBindings(Config config, List<Binding> bindings) {
@visibleForTesting
List<Binding> transformBindings(Config config, List<Binding> bindings) {
visit(CopyMethodsFromSuperTypesVisitation(), bindings);
visit(FixOverriddenMethodsVisitation(), bindings);
visit(FillMethodDependenciesVisitation(), bindings);
Expand Down
101 changes: 59 additions & 42 deletions pkgs/ffigen/test/collision_tests/decl_decl_collision_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:ffigen/src/code_generator.dart';
import 'package:ffigen/src/config_provider/config.dart';
import 'package:ffigen/src/header_parser/parser.dart';
import 'package:logging/logging.dart';
import 'package:test/test.dart';
import '../test_utils.dart';
Expand All @@ -13,50 +15,65 @@ void main() {
logWarnings(Level.SEVERE);
});
test('declaration conflict', () {
final library = Library(name: 'Bindings', bindings: [
Struct(name: 'TestStruct'),
Struct(name: 'TestStruct'),
EnumClass(name: 'TestEnum'),
EnumClass(name: 'TestEnum'),
Func(
name: 'testFunc',
returnType: NativeType(SupportedNativeType.voidType)),
Func(
name: 'testFunc',
returnType: NativeType(SupportedNativeType.voidType)),
Constant(
originalName: 'Test_Macro',
name: 'Test_Macro',
rawType: 'int',
rawValue: '0',
),
Constant(
originalName: 'Test_Macro',
name: 'Test_Macro',
rawType: 'int',
rawValue: '0',
),
Typealias(
name: 'testAlias', type: NativeType(SupportedNativeType.voidType)),
Typealias(
name: 'testAlias', type: NativeType(SupportedNativeType.voidType)),
final config = Config(
entryPoints: [],
output: Uri(),
functionDecl: DeclarationFilters.includeAll,
structDecl: DeclarationFilters.includeAll,
enumClassDecl: DeclarationFilters.includeAll,
globals: DeclarationFilters.includeAll,
macroDecl: DeclarationFilters.includeAll,
typedefs: DeclarationFilters.includeAll,
);
final library = Library(
name: 'Bindings',
bindings: transformBindings(config, [
Struct(name: 'TestStruct'),
Struct(name: 'TestStruct'),
EnumClass(name: 'TestEnum'),
EnumClass(name: 'TestEnum'),
Func(
name: 'testFunc',
returnType: NativeType(SupportedNativeType.voidType)),
Func(
name: 'testFunc',
returnType: NativeType(SupportedNativeType.voidType)),
MacroConstant(
originalName: 'Test_Macro',
name: 'Test_Macro',
rawType: 'int',
rawValue: '0',
),
MacroConstant(
originalName: 'Test_Macro',
name: 'Test_Macro',
rawType: 'int',
rawValue: '0',
),
Typealias(
name: 'testAlias',
type: NativeType(SupportedNativeType.voidType)),
Typealias(
name: 'testAlias',
type: NativeType(SupportedNativeType.voidType)),

/// Conflicts across declarations.
Struct(name: 'testCrossDecl'),
Func(
name: 'testCrossDecl',
returnType: NativeType(SupportedNativeType.voidType)),
Constant(name: 'testCrossDecl', rawValue: '0', rawType: 'int'),
EnumClass(name: 'testCrossDecl'),
Typealias(
name: 'testCrossDecl',
type: NativeType(SupportedNativeType.voidType)),
/// Conflicts across declarations.
Struct(name: 'testCrossDecl'),
Func(
name: 'testCrossDecl',
returnType: NativeType(SupportedNativeType.voidType)),
MacroConstant(name: 'testCrossDecl', rawValue: '0', rawType: 'int'),
EnumClass(name: 'testCrossDecl'),
Typealias(
name: 'testCrossDecl',
type: NativeType(SupportedNativeType.voidType)),

/// Conflicts with ffi library prefix, name of prefix is changed.
Struct(name: 'ffi'),
Func(
name: 'ffi1', returnType: NativeType(SupportedNativeType.voidType)),
]);
/// Conflicts with ffi library prefix, name of prefix is changed.
Struct(name: 'ffi'),
Func(
name: 'ffi1',
returnType: NativeType(SupportedNativeType.voidType)),
]));
matchLibraryWithExpected(
library, 'decl_decl_collision_test_output.dart', [
'test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
//
// Generated by `package:ffigen`.
// ignore_for_file: type=lint
import 'dart:ffi' as ffi;
import 'dart:ffi' as ffi2;

class Bindings {
/// Holds the symbol lookup function.
final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
final ffi2.Pointer<T> Function<T extends ffi2.NativeType>(String symbolName)
_lookup;

/// The symbols are looked up in [dynamicLibrary].
Bindings(ffi.DynamicLibrary dynamicLibrary) : _lookup = dynamicLibrary.lookup;
Bindings(ffi2.DynamicLibrary dynamicLibrary)
: _lookup = dynamicLibrary.lookup;

/// The symbols are looked up with [lookup].
Bindings.fromLookup(
ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
ffi2.Pointer<T> Function<T extends ffi2.NativeType>(String symbolName)
lookup)
: _lookup = lookup;

Expand All @@ -23,58 +24,58 @@ class Bindings {
}

late final _testFuncPtr =
_lookup<ffi.NativeFunction<ffi.Void Function()>>('testFunc');
_lookup<ffi2.NativeFunction<ffi2.Void Function()>>('testFunc');
late final _testFunc = _testFuncPtr.asFunction<void Function()>();

void testFunc() {
void testFunc1() {
return _testFunc1();
}

late final _testFuncPtr1 =
_lookup<ffi.NativeFunction<ffi.Void Function()>>('testFunc');
late final _testFunc1 = _testFuncPtr1.asFunction<void Function()>();
late final _testFunc1Ptr =
_lookup<ffi2.NativeFunction<ffi2.Void Function()>>('testFunc');
late final _testFunc1 = _testFunc1Ptr.asFunction<void Function()>();

void testCrossDecl() {
return _testCrossDecl();
void testCrossDecl1() {
return _testCrossDecl1();
}

late final _testCrossDeclPtr =
_lookup<ffi.NativeFunction<ffi.Void Function()>>('testCrossDecl');
late final _testCrossDecl = _testCrossDeclPtr.asFunction<void Function()>();
late final _testCrossDecl1Ptr =
_lookup<ffi2.NativeFunction<ffi2.Void Function()>>('testCrossDecl');
late final _testCrossDecl1 = _testCrossDecl1Ptr.asFunction<void Function()>();

void ffi1() {
return _ffi1();
}

late final _ffi1Ptr =
_lookup<ffi.NativeFunction<ffi.Void Function()>>('ffi1');
_lookup<ffi2.NativeFunction<ffi2.Void Function()>>('ffi1');
late final _ffi1 = _ffi1Ptr.asFunction<void Function()>();
}

final class TestStruct extends ffi.Opaque {}
final class TestStruct extends ffi2.Opaque {}

final class TestStruct extends ffi.Opaque {}
final class TestStruct1 extends ffi2.Opaque {}

sealed class TestEnum {}

sealed class TestEnum {}
sealed class TestEnum1 {}

const int Test_Macro = 0;

const int Test_Macro = 0;
const int Test_Macro1 = 0;

typedef testAlias = ffi.Void;
typedef testAlias = ffi2.Void;
typedef DarttestAlias = void;
typedef testAlias = ffi.Void;
typedef testAlias1 = ffi2.Void;
typedef DarttestAlias1 = void;

final class testCrossDecl extends ffi.Opaque {}
final class testCrossDecl extends ffi2.Opaque {}

const int testCrossDecl = 0;
const int testCrossDecl2 = 0;

sealed class testCrossDecl {}
sealed class testCrossDecl3 {}

typedef testCrossDecl = ffi.Void;
typedef testCrossDecl4 = ffi2.Void;
typedef DarttestCrossDecl = void;

final class ffi extends ffi.Opaque {}
final class ffi extends ffi2.Opaque {}
Loading
Loading