Get type-checked AST

Hello !

I'm exploring the possibilities to write a SPM plugin that would inspect code architecture. I think what I'm looking for is a mean to get and walk through a type-checked AST.

I'm not sure it's feasible as a plugin (like I would for a linter that would just need AST using SwiftSyntax), but theoretically I think it is at least possible to create an executable as the semantic analysis is obviously part of the compiler.

Do you have any direction or suggestion that would help me find an entry point to investigate this topic ? :pray:

Thanks !


The semantic AST isn't stable from version-to-version of the compiler, so there's no official API to do this unfortunately. The -dump-ast flag gives you a text dump of the AST for a file in an S-expression-like format, but it's not easily (nor meant to be) machine-parsable.

You could potentially cobble together the information you need from a combination of index-store data, SourceKit requests, and SwiftSyntax, but I don't know how feasible that is to integrate into a SPM plug-in.

1 Like

Very appropriate directions indeed, thank you very much @allevato !