Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Remove members of structs with bit fields #85

Merged
merged 1 commit into from
Aug 20, 2020
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.2.3
- Fixed parsing structs with bitfields, all members of structs with bit field members will now be removed. See [#84](https://github.com/dart-lang/ffigen/issues/84)

# 0.2.2+1
- Updated `package:meta` version to `^1.1.8` for compatibility with flutter sdk.

Expand Down
4 changes: 4 additions & 0 deletions lib/src/clang_library/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,8 @@ CXString *clang_getCursorUSR_wrap(CXCursor *cursor)
return ptrToCXString(clang_getCursorUSR(*cursor));
}

int clang_getFieldDeclBitWidth_wrap(CXCursor *cursor){
return clang_getFieldDeclBitWidth(*cursor);
}

// END ===== WRAPPER FUNCTIONS =====================
1 change: 1 addition & 0 deletions lib/src/clang_library/wrapper.def
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,4 @@ clang_Cursor_Evaluate_wrap
clang_Cursor_isAnonymous_wrap
clang_Cursor_isAnonymousRecordDecl_wrap
clang_getCursorUSR_wrap
clang_getFieldDeclBitWidth_wrap
22 changes: 22 additions & 0 deletions lib/src/header_parser/clang_bindings/clang_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,20 @@ class Clang {
}

_dart_clang_getCursorUSR_wrap _clang_getCursorUSR_wrap;

int clang_getFieldDeclBitWidth_wrap(
ffi.Pointer<CXCursor> cursor,
) {
_clang_getFieldDeclBitWidth_wrap ??= _dylib.lookupFunction<
_c_clang_getFieldDeclBitWidth_wrap,
_dart_clang_getFieldDeclBitWidth_wrap>(
'clang_getFieldDeclBitWidth_wrap');
return _clang_getFieldDeclBitWidth_wrap(
cursor,
);
}

_dart_clang_getFieldDeclBitWidth_wrap _clang_getFieldDeclBitWidth_wrap;
}

/// A character string.
Expand Down Expand Up @@ -2662,3 +2676,11 @@ typedef _c_clang_getCursorUSR_wrap = ffi.Pointer<CXString> Function(
typedef _dart_clang_getCursorUSR_wrap = ffi.Pointer<CXString> Function(
ffi.Pointer<CXCursor> cursor,
);

typedef _c_clang_getFieldDeclBitWidth_wrap = ffi.Int32 Function(
ffi.Pointer<CXCursor> cursor,
);

typedef _dart_clang_getFieldDeclBitWidth_wrap = int Function(
ffi.Pointer<CXCursor> cursor,
);
11 changes: 10 additions & 1 deletion lib/src/header_parser/sub_parsers/structdecl_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class _ParsedStruc {
bool unimplementedMemberType = false;
bool flexibleArrayMember = false;
bool arrayMember = false;
bool bitFieldMember = false;
_ParsedStruc();
}

Expand Down Expand Up @@ -108,6 +109,12 @@ void _setStructMembers(Pointer<clang_types.CXCursor> cursor) {
_logger.warning(
'Removed All Struct Members from ${_stack.top.struc.name}(${_stack.top.struc.originalName}), Flexible array members not supported.');
return _stack.top.struc.members.clear();
} else if (_stack.top.bitFieldMember) {
_logger.fine(
'---- Removed Struct members, reason: bitfield members ${cursor.completeStringRepr()}');
_logger.warning(
'Removed All Struct Members from ${_stack.top.struc.name}(${_stack.top.struc.originalName}), Bit Field members not supported.');
return _stack.top.struc.members.clear();
}
}

Expand All @@ -121,7 +128,6 @@ int _structMembersVisitor(Pointer<clang_types.CXCursor> cursor,
_logger.finer('===== member: ${cursor.completeStringRepr()}');

final mt = cursor.type().toCodeGenTypeAndDispose();

//TODO(4): Remove these when support for Structs by value arrives.
if (mt.broadType == BroadType.Struct) {
// Setting this flag will exclude adding members for this struct's
Expand All @@ -137,6 +143,9 @@ int _structMembersVisitor(Pointer<clang_types.CXCursor> cursor,
} else if (mt.broadType == BroadType.IncompleteArray) {
// TODO(68): Structs with flexible Array Members are not supported.
_stack.top.flexibleArrayMember = true;
} else if (clang.clang_getFieldDeclBitWidth_wrap(cursor) != -1) {
// TODO(84): Struct with bitfields are not suppoorted.
_stack.top.bitFieldMember = true;
}

if (mt.getBaseType().broadType == BroadType.Unimplemented) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:ffigen/src/header_parser/clang_bindings/clang_bindings.dart'
as clang;

// This version must be updated whenever we update the libclang wrapper.
const dylibVersion = 'v3';
const dylibVersion = 'v4';

/// Name of the dynamic library file according to current platform.
String get dylibFileName {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# BSD-style license that can be found in the LICENSE file.

name: ffigen
version: 0.2.2+1
version: 0.2.3
homepage: https://github.com/dart-lang/ffigen
description: Experimental generator for FFI bindings, using LibClang to parse C/C++ header files.

Expand Down
7 changes: 7 additions & 0 deletions test/header_parser_tests/function_n_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ struct Struct3
int b[]; // Flexible array member.
};

// All members should be removed, Bit fields are not supported.
struct Struct4
{
int a:3;
int :2; // Unnamed bit field.
};

void func1(struct Struct2 *s);

// Incomplete array parameter will be treated as a pointer.
Expand Down
4 changes: 4 additions & 0 deletions test/header_parser_tests/function_n_struct_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ ${strings.headers}:
test('Struct3 flexible array member', () {
expect((actual.getBinding('Struct3') as Struc).members.isEmpty, true);
});
test('Struct4 bit field member', () {
expect((actual.getBinding('Struct4') as Struc).members.isEmpty, true);
});
});
}

Expand Down Expand Up @@ -81,6 +84,7 @@ Library expectedLibrary() {
SupportedNativeType.Void,
),
),
Struc(name: 'Struct4'),
],
);
}
1 change: 1 addition & 0 deletions tool/libclang_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,4 @@ functions:
- clang_Cursor_isAnonymous_wrap
- clang_Cursor_isAnonymousRecordDecl_wrap
- clang_getCursorUSR_wrap
- clang_getFieldDeclBitWidth_wrap