Skip to content

Commit efd5fbf

Browse files
authored
Merge branch 'main' into ffigen_cleanup_3
2 parents bc7803e + 0ce2b7b commit efd5fbf

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.shouldIncludeHeader(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.shouldIncludeHeader(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
@@ -86,6 +86,10 @@ extension CXSourceRangePtrExt on Pointer<clang_types.CXSourceRange> {
8686
}
8787

8888
extension CXCursorExt on clang_types.CXCursor {
89+
bool get isNull => clang.clang_Cursor_isNull(this) != 0;
90+
bool get isDefinition => clang.clang_isCursorDefinition(this) != 0;
91+
clang_types.CXCursor get definition => clang.clang_getCursorDefinition(this);
92+
8993
String usr() {
9094
var res = clang.clang_getCursorUSR(this).toStringAndDispose();
9195
assert(!res.contains(synthUsrChar));
@@ -473,14 +477,6 @@ extension DynamicCStringArray on Pointer<Pointer<Utf8>> {
473477
}
474478
}
475479

476-
class Stack<T> {
477-
final _stack = <T>[];
478-
479-
T get top => _stack.last;
480-
T pop() => _stack.removeLast();
481-
void push(T item) => _stack.add(item);
482-
}
483-
484480
class Macro {
485481
final String usr;
486482
final String? originalName;
@@ -503,9 +499,6 @@ class BindingsIndex {
503499
/// Contains usr for typedefs which cannot be generated.
504500
final Set<String> _unsupportedTypealiases = {};
505501

506-
/// Index for headers.
507-
final Map<String, bool> _headerCache = {};
508-
509502
bool isSeenType(String usr) => _declaredTypes.containsKey(usr);
510503
void addTypeToSeen(String usr, Type type) => _declaredTypes[usr] = type;
511504
Type? getSeenType(String usr) => _declaredTypes[usr];
@@ -525,10 +518,6 @@ class BindingsIndex {
525518
_unsupportedTypealiases.contains(usr);
526519
void addUnsupportedTypealiasToSeen(String usr) =>
527520
_unsupportedTypealiases.add(usr);
528-
bool isSeenHeader(String source) => _headerCache.containsKey(source);
529-
void addHeaderToSeen(String source, bool includeStatus) =>
530-
_headerCache[source] = includeStatus;
531-
bool? getSeenHeaderStatus(String source) => _headerCache[source];
532521
void addObjCBlockToSeen(String key, ObjCBlock t) => _objcBlocks[key] = t;
533522
ObjCBlock? getSeenObjCBlock(String key) => _objcBlocks[key];
534523
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)