[ffigen] Fix invalid code generated due to anoymous structs/unions (#572)
diff --git a/pkgs/ffigen/CHANGELOG.md b/pkgs/ffigen/CHANGELOG.md index 9b07724..b9e9aec 100644 --- a/pkgs/ffigen/CHANGELOG.md +++ b/pkgs/ffigen/CHANGELOG.md
@@ -1,3 +1,7 @@ +# 8.0.1 + +- Fixed invalid code generated due to anonymous structs/unions with unsupported types. + # 8.0.0 - Stable release for Dart 3.0 with support for class modifers, variadic arguments, and `@Native`s.
diff --git a/pkgs/ffigen/lib/src/header_parser/sub_parsers/compounddecl_parser.dart b/pkgs/ffigen/lib/src/header_parser/sub_parsers/compounddecl_parser.dart index 93d662d..8c700c9 100644 --- a/pkgs/ffigen/lib/src/header_parser/sub_parsers/compounddecl_parser.dart +++ b/pkgs/ffigen/lib/src/header_parser/sub_parsers/compounddecl_parser.dart
@@ -294,12 +294,15 @@ break; case clang_types.CXCursorKind.CXCursor_UnionDecl: case clang_types.CXCursorKind.CXCursor_StructDecl: - final mt = cursor.toCodeGenType(); - // If the union/struct are anonymous, then we need to add them now, // otherwise they will be added in the next iteration. if (!cursor.isAnonymousRecordDecl()) break; + final mt = cursor.toCodeGenType(); + if (mt.isIncompleteCompound) { + parsed.incompleteCompoundMember = true; + } + // Anonymous members are always unnamed. To avoid environment- // dependent naming issues with the generated code, we explicitly // use the empty string as spelling.
diff --git a/pkgs/ffigen/pubspec.yaml b/pkgs/ffigen/pubspec.yaml index 58e6bfb..fcc83ca 100644 --- a/pkgs/ffigen/pubspec.yaml +++ b/pkgs/ffigen/pubspec.yaml
@@ -3,7 +3,7 @@ # BSD-style license that can be found in the LICENSE file. name: ffigen -version: 8.0.0 +version: 8.0.1 description: Generator for FFI bindings, using LibClang to parse C header files. repository: https://github.com/dart-lang/ffigen
diff --git a/pkgs/ffigen/test/header_parser_tests/expected_bindings/_expected_unions_bindings.dart b/pkgs/ffigen/test/header_parser_tests/expected_bindings/_expected_unions_bindings.dart index 82fedd0..9ef50eb 100644 --- a/pkgs/ffigen/test/header_parser_tests/expected_bindings/_expected_unions_bindings.dart +++ b/pkgs/ffigen/test/header_parser_tests/expected_bindings/_expected_unions_bindings.dart
@@ -79,3 +79,5 @@ @ffi.Float() external double b; } + +final class Union7 extends ffi.Opaque {}
diff --git a/pkgs/ffigen/test/header_parser_tests/unions.h b/pkgs/ffigen/test/header_parser_tests/unions.h index e7d86ff..7f03834 100644 --- a/pkgs/ffigen/test/header_parser_tests/unions.h +++ b/pkgs/ffigen/test/header_parser_tests/unions.h
@@ -46,6 +46,21 @@ }; }; +// Multiple anonymous declarations with incomplete members +union Union7 +{ + union + { + float a; + }; + + union + { + float b; + int c[]; + }; +}; + void func1(union Union2 *s); // Incomplete array parameter will be treated as a pointer.