[ffigen] Merge stable to master, bump version (#561)
* Handle multi anonymous struct with same USR (#559)
Co-authored-by: Daco Harkes <dacoharkes@google.com>
diff --git a/pkgs/ffigen/CHANGELOG.md b/pkgs/ffigen/CHANGELOG.md
index 6b40576..168d731 100644
--- a/pkgs/ffigen/CHANGELOG.md
+++ b/pkgs/ffigen/CHANGELOG.md
@@ -1,3 +1,7 @@
+# 8.0.0-dev.1
+
+- Fix invalid struct/enum member references due to multiple anonymous struct/enum in a declaration.
+
# 8.0.0-dev.0
- Adds `final` class modifier to generated sub types `Struct`, `Union` and
@@ -9,6 +13,9 @@
`Opaque` sub types is already disallowed by `dart:ffi` pre 3.0, so adding the
`final` keyword is not a breaking change.
- Bumps SDK lowerbound to 3.0.
+# 7.2.11
+
+- Fix invalid struct/enum member references due to multiple anonymous struct/enum in a declaration.
# 7.2.10
diff --git a/pkgs/ffigen/lib/src/header_parser/utils.dart b/pkgs/ffigen/lib/src/header_parser/utils.dart
index a674618..f03df3f 100644
--- a/pkgs/ffigen/lib/src/header_parser/utils.dart
+++ b/pkgs/ffigen/lib/src/header_parser/utils.dart
@@ -60,7 +60,11 @@
extension CXCursorExt on clang_types.CXCursor {
String usr() {
- return clang.clang_getCursorUSR(this).toStringAndDispose();
+ var res = clang.clang_getCursorUSR(this).toStringAndDispose();
+ if (isAnonymousRecordDecl()) {
+ res += "@offset:${sourceFileOffset()}";
+ }
+ return res;
}
/// Returns the kind int from [clang_types.CXCursorKind].
@@ -124,6 +128,17 @@
return s;
}
+ int sourceFileOffset() {
+ final cxsource = clang.clang_getCursorLocation(this);
+ final cxOffset = calloc<UnsignedInt>();
+
+ // Puts the values in these pointers.
+ clang.clang_getFileLocation(cxsource, nullptr, nullptr, nullptr, cxOffset);
+ final offset = cxOffset.value;
+ calloc.free(cxOffset);
+ return offset;
+ }
+
/// Returns whether the file that the cursor is inside is a system header.
bool isInSystemHeader() {
final location = clang.clang_getCursorLocation(this);
diff --git a/pkgs/ffigen/pubspec.yaml b/pkgs/ffigen/pubspec.yaml
index 53dc9f2..d477965 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-dev.0
+version: 8.0.0-dev.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 ca7a938..82fedd0 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
@@ -63,3 +63,19 @@
final class Union4 extends ffi.Opaque {}
final class Union5 extends ffi.Opaque {}
+
+final class Union6 extends ffi.Union {
+ external UnnamedUnion1 unnamed;
+
+ external UnnamedUnion2 unnamed1;
+}
+
+final class UnnamedUnion1 extends ffi.Union {
+ @ffi.Float()
+ external double a;
+}
+
+final class UnnamedUnion2 extends ffi.Union {
+ @ffi.Float()
+ external double b;
+}
diff --git a/pkgs/ffigen/test/header_parser_tests/unions.h b/pkgs/ffigen/test/header_parser_tests/unions.h
index 1bd9ae1..e7d86ff 100644
--- a/pkgs/ffigen/test/header_parser_tests/unions.h
+++ b/pkgs/ffigen/test/header_parser_tests/unions.h
@@ -32,6 +32,20 @@
union Union3 s; // Incomplete nested union.
};
+// Multiple anonymous declarations
+union Union6
+{
+ union
+ {
+ float a;
+ };
+
+ union
+ {
+ float b;
+ };
+};
+
void func1(union Union2 *s);
// Incomplete array parameter will be treated as a pointer.