Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,19 @@ class Clang {
late final _clang_getCursorDefinition = _clang_getCursorDefinitionPtr
.asFunction<CXCursor Function(CXCursor)>();

/// 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<ffi.NativeFunction<ffi.UnsignedInt Function(CXCursor)>>(
'clang_isCursorDefinition',
);
late final _clang_isCursorDefinition = _clang_isCursorDefinitionPtr
.asFunction<int Function(CXCursor)>();

/// Given a cursor that represents a property declaration, return the
/// associated property attributes. The bits are formed from
/// \c CXObjCPropertyAttrKind.
Expand Down
42 changes: 13 additions & 29 deletions pkgs/ffigen/lib/src/header_parser/translation_unit_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ Set<Binding> parseTranslationUnit(
) {
final bindings = <Binding>{};
final logger = context.logger;
final headers = <String, bool>{};

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:
Expand Down Expand Up @@ -57,15 +60,15 @@ Set<Binding> 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()}',
);
}
});

Expand Down Expand Up @@ -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)!;
}
19 changes: 4 additions & 15 deletions pkgs/ffigen/lib/src/header_parser/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ extension CXSourceRangePtrExt on Pointer<clang_types.CXSourceRange> {
}

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()) {
Expand Down Expand Up @@ -471,14 +475,6 @@ extension DynamicCStringArray on Pointer<Pointer<Utf8>> {
}
}

class Stack<T> {
final _stack = <T>[];

T get top => _stack.last;
T pop() => _stack.removeLast();
void push(T item) => _stack.add(item);
}

class Macro {
final String usr;
final String? originalName;
Expand All @@ -501,9 +497,6 @@ class BindingsIndex {
/// Contains usr for typedefs which cannot be generated.
final Set<String> _unsupportedTypealiases = {};

/// Index for headers.
final Map<String, bool> _headerCache = {};

bool isSeenType(String usr) => _declaredTypes.containsKey(usr);
void addTypeToSeen(String usr, Type type) => _declaredTypes[usr] = type;
Type? getSeenType(String usr) => _declaredTypes[usr];
Expand All @@ -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) =>
Expand Down
1 change: 1 addition & 0 deletions pkgs/ffigen/tool/libclang_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ functions:
- clang_getFieldDeclBitWidth
- clang_Cursor_isFunctionInlined
- clang_getCursorDefinition
- clang_isCursorDefinition
- clang_getCursorAvailability
- clang_getCursorPlatformAvailability
- clang_disposeCXPlatformAvailability
Expand Down
Loading