Skip to content
Open
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
42 changes: 26 additions & 16 deletions pkgs/ffigen/lib/src/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,31 @@ class Context {
final Scope rootScope = Scope.createRoot('root');
final Scope rootObjCScope = Scope.createRoot('objc_root');
late final ExtraSymbols extraSymbols;

Context(this.logger, FfiGenerator generator, {Uri? libclangDylib})
: config = Config(generator),
cursorIndex = CursorIndex(logger) {
objCBuiltInFunctions = ObjCBuiltInFunctions(
this,
// ignore: deprecated_member_use_from_same_package
generator.objectiveC?.generateForPackageObjectiveC ?? false,
);
final libclangDylibPath =
// ignore: deprecated_member_use_from_same_package
generator.libclangDylib?.toFilePath() ??
libclangDylib?.toFilePath() ??
findDylibAtDefaultLocations(logger);
_clang ??= Clang(DynamicLibrary.open(libclangDylibPath));
}
final String tmpDir;

Context(
this.logger,
FfiGenerator generator, {
Uri? libclangDylib,
String? tmpDir, // optional parameter
}) : config = Config(generator),
cursorIndex = CursorIndex(logger),
tmpDir = tmpDir ?? Directory.systemTemp.createTempSync('ffigen temp dir ').path // default temp dir with spaces
{
// Initialize compiler options
compilerOpts = config.headers.compilerOptions ?? defaultCompilerOpts(logger);

objCBuiltInFunctions = ObjCBuiltInFunctions(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's with all these unrelated diffs? You've removed some important stuff.

this,
generator.objectiveC?.generateForPackageObjectiveC ?? false,
);

final libclangDylibPath =
generator.libclangDylib?.toFilePath() ??
libclangDylib?.toFilePath() ??
findDylibAtDefaultLocations(logger);

_clang ??= Clang(DynamicLibrary.open(libclangDylibPath));
}

/// The clang bindings.
Expand Down Expand Up @@ -123,3 +132,4 @@ typedef ExtraSymbols = ({
// TODO(https://github.com/dart-lang/native/issues/1259): Make this nullable.
Symbol symbolAddressVariableName,
});
}
13 changes: 0 additions & 13 deletions pkgs/ffigen/lib/src/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,6 @@ const synthUsrChar = '~';
const ffiNative = 'ffi-native';
const ffiNativeAsset = 'asset-id';

Directory? _tmpDir;

/// A path to a unique temporary directory that should be used for files meant
/// to be discarded after the current execution is finished.
String get tmpDir {
if (Platform.environment.containsKey('TEST_TMPDIR')) {
return Platform.environment['TEST_TMPDIR']!;
}

_tmpDir ??= Directory.systemTemp.createTempSync();
return _tmpDir!.path;
}

const ffigenJsonSchemaIndent = ' ';
const ffigenJsonSchemaId = 'https://json.schemastore.org/ffigen';
const ffigenJsonSchemaFileName = 'ffigen.schema.json';
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,8 @@ void main() {

/// Utility to match expected bindings to the generated bindings.
void _matchLib(Library lib, String testName) {
matchLibraryWithExpected(lib, 'code_generator_test_${testName}_output.dart', [
final context = testContext();
matchLibraryWithExpected(context, lib, 'code_generator_test_${testName}_output.dart', [
'test',
'code_generator_tests',
'expected_bindings',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ void main() {
),
], context),
);
final context = testContext();
matchLibraryWithExpected(
context,
library,
'decl_decl_collision_test_output.dart',
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ void main() {
);
});
test('declaration and symbol address conflict', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'collision_test_decl_symbol_address_collision_output.dart',
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ ${strings.headers}:
});

test('Expected bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'decl_type_name_collision_test_output.dart',
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import '../test_utils.dart';
void main() {
group('reserved_keyword_collision_test', () {
test('reserved keyword collision', () {
final context = testContext();
final library = parser.parse(
testContext(
FfiGenerator(
Expand Down Expand Up @@ -45,6 +46,7 @@ void main() {
),
);
matchLibraryWithExpected(
context,
library,
'reserved_keyword_collision_test_output.dart',
[
Expand Down
3 changes: 2 additions & 1 deletion pkgs/ffigen/test/example_tests/cjson_example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ void main() {
final config = testConfigFromPath(
path.join(packagePathForTests, 'example', 'c_json', 'config.yaml'),
);
final context = testContext(config);
final library = parse(testContext(config));

matchLibraryWithExpected(library, 'example_c_json.dart', [
matchLibraryWithExpected(context, library, 'example_c_json.dart', [
config.output.dartFile.toFilePath(),
]);
});
Expand Down
5 changes: 3 additions & 2 deletions pkgs/ffigen/test/example_tests/ffinative_example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ void main() {
final config = testConfigFromPath(
path.join(packagePathForTests, 'example', 'ffinative', 'config.yaml'),
);
final library = parse(testContext(config));
final context = testContext(config);
final library = parse(context);

matchLibraryWithExpected(library, 'example_ffinative.dart', [
matchLibraryWithExpected(context, library, 'example_ffinative.dart', [
config.output.dartFile.toFilePath(),
]);
});
Expand Down
2 changes: 1 addition & 1 deletion pkgs/ffigen/test/example_tests/libclang_example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void main() {
final context = testContext(generator);
final library = parse(context);

matchLibraryWithExpected(library, 'example_libclang.dart', [
matchLibraryWithExpected(context, library, 'example_libclang.dart', [
generator.output.dartFile.toFilePath(),
]);
});
Expand Down
10 changes: 6 additions & 4 deletions pkgs/ffigen/test/example_tests/shared_bindings_example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ void main() {
'a_shared_base.yaml',
),
);
final library = parse(testContext(config));

matchLibraryWithExpected(library, 'example_shared_bindings.dart', [
final context = testContext(config);
final library = parse(context);
matchLibraryWithExpected(context, library, 'example_shared_bindings.dart', [
config.output.dartFile.toFilePath(),
]);
});
Expand All @@ -37,8 +37,10 @@ void main() {
'base.yaml',
),
);
final library = parse(testContext(config));
final context = testContext(config);
final library = parse(context);
matchLibrarySymbolFileWithExpected(
context,
library,
'example_shared_bindings.yaml',
[config.output.symbolFile!.output.toFilePath()],
Expand Down
5 changes: 3 additions & 2 deletions pkgs/ffigen/test/example_tests/simple_example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ void main() {
final config = testConfigFromPath(
path.join(packagePathForTests, 'example', 'simple', 'config.yaml'),
);
final library = parse(testContext(config));
final context = testContext(config);
final library = parse(context);

matchLibraryWithExpected(library, 'example_simple.dart', [
matchLibraryWithExpected(context, library, 'example_simple.dart', [
config.output.dartFile.toFilePath(),
]);
});
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/test/header_parser_tests/comment_markup_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ ${strings.comments}:
});

test('Expected bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_comment_markup_test_output.dart',
[
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/test/header_parser_tests/dart_handle_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ ${strings.headers}:
);
});
test('Expected Bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_dart_handle_test_output.dart',
[
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/test/header_parser_tests/enum_int_mimic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ ${strings.ignoreSourceErrors}: true
});

test('Expected bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_enum_int_mimic_test_output.dart',
[
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/test/header_parser_tests/forward_decl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ ${strings.ignoreSourceErrors}: true
});

test('Expected bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_forward_decl_test_output.dart',
[
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/test/header_parser_tests/functions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ ${strings.functions}:
);
});
test('Expected Bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_functions_test_output.dart',
[
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/test/header_parser_tests/imported_types_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ ${strings.headers}:
);
});
test('Expected Bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_imported_types_test_output.dart',
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ ${strings.headers}:
});

test('Expected bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_native_func_typedef_test_output.dart',
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ ${strings.unions}:
);
});
test('Expected bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_opaque_dependencies_test_output.dart',
[
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/test/header_parser_tests/packed_structs_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ ${strings.headers}:
});

test('Expected bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_packed_structs_test_output.dart',
[
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/test/header_parser_tests/regress_384_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ ${strings.headers}:
});

test('Expected bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_regress_384_test_output.dart',
[
Expand Down
3 changes: 2 additions & 1 deletion pkgs/ffigen/test/header_parser_tests/sort_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ void main() {
);
});
test('Expected Bindings', () {
matchLibraryWithExpected(actual, 'header_parser_sort_test_output.dart', [
final context = testContext();
matchLibraryWithExpected(context, actual, 'header_parser_sort_test_output.dart', [
'test',
'header_parser_tests',
'expected_bindings',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ ${strings.headers}:
});

test('Expected bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_struct_fptr_fields_output.dart',
[
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/test/header_parser_tests/typedef_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ ${strings.preamble}: |
});

test('Expected Bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_typedef_test_output.dart',
[
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/test/header_parser_tests/unions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ ${strings.ignoreSourceErrors}: true
});

test('Expected bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_unions_test_output.dart',
[
Expand Down
2 changes: 2 additions & 0 deletions pkgs/ffigen/test/header_parser_tests/varargs_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ ${strings.functions}:
);
});
test('Expected Bindings', () {
final context = testContext();
matchLibraryWithExpected(
context,
actual,
'header_parser_varargs_test_output.dart',
[
Expand Down
12 changes: 8 additions & 4 deletions pkgs/ffigen/test/large_integration_tests/large_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ void main() {
),
);
final library = parse(Context(logger, generator));
final context = testContext();

matchLibraryWithExpected(
context,
library,
'large_test_libclang.dart',
['test', 'large_integration_tests', '_expected_libclang_bindings.dart'],
Expand Down Expand Up @@ -151,9 +153,10 @@ void main() {
macros: Macros.includeAll,
typedefs: Typedefs.includeAll,
);
final library = parse(testContext(generator));
final context = testContext(generator);
final library = parse(context);

matchLibraryWithExpected(library, 'large_test_cjson.dart', [
matchLibraryWithExpected(context, library, 'large_test_cjson.dart', [
'test',
'large_integration_tests',
'_expected_cjson_bindings.dart',
Expand Down Expand Up @@ -202,9 +205,10 @@ void main() {
include: (declaration) => !vaRegex.hasMatch(declaration.originalName),
),
);
final library = parse(testContext(generator));
final context = testContext(generator);
final library = parse(context);

matchLibraryWithExpected(library, 'large_test_sqlite.dart', [
matchLibraryWithExpected(context, library, 'large_test_sqlite.dart', [
'test',
'large_integration_tests',
'_expected_sqlite_bindings.dart',
Expand Down
Loading