[ffigen] [jnigen] Util to dump entire parsed API #3621
Looking at the outputs in the test, I think it'd be more readable to put the $interfaceStr last. Same with the parents below.
Looks like this special casing exists here because ObjCMethod.parent can be either a ObjCInterface or ObjCCategory. So it would be better to do that special casing in visitObjCMethod:
@override
void visitObjCMethod(ObjCMethod node) {
_sink.writeln('ObjCMethod(${_objCMethodParent(node.parent)}, ${node.selector})');
}
static String _objCMethodParent(DeclNode node) => switch(node) {
ObjCInterface i => _visitObjCInterface(i),
ObjCCategory c => _visitObjCCategory(c),
};
@override
void visitObjCInterface(ObjCInterface node) {
_sink.writeln(_visitObjCInterface(node));
}
static String _visitObjCInterface(ObjCInterface node) => _declString(node);
static String _declString(DeclNode decl) =>
'${decl.runtimeType}(${decl.originalName}, ${decl.usr})';
Same for _namedNodeString.
I think the API should do one or the other: return the string or print to the sink. We don't need both.
Why run the user visitors here?
v22 is already out. You'll need to bump the version to 22.1.0-wip, here and in the pubspec.
DRY. Reuse the logic from visitMethod.
DRY. Reuse the logic from visitClass.
As in the FFIgen one, move the method name to the start.
The dual behavior was for convenience, but doing one or the other makes complete sense for a cleaner API. so between:Future<String> dumpApi(...) and Future<void> dumpApi({StringSink? sink})
Which do you prefer? I was leaning more towards Future<String> dumpApi(...).
Yeah, I think returning a String makes more sense.
I have addressed that now. thanks
My idea was that if a user has configured renaming or filtering visitors, running them before dumper allows dumpApi() to inspect the AST in the exact state when the code generator will see it (consistent with how jnigen runs visitors before dumper in generate_bindings.dart). if you prefer I can remove it or keep visitors: [dumper].
I can see the utility in that, but I don't think it will work. At least not the way you have it written currently.
In FFIgen, later visitors still see nodes that have been filtered out by earlier visitors, so this won't actually show which nodes are filtered. You could fix this by only printing the node if isIncluded, or printing "X " or something at the start of the line if the node isn't included. This is an inconsistency with JNIgen, so maybe we should change the visitor infra to skip nodes that are filtered, but it's tricky because we filter almost all nodes out by default, so the first visitor wouldn't see anything :upside_down_face:.
As for renames, you're printing the originalName, not the name, so these changes won't be visible either. As it stands now I don't think any of the user visitors can have any visible effect on the dumpApi output.
In any case, if this is a use case you want to support, you'll need to write tests for it.
Ohh okay, 😅 must have missed that. I will look into it more
and you're completely right. I verified the parser pipeline. user visitors don't have any visible effect on the output anyway. I've updated dumpApi to only run dumper in both ffigen and jnigen.
@liamappelbe all CI are green now, please merge this, as this is my first pr here 😄. I appreciate your guidance.
This change seems unrelated?
If you want to include it, it'll need a changelog entry. I'm also wondering why it's nullable. Under what circumstances would the descriptor not be available? It'll also need a test (for both the non-null and null cases). And at that point it should probably be a separate PR.
Most of this function duplicates line 67-96 of generate. Can you factor out the logic into a helper? Looks like the helper could just take a Logger? and return userClasses.
Move this to the changelog entry for 1.0.1-wip, and bump it to 1.1.0-wip. You'll also need to update the jnigen pubspec, and I think there's a const variable somewhere in jnigen that holds the version too.
As of https://github.com/dart-lang/native/pull/3627, you at least won't need to update all the bindings files 😅
Almost there. Just a few more details.
@liamappelbe I have addressed every suggestion. please take a look and approve
liamappelbe
amanmaurya92
Description
When configuring FFIgen or JNIgen, users frequently encounter scenarios where expected API elements do not appear in the generated bindings (due to incorrect entry point headers, missing include paths, preprocessor flags, or misconfigured filters). Diagnosing this previously required users to construct custom AST visitor debug scripts from scratch.
This PR introduces a standardized AST dumping utility:
ApiDumperVisitortopackage:ffigento traverse and format all public AST declarations into a clear, greppable format (Node(name, USR/parent)).dumpApi({StringSink? sink, Logger? logger, Uri? libclangDylib})extension method onFfiGeneratorthat parses headers without writing output binding files.ApiDumperVisitoranddumpApi({StringSink? sink, Logger? logger})onJniGeneratorinpackage:jnigento dump Java classes, methods, parameters, and fields.pkgs/ffigenandpkgs/jnigencovering synthetic AST node dumping and end-to-end header parsing.CHANGELOG.mdfor both packages.Related Issues
Fixes https://github.com/dart-lang/native/issues/3605
PR Checklist
dart tool/ci.dart --alllocally and resolved all issues identified. This ensures the PR is formatted, has no lint errors, and ran all code generators. This applies to the packages part of the toplevelpubspec.yamlworkspace.CHANGELOG.mdfor the relevant packages. (Not needed for small changes such as doc typos).