diff --git a/pkgs/ffigen/lib/src/header_parser/clang_bindings/clang_bindings.dart b/pkgs/ffigen/lib/src/header_parser/clang_bindings/clang_bindings.dart index e8a47b5fe..5103c9287 100644 --- a/pkgs/ffigen/lib/src/header_parser/clang_bindings/clang_bindings.dart +++ b/pkgs/ffigen/lib/src/header_parser/clang_bindings/clang_bindings.dart @@ -1156,6 +1156,19 @@ class Clang { late final _clang_getCursorDefinition = _clang_getCursorDefinitionPtr .asFunction(); + /// Determine whether the declaration pointed to by this cursor + /// is also a definition of that entity. + int clang_isCursorDefinition(CXCursor arg0) { + return _clang_isCursorDefinition(arg0); + } + + late final _clang_isCursorDefinitionPtr = + _lookup>( + 'clang_isCursorDefinition', + ); + late final _clang_isCursorDefinition = _clang_isCursorDefinitionPtr + .asFunction(); + /// Given a cursor that represents a property declaration, return the /// associated property attributes. The bits are formed from /// \c CXObjCPropertyAttrKind. diff --git a/pkgs/ffigen/lib/src/header_parser/translation_unit_parser.dart b/pkgs/ffigen/lib/src/header_parser/translation_unit_parser.dart index 284c6b279..6e861cdf2 100644 --- a/pkgs/ffigen/lib/src/header_parser/translation_unit_parser.dart +++ b/pkgs/ffigen/lib/src/header_parser/translation_unit_parser.dart @@ -20,10 +20,13 @@ Set parseTranslationUnit( ) { final bindings = {}; final logger = context.logger; + final headers = {}; translationUnitCursor.visitChildren((cursor) { - try { - if (shouldIncludeRootCursor(context, cursor.sourceFileName())) { + final file = cursor.sourceFileName(); + if (file.isEmpty) return; + if (headers[file] ??= context.config.shouldIncludeHeader(Uri.file(file))) { + try { logger.finest('rootCursorVisitor: ${cursor.completeStringRepr()}'); switch (clang.clang_getCursorKind(cursor)) { case clang_types.CXCursorKind.CXCursor_FunctionDecl: @@ -57,15 +60,15 @@ Set parseTranslationUnit( default: logger.finer('rootCursorVisitor: CursorKind not implemented'); } - } else { - logger.finest( - 'rootCursorVisitor:(not included) ${cursor.completeStringRepr()}', - ); + } catch (e, s) { + logger.severe(e); + logger.severe(s); + rethrow; } - } catch (e, s) { - logger.severe(e); - logger.severe(s); - rethrow; + } else { + logger.finest( + 'rootCursorVisitor:(not included) ${cursor.completeStringRepr()}', + ); } }); @@ -104,22 +107,3 @@ void buildUsrCursorDefinitionMap( } }); } - -/// True if a cursor should be included based on headers config, used on root -/// declarations. -bool shouldIncludeRootCursor(Context context, String sourceFile) { - // Handle empty string in case of system headers or macros. - if (sourceFile.isEmpty) { - return false; - } - - // Add header to seen if it's not. - if (!context.bindingsIndex.isSeenHeader(sourceFile)) { - context.bindingsIndex.addHeaderToSeen( - sourceFile, - context.config.shouldIncludeHeader(Uri.file(sourceFile)), - ); - } - - return context.bindingsIndex.getSeenHeaderStatus(sourceFile)!; -} diff --git a/pkgs/ffigen/lib/src/header_parser/utils.dart b/pkgs/ffigen/lib/src/header_parser/utils.dart index f2f5746e3..b69517e10 100644 --- a/pkgs/ffigen/lib/src/header_parser/utils.dart +++ b/pkgs/ffigen/lib/src/header_parser/utils.dart @@ -85,6 +85,10 @@ extension CXSourceRangePtrExt on Pointer { } extension CXCursorExt on clang_types.CXCursor { + bool get isNull => clang.clang_Cursor_isNull(this) != 0; + bool get isDefinition => clang.clang_isCursorDefinition(this) != 0; + clang_types.CXCursor get definition => clang.clang_getCursorDefinition(this); + String usr() { var res = clang.clang_getCursorUSR(this).toStringAndDispose(); if (isAnonymousRecordDecl()) { @@ -471,14 +475,6 @@ extension DynamicCStringArray on Pointer> { } } -class Stack { - final _stack = []; - - T get top => _stack.last; - T pop() => _stack.removeLast(); - void push(T item) => _stack.add(item); -} - class Macro { final String usr; final String? originalName; @@ -501,9 +497,6 @@ class BindingsIndex { /// Contains usr for typedefs which cannot be generated. final Set _unsupportedTypealiases = {}; - /// Index for headers. - final Map _headerCache = {}; - bool isSeenType(String usr) => _declaredTypes.containsKey(usr); void addTypeToSeen(String usr, Type type) => _declaredTypes[usr] = type; Type? getSeenType(String usr) => _declaredTypes[usr]; @@ -523,10 +516,6 @@ class BindingsIndex { _unsupportedTypealiases.contains(usr); void addUnsupportedTypealiasToSeen(String usr) => _unsupportedTypealiases.add(usr); - bool isSeenHeader(String source) => _headerCache.containsKey(source); - void addHeaderToSeen(String source, bool includeStatus) => - _headerCache[source] = includeStatus; - bool? getSeenHeaderStatus(String source) => _headerCache[source]; void addObjCBlockToSeen(String key, ObjCBlock t) => _objcBlocks[key] = t; ObjCBlock? getSeenObjCBlock(String key) => _objcBlocks[key]; void addObjCProtocolToSeen(String usr, ObjCProtocol t) => diff --git a/pkgs/ffigen/tool/libclang_config.yaml b/pkgs/ffigen/tool/libclang_config.yaml index e5d7ebc2f..0221ec981 100644 --- a/pkgs/ffigen/tool/libclang_config.yaml +++ b/pkgs/ffigen/tool/libclang_config.yaml @@ -120,6 +120,7 @@ functions: - clang_getFieldDeclBitWidth - clang_Cursor_isFunctionInlined - clang_getCursorDefinition + - clang_isCursorDefinition - clang_getCursorAvailability - clang_getCursorPlatformAvailability - clang_disposeCXPlatformAvailability