Skip to content

Commit 424f0d2

Browse files
authored
Merge branch 'main' into ffigen_config_cleanup_4
2 parents c221448 + 0ce2b7b commit 424f0d2

File tree

4 files changed

+31
-44
lines changed

4 files changed

+31
-44
lines changed

pkgs/ffigen/lib/src/header_parser/clang_bindings/clang_bindings.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,19 @@ class Clang {
11561156
late final _clang_getCursorDefinition = _clang_getCursorDefinitionPtr
11571157
.asFunction<CXCursor Function(CXCursor)>();
11581158

1159+
/// Determine whether the declaration pointed to by this cursor
1160+
/// is also a definition of that entity.
1161+
int clang_isCursorDefinition(CXCursor arg0) {
1162+
return _clang_isCursorDefinition(arg0);
1163+
}
1164+
1165+
late final _clang_isCursorDefinitionPtr =
1166+
_lookup<ffi.NativeFunction<ffi.UnsignedInt Function(CXCursor)>>(
1167+
'clang_isCursorDefinition',
1168+
);
1169+
late final _clang_isCursorDefinition = _clang_isCursorDefinitionPtr
1170+
.asFunction<int Function(CXCursor)>();
1171+
11591172
/// Given a cursor that represents a property declaration, return the
11601173
/// associated property attributes. The bits are formed from
11611174
/// \c CXObjCPropertyAttrKind.

pkgs/ffigen/lib/src/header_parser/translation_unit_parser.dart

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ Set<Binding> parseTranslationUnit(
2020
) {
2121
final bindings = <Binding>{};
2222
final logger = context.logger;
23+
final headers = <String, bool>{};
2324

2425
translationUnitCursor.visitChildren((cursor) {
25-
try {
26-
if (shouldIncludeRootCursor(context, cursor.sourceFileName())) {
26+
final file = cursor.sourceFileName();
27+
if (file.isEmpty) return;
28+
if (headers[file] ??= context.config.headers.include(Uri.file(file))) {
29+
try {
2730
logger.finest('rootCursorVisitor: ${cursor.completeStringRepr()}');
2831
switch (clang.clang_getCursorKind(cursor)) {
2932
case clang_types.CXCursorKind.CXCursor_FunctionDecl:
@@ -57,15 +60,15 @@ Set<Binding> parseTranslationUnit(
5760
default:
5861
logger.finer('rootCursorVisitor: CursorKind not implemented');
5962
}
60-
} else {
61-
logger.finest(
62-
'rootCursorVisitor:(not included) ${cursor.completeStringRepr()}',
63-
);
63+
} catch (e, s) {
64+
logger.severe(e);
65+
logger.severe(s);
66+
rethrow;
6467
}
65-
} catch (e, s) {
66-
logger.severe(e);
67-
logger.severe(s);
68-
rethrow;
68+
} else {
69+
logger.finest(
70+
'rootCursorVisitor:(not included) ${cursor.completeStringRepr()}',
71+
);
6972
}
7073
});
7174

@@ -104,22 +107,3 @@ void buildUsrCursorDefinitionMap(
104107
}
105108
});
106109
}
107-
108-
/// True if a cursor should be included based on headers config, used on root
109-
/// declarations.
110-
bool shouldIncludeRootCursor(Context context, String sourceFile) {
111-
// Handle empty string in case of system headers or macros.
112-
if (sourceFile.isEmpty) {
113-
return false;
114-
}
115-
116-
// Add header to seen if it's not.
117-
if (!context.bindingsIndex.isSeenHeader(sourceFile)) {
118-
context.bindingsIndex.addHeaderToSeen(
119-
sourceFile,
120-
context.config.headers.include(Uri.file(sourceFile)),
121-
);
122-
}
123-
124-
return context.bindingsIndex.getSeenHeaderStatus(sourceFile)!;
125-
}

pkgs/ffigen/lib/src/header_parser/utils.dart

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ extension CXSourceRangePtrExt on Pointer<clang_types.CXSourceRange> {
8585
}
8686

8787
extension CXCursorExt on clang_types.CXCursor {
88+
bool get isNull => clang.clang_Cursor_isNull(this) != 0;
89+
bool get isDefinition => clang.clang_isCursorDefinition(this) != 0;
90+
clang_types.CXCursor get definition => clang.clang_getCursorDefinition(this);
91+
8892
String usr() {
8993
var res = clang.clang_getCursorUSR(this).toStringAndDispose();
9094
if (isAnonymousRecordDecl()) {
@@ -471,14 +475,6 @@ extension DynamicCStringArray on Pointer<Pointer<Utf8>> {
471475
}
472476
}
473477

474-
class Stack<T> {
475-
final _stack = <T>[];
476-
477-
T get top => _stack.last;
478-
T pop() => _stack.removeLast();
479-
void push(T item) => _stack.add(item);
480-
}
481-
482478
class Macro {
483479
final String usr;
484480
final String? originalName;
@@ -501,9 +497,6 @@ class BindingsIndex {
501497
/// Contains usr for typedefs which cannot be generated.
502498
final Set<String> _unsupportedTypealiases = {};
503499

504-
/// Index for headers.
505-
final Map<String, bool> _headerCache = {};
506-
507500
bool isSeenType(String usr) => _declaredTypes.containsKey(usr);
508501
void addTypeToSeen(String usr, Type type) => _declaredTypes[usr] = type;
509502
Type? getSeenType(String usr) => _declaredTypes[usr];
@@ -523,10 +516,6 @@ class BindingsIndex {
523516
_unsupportedTypealiases.contains(usr);
524517
void addUnsupportedTypealiasToSeen(String usr) =>
525518
_unsupportedTypealiases.add(usr);
526-
bool isSeenHeader(String source) => _headerCache.containsKey(source);
527-
void addHeaderToSeen(String source, bool includeStatus) =>
528-
_headerCache[source] = includeStatus;
529-
bool? getSeenHeaderStatus(String source) => _headerCache[source];
530519
void addObjCBlockToSeen(String key, ObjCBlock t) => _objcBlocks[key] = t;
531520
ObjCBlock? getSeenObjCBlock(String key) => _objcBlocks[key];
532521
void addObjCProtocolToSeen(String usr, ObjCProtocol t) =>

pkgs/ffigen/tool/libclang_config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ functions:
120120
- clang_getFieldDeclBitWidth
121121
- clang_Cursor_isFunctionInlined
122122
- clang_getCursorDefinition
123+
- clang_isCursorDefinition
123124
- clang_getCursorAvailability
124125
- clang_getCursorPlatformAvailability
125126
- clang_disposeCXPlatformAvailability

0 commit comments

Comments
 (0)