Skip to content

Commit 8882a55

Browse files
authored
[ffigen] Fix decl_decl_collision_test.dart (#1899)
1 parent 817c76e commit 8882a55

File tree

7 files changed

+116
-97
lines changed

7 files changed

+116
-97
lines changed

pkgs/ffigen/lib/src/code_generator/imports.dart

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ final objcPkgImport = LibraryImport(
120120
'objc', 'package:objective_c/objective_c.dart',
121121
importPathWhenImportedByPackageObjC: '../objective_c.dart');
122122
final self = LibraryImport('self', '');
123+
final allLibraries = [ffiImport, ffiPkgImport, objcPkgImport, self];
123124

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

pkgs/ffigen/lib/src/code_generator/library.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Library {
4444
required List<Binding> bindings,
4545
String? header,
4646
bool generateForPackageObjectiveC = false,
47-
List<LibraryImport>? libraryImports,
47+
List<LibraryImport> libraryImports = const <LibraryImport>[],
4848
bool silenceEnumWarning = false,
4949
List<String> nativeEntryPoints = const <String>[],
5050
}) {

pkgs/ffigen/lib/src/code_generator/writer.dart

+10-12
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class Writer {
128128
required this.noLookUpBindings,
129129
required String className,
130130
required this.nativeAssetId,
131-
List<LibraryImport>? additionalImports,
131+
List<LibraryImport> additionalImports = const <LibraryImport>[],
132132
this.classDocComment,
133133
this.header,
134134
required this.generateForPackageObjectiveC,
@@ -153,17 +153,15 @@ class Writer {
153153
);
154154

155155
/// Library imports prefix should be unique unique among all names.
156-
if (additionalImports != null) {
157-
for (final lib in additionalImports) {
158-
lib.prefix = _resolveNameConflict(
159-
name: lib.prefix,
160-
makeUnique: allLevelsUniqueNamer,
161-
markUsed: [
162-
_initialWrapperLevelUniqueNamer,
163-
_initialTopLevelUniqueNamer
164-
],
165-
);
166-
}
156+
for (final lib in [...additionalImports, ...allLibraries]) {
157+
lib.prefix = _resolveNameConflict(
158+
name: lib.prefix,
159+
makeUnique: allLevelsUniqueNamer,
160+
markUsed: [
161+
_initialWrapperLevelUniqueNamer,
162+
_initialTopLevelUniqueNamer
163+
],
164+
);
167165
}
168166

169167
/// [_lookupFuncIdentifier] should be unique in top level.

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:io';
88
import 'package:collection/collection.dart';
99
import 'package:ffi/ffi.dart';
1010
import 'package:logging/logging.dart';
11+
import 'package:meta/meta.dart';
1112

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

3435
return Library.fromConfig(
3536
config: config,
36-
bindings: _transformBindings(config, parseToBindings(config)),
37+
bindings: transformBindings(config, parseToBindings(config)),
3738
);
3839
}
3940

@@ -170,7 +171,8 @@ List<String> _findObjectiveCSysroot() {
170171
return [];
171172
}
172173

173-
List<Binding> _transformBindings(Config config, List<Binding> bindings) {
174+
@visibleForTesting
175+
List<Binding> transformBindings(Config config, List<Binding> bindings) {
174176
visit(CopyMethodsFromSuperTypesVisitation(), bindings);
175177
visit(FixOverriddenMethodsVisitation(), bindings);
176178
visit(FillMethodDependenciesVisitation(), bindings);

pkgs/ffigen/test/collision_tests/decl_decl_collision_test.dart

+59-42
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:ffigen/src/code_generator.dart';
6+
import 'package:ffigen/src/config_provider/config.dart';
7+
import 'package:ffigen/src/header_parser/parser.dart';
68
import 'package:logging/logging.dart';
79
import 'package:test/test.dart';
810
import '../test_utils.dart';
@@ -13,50 +15,65 @@ void main() {
1315
logWarnings(Level.SEVERE);
1416
});
1517
test('declaration conflict', () {
16-
final library = Library(name: 'Bindings', bindings: [
17-
Struct(name: 'TestStruct'),
18-
Struct(name: 'TestStruct'),
19-
EnumClass(name: 'TestEnum'),
20-
EnumClass(name: 'TestEnum'),
21-
Func(
22-
name: 'testFunc',
23-
returnType: NativeType(SupportedNativeType.voidType)),
24-
Func(
25-
name: 'testFunc',
26-
returnType: NativeType(SupportedNativeType.voidType)),
27-
Constant(
28-
originalName: 'Test_Macro',
29-
name: 'Test_Macro',
30-
rawType: 'int',
31-
rawValue: '0',
32-
),
33-
Constant(
34-
originalName: 'Test_Macro',
35-
name: 'Test_Macro',
36-
rawType: 'int',
37-
rawValue: '0',
38-
),
39-
Typealias(
40-
name: 'testAlias', type: NativeType(SupportedNativeType.voidType)),
41-
Typealias(
42-
name: 'testAlias', type: NativeType(SupportedNativeType.voidType)),
18+
final config = Config(
19+
entryPoints: [],
20+
output: Uri(),
21+
functionDecl: DeclarationFilters.includeAll,
22+
structDecl: DeclarationFilters.includeAll,
23+
enumClassDecl: DeclarationFilters.includeAll,
24+
globals: DeclarationFilters.includeAll,
25+
macroDecl: DeclarationFilters.includeAll,
26+
typedefs: DeclarationFilters.includeAll,
27+
);
28+
final library = Library(
29+
name: 'Bindings',
30+
bindings: transformBindings(config, [
31+
Struct(name: 'TestStruct'),
32+
Struct(name: 'TestStruct'),
33+
EnumClass(name: 'TestEnum'),
34+
EnumClass(name: 'TestEnum'),
35+
Func(
36+
name: 'testFunc',
37+
returnType: NativeType(SupportedNativeType.voidType)),
38+
Func(
39+
name: 'testFunc',
40+
returnType: NativeType(SupportedNativeType.voidType)),
41+
MacroConstant(
42+
originalName: 'Test_Macro',
43+
name: 'Test_Macro',
44+
rawType: 'int',
45+
rawValue: '0',
46+
),
47+
MacroConstant(
48+
originalName: 'Test_Macro',
49+
name: 'Test_Macro',
50+
rawType: 'int',
51+
rawValue: '0',
52+
),
53+
Typealias(
54+
name: 'testAlias',
55+
type: NativeType(SupportedNativeType.voidType)),
56+
Typealias(
57+
name: 'testAlias',
58+
type: NativeType(SupportedNativeType.voidType)),
4359

44-
/// Conflicts across declarations.
45-
Struct(name: 'testCrossDecl'),
46-
Func(
47-
name: 'testCrossDecl',
48-
returnType: NativeType(SupportedNativeType.voidType)),
49-
Constant(name: 'testCrossDecl', rawValue: '0', rawType: 'int'),
50-
EnumClass(name: 'testCrossDecl'),
51-
Typealias(
52-
name: 'testCrossDecl',
53-
type: NativeType(SupportedNativeType.voidType)),
60+
/// Conflicts across declarations.
61+
Struct(name: 'testCrossDecl'),
62+
Func(
63+
name: 'testCrossDecl',
64+
returnType: NativeType(SupportedNativeType.voidType)),
65+
MacroConstant(name: 'testCrossDecl', rawValue: '0', rawType: 'int'),
66+
EnumClass(name: 'testCrossDecl'),
67+
Typealias(
68+
name: 'testCrossDecl',
69+
type: NativeType(SupportedNativeType.voidType)),
5470

55-
/// Conflicts with ffi library prefix, name of prefix is changed.
56-
Struct(name: 'ffi'),
57-
Func(
58-
name: 'ffi1', returnType: NativeType(SupportedNativeType.voidType)),
59-
]);
71+
/// Conflicts with ffi library prefix, name of prefix is changed.
72+
Struct(name: 'ffi'),
73+
Func(
74+
name: 'ffi1',
75+
returnType: NativeType(SupportedNativeType.voidType)),
76+
]));
6077
matchLibraryWithExpected(
6178
library, 'decl_decl_collision_test_output.dart', [
6279
'test',

pkgs/ffigen/test/collision_tests/expected_bindings/_expected_decl_decl_collision_bindings.dart

+27-26
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
//
33
// Generated by `package:ffigen`.
44
// ignore_for_file: type=lint
5-
import 'dart:ffi' as ffi;
5+
import 'dart:ffi' as ffi2;
66

77
class Bindings {
88
/// Holds the symbol lookup function.
9-
final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
9+
final ffi2.Pointer<T> Function<T extends ffi2.NativeType>(String symbolName)
1010
_lookup;
1111

1212
/// The symbols are looked up in [dynamicLibrary].
13-
Bindings(ffi.DynamicLibrary dynamicLibrary) : _lookup = dynamicLibrary.lookup;
13+
Bindings(ffi2.DynamicLibrary dynamicLibrary)
14+
: _lookup = dynamicLibrary.lookup;
1415

1516
/// The symbols are looked up with [lookup].
1617
Bindings.fromLookup(
17-
ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
18+
ffi2.Pointer<T> Function<T extends ffi2.NativeType>(String symbolName)
1819
lookup)
1920
: _lookup = lookup;
2021

@@ -23,58 +24,58 @@ class Bindings {
2324
}
2425

2526
late final _testFuncPtr =
26-
_lookup<ffi.NativeFunction<ffi.Void Function()>>('testFunc');
27+
_lookup<ffi2.NativeFunction<ffi2.Void Function()>>('testFunc');
2728
late final _testFunc = _testFuncPtr.asFunction<void Function()>();
2829

29-
void testFunc() {
30+
void testFunc1() {
3031
return _testFunc1();
3132
}
3233

33-
late final _testFuncPtr1 =
34-
_lookup<ffi.NativeFunction<ffi.Void Function()>>('testFunc');
35-
late final _testFunc1 = _testFuncPtr1.asFunction<void Function()>();
34+
late final _testFunc1Ptr =
35+
_lookup<ffi2.NativeFunction<ffi2.Void Function()>>('testFunc');
36+
late final _testFunc1 = _testFunc1Ptr.asFunction<void Function()>();
3637

37-
void testCrossDecl() {
38-
return _testCrossDecl();
38+
void testCrossDecl1() {
39+
return _testCrossDecl1();
3940
}
4041

41-
late final _testCrossDeclPtr =
42-
_lookup<ffi.NativeFunction<ffi.Void Function()>>('testCrossDecl');
43-
late final _testCrossDecl = _testCrossDeclPtr.asFunction<void Function()>();
42+
late final _testCrossDecl1Ptr =
43+
_lookup<ffi2.NativeFunction<ffi2.Void Function()>>('testCrossDecl');
44+
late final _testCrossDecl1 = _testCrossDecl1Ptr.asFunction<void Function()>();
4445

4546
void ffi1() {
4647
return _ffi1();
4748
}
4849

4950
late final _ffi1Ptr =
50-
_lookup<ffi.NativeFunction<ffi.Void Function()>>('ffi1');
51+
_lookup<ffi2.NativeFunction<ffi2.Void Function()>>('ffi1');
5152
late final _ffi1 = _ffi1Ptr.asFunction<void Function()>();
5253
}
5354

54-
final class TestStruct extends ffi.Opaque {}
55+
final class TestStruct extends ffi2.Opaque {}
5556

56-
final class TestStruct extends ffi.Opaque {}
57+
final class TestStruct1 extends ffi2.Opaque {}
5758

5859
sealed class TestEnum {}
5960

60-
sealed class TestEnum {}
61+
sealed class TestEnum1 {}
6162

6263
const int Test_Macro = 0;
6364

64-
const int Test_Macro = 0;
65+
const int Test_Macro1 = 0;
6566

66-
typedef testAlias = ffi.Void;
67+
typedef testAlias = ffi2.Void;
6768
typedef DarttestAlias = void;
68-
typedef testAlias = ffi.Void;
69+
typedef testAlias1 = ffi2.Void;
6970
typedef DarttestAlias1 = void;
7071

71-
final class testCrossDecl extends ffi.Opaque {}
72+
final class testCrossDecl extends ffi2.Opaque {}
7273

73-
const int testCrossDecl = 0;
74+
const int testCrossDecl2 = 0;
7475

75-
sealed class testCrossDecl {}
76+
sealed class testCrossDecl3 {}
7677

77-
typedef testCrossDecl = ffi.Void;
78+
typedef testCrossDecl4 = ffi2.Void;
7879
typedef DarttestCrossDecl = void;
7980

80-
final class ffi extends ffi.Opaque {}
81+
final class ffi extends ffi2.Opaque {}

0 commit comments

Comments
 (0)