why generate source code
The most frequent use case for code generators I had to work with in my career were generators which
took some high level meta-description for some kind of data model or database schema as input (maybe a relational schema, or some kind of XML schema)
and produced boiler-plate CRUD code for data access classes as output, and maybe additional things like corresponding SQLs or documentation.
The benefit here is that from one line of a short input specification you get 5 to 10 lines of debuggable, type-safe, bug-free (assumed the code generators output is mature) code you otherwise had to implement and maintain manually. You can imagine how much this reduces maintenance and evolvement effort.
Let me also respond to your initial question
Is source code generation an anti pattern
No, not source code generation per se, but there are indeed some pitfalls. As stated in The Pragmatic Programmer, one should avoid the usage of a code generator when it produces code which is hard to understandwhen it produces code which is hard to understand. Otherwise, the increased efforts to use or debug this code may easily outweigh the effort saved by not writing the code manually.
I would also like to add that it is most times a good idea to separate generated parts of code from manually written code physically in a way re-generation does not overwrite any manual changes. However, I also have dealt with the situation more than once where the task was to migrate some code written in old language X to another, more modern language Y, with the intention to to the maintenance afterwards in language Y. This is a valid use case for one-time code generation.