From 1b120eeb2d69e6eb9d767bb553d291d9b514513c Mon Sep 17 00:00:00 2001 From: Prerak Mann Date: Fri, 12 Feb 2021 14:54:38 +0530 Subject: [PATCH] Skip inline functions, added test, updated changelog and version. --- CHANGELOG.md | 3 +++ .../clang_bindings/clang_bindings.dart | 23 +++++++++++++++++++ .../sub_parsers/functiondecl_parser.dart | 10 ++++++++ pubspec.yaml | 2 +- test/header_parser_tests/functions.h | 3 +++ test/header_parser_tests/functions_test.dart | 5 ++++ tool/libclang_config.yaml | 1 + 7 files changed, 46 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ed8dfbf..7ceb874d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.0-dev.6 +- Functions marked `inline` are now skipped. + # 2.0.0-dev.5 - Use `Opaque` for representing empty `Struct`s. diff --git a/lib/src/header_parser/clang_bindings/clang_bindings.dart b/lib/src/header_parser/clang_bindings/clang_bindings.dart index a68621eb..b8342177 100644 --- a/lib/src/header_parser/clang_bindings/clang_bindings.dart +++ b/lib/src/header_parser/clang_bindings/clang_bindings.dart @@ -510,6 +510,21 @@ class Clang { _dart_clang_Cursor_isMacroBuiltin? _clang_Cursor_isMacroBuiltin; + /// Determine whether a CXCursor that is a function declaration, is an + /// inline declaration. + int clang_Cursor_isFunctionInlined( + CXCursor C, + ) { + return (_clang_Cursor_isFunctionInlined ??= _dylib.lookupFunction< + _c_clang_Cursor_isFunctionInlined, + _dart_clang_Cursor_isFunctionInlined>( + 'clang_Cursor_isFunctionInlined'))( + C, + ); + } + + _dart_clang_Cursor_isFunctionInlined? _clang_Cursor_isFunctionInlined; + /// For pointer types, returns the type of the pointee. CXType clang_getPointeeType( CXType T, @@ -2563,6 +2578,14 @@ typedef _dart_clang_Cursor_isMacroBuiltin = int Function( CXCursor C, ); +typedef _c_clang_Cursor_isFunctionInlined = ffi.Uint32 Function( + CXCursor C, +); + +typedef _dart_clang_Cursor_isFunctionInlined = int Function( + CXCursor C, +); + typedef _c_clang_getPointeeType = CXType Function( CXType T, ); diff --git a/lib/src/header_parser/sub_parsers/functiondecl_parser.dart b/lib/src/header_parser/sub_parsers/functiondecl_parser.dart index 7bf089ab..ccfcb216 100644 --- a/lib/src/header_parser/sub_parsers/functiondecl_parser.dart +++ b/lib/src/header_parser/sub_parsers/functiondecl_parser.dart @@ -35,6 +35,16 @@ Func? parseFunctionDeclaration(clang_types.CXCursor cursor) { final rt = _getFunctionReturnType(cursor); final parameters = _getParameters(cursor, funcName); + if (clang.clang_Cursor_isFunctionInlined(cursor) != 0) { + _logger.fine( + '---- Removed Function, reason: inline function: ${cursor.completeStringRepr()}'); + _logger.warning( + "Skipped Function '$funcName', inline functions are not supported."); + return _stack + .pop() + .func; // Returning null so that [addToBindings] function excludes this. + } + if (rt.isIncompleteStruct || _stack.top.incompleteStructParameter) { _logger.fine( '---- Removed Function, reason: Incomplete struct pass/return by value: ${cursor.completeStringRepr()}'); diff --git a/pubspec.yaml b/pubspec.yaml index 3465af72..06fd2c8e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ # BSD-style license that can be found in the LICENSE file. name: ffigen -version: 2.0.0-dev.5 +version: 2.0.0-dev.6 homepage: https://github.com/dart-lang/ffigen description: Experimental generator for FFI bindings, using LibClang to parse C header files. diff --git a/test/header_parser_tests/functions.h b/test/header_parser_tests/functions.h index 5c00b33c..56ec2bef 100644 --- a/test/header_parser_tests/functions.h +++ b/test/header_parser_tests/functions.h @@ -16,3 +16,6 @@ void *func4(int8_t **, double, int32_t ***); typedef void shortHand(void(b)()); // Would be treated as `void func5(shortHand *a, void (*b)())`. void func5(shortHand a, void(b)()); + +// Should be skipped as inline functions are not supported. +static inline void inlineFunc(); diff --git a/test/header_parser_tests/functions_test.dart b/test/header_parser_tests/functions_test.dart index 31e05069..c02905d0 100644 --- a/test/header_parser_tests/functions_test.dart +++ b/test/header_parser_tests/functions_test.dart @@ -58,6 +58,11 @@ ${strings.headers}: expect(actual.getBindingAsString('func5'), expected.getBindingAsString('func5')); }); + + test('Skip inline functions', () { + expect(() => actual.getBindingAsString('inlineFunc'), + throwsA(TypeMatcher())); + }); }); } diff --git a/tool/libclang_config.yaml b/tool/libclang_config.yaml index 2a059dd2..c27f17c4 100644 --- a/tool/libclang_config.yaml +++ b/tool/libclang_config.yaml @@ -101,3 +101,4 @@ functions: - clang_Cursor_isAnonymousRecordDecl - clang_getCursorUSR - clang_getFieldDeclBitWidth + - clang_Cursor_isFunctionInlined