Why Printer Classes are private?

Hello,
I want to redefine some visitors in DeclPrinter and StmtPrinter, but they are not exported over Clang API. Why is that?

Currently I have to copy-paste them into my tool. But this unfortunately leads to some duplication.

-Roman

In StmtPrinter there is a PrintHelper that can be used to override printing of some nodes. I guess PrintHelper was introduced for someone like me that wanted to override some StmtPrinter visitors.
But DeclPrinter does not supports PrintHelper for some reason.

Hi Roman,

Hi Alex,
I can probably look into it if I have time and will feel myself confident enough. I’ve started learning Clang just recently (and my background is HW design, not programming).

Meanwhile I’ve found a workaround to do what I’ve needed originally:
Instead of printing nodes in a special way, I create Replacements for them, then use a temporary Rewriter to apply replacements and get back text I wanted to print.

Probably it was supposed to be always done like that?

The only problem is the case when I don’t have parts of AST in original source code. For example when I deal with template instantiations. In that case I will have to emit implicit template specializations in overlay filesystem, reparse TU again, and then create replacements for nodes of interest.

So If I had a capability to customize AST pretty-printer, I won’t need to re-parse source code multiple times.

Hi Alex,
I can probably look into it if I have time and will feel myself confident
enough. I've started learning Clang just recently (and my background is HW
design, not programming).

Meanwhile I've found a workaround to do what I've needed originally:
Instead of printing nodes in a special way, I create Replacements for
them, then use a temporary Rewriter to apply replacements and get back text
I wanted to print.

Probably it was supposed to be always done like that?

I think that it's definitely a viable option.

The only problem is the case when I don't have parts of AST in original
source code. For example when I deal with template instantiations. In that
case I will have to emit implicit template specializations in overlay
filesystem, reparse TU again, and then create replacements for nodes of
interest.

So If I had a capability to customize AST pretty-printer, I won't need to
re-parse source code multiple times.

Personally I would be interested in adding such a customization to the
DeclPrinter for some of my work as well. I'm not currently working on, but
I might get to it in a couple of weeks. However if you think that you would
benefit from such a customization now, I would recommend you to look into
providing patches for such customization. If you are interested in learning
more about contributing to Clang feel free to take a look at the
documentation that we have:
http://llvm.org/docs/GettingStarted.html

Unfortunately in addition to Visitors, DeclPrinter also has some hidden helper printXXX methods: printTemplateParameters, printTemplateArguments, etc…
So even if we add PrintHelper to override some visitors, it won’t have an access to those very useful helpers.

This seems to me as a fundamental limitation of curiously recurring template pattern : unlike real dynamic dispatch, it allows only a single level of inheritance.

Does anyone have any ideas how to allow customization of a DeclPrinter?