Viewing Synthesized Code

Does Xcode support some means by which I can view the code synthesized by the compiler (e.g., the code supporting the Codable protocol)?

If Xcode does not provide such means, then does Xcode maintain intermediate files that contain synthesized code? If so, where can I find these files?

2 Likes

I thought the synthesization is just baked into the compiler and there are no files generated for that. You could have a fileprivate type with Hashable synthesization which cannot have a standalone extension in some other file. I'm definitely want to know the answers to your questions as well.

You can read the code that synthesises Codable conformance in this file, e.g. here.

1 Like

There's no way AFAIK to view generated Swift for these kinds of things. The synthesis happens inside of the compiler, and is just inserted into the AST. That being said, you can see the emitted SIL/IR/asm for them by passing the various -emit- options.

Edit: By that logic, you can also pass -dump-ast and -print-ast to see the synthesis as well.

Thanks for the responses. This is somewhat useful, but not necessarily efficient.

However, I think developers should have an easy way from within Xcode to view the code synthesized by the compiler.

The simplest use case is debugging. I like the fact that the compiler synthesizes a lot of boilerplate code for me. However, I would like to have a better understanding of what it's doing, rather than blindly trusting the compiler to do the right thing. Maybe the compiler isn't always going to do what I want, and today this is difficult to validate.

A more specific use case is development of an Encoder or a Decoder. Today, I have no idea what the compiler is synthesizing. I can only come to an educated guess by strategically breaking on functions defined by the protocols. I would love to be able to step right into a call of encode or decode, but instead there are these dark zones the debugger skips because they were synthesized. A lot of head scratching goes as the debugger magically skips over this codes.

2 Likes

I think it would be good to have something like this. It'd be useful to have refactorings to go from synthesized to non-synthesized too. cc @Xi_Ge

2 Likes

I agree — this is something I think would be extremely useful, especially as we've increased the amount of synthesis we do over time. The main difficulty I see here is generating meaningful source from the AST and finding a place to put it in a source file (esp. since things like Codable can't be synthesized into an extension and appended to the file).

Internal folks, see rdar://30472233

Terms of Service

Privacy Policy

Cookie Policy