Bug 11806

Hi,

there is a bug http://llvm.org/bugs/show_bug.cgi?id=11806 about -ast-print not being production quality. I am using clang to rewrite C++ source. Is anyone else using clang in that way? Some comments on the bug pages suggest, that nobody is using this feature.

I am not sure it is a proper way of handling it - I have put a few patches on the bug page and pages of bugs marked as duplicates.

Regards,
Grzegorz

Yes, various people use Clang to rewrite C++ source, but mainly not by pretty-printing. Instead, targeted rewrites are performed on the original source file in each place that needs to be changed (see the Rewriter class). This has the advantage of preserving source formatting, comments, macros, header inclusions, etc. Generally speaking, pretty-printing isn’t a good way to perform rewrites if you want to produce code for a person to maintain, because it loses all this information. Also, since the AST is intended to be essentially immutable, if you want to perform rewrites you are likely to find it easier to perform them textually rather than writing a pass to build a rewritten AST.

That said, making the AST pretty-printing more correct would be a good thing. Patches attached to bugs are not likely to get much attention; instead, you should mail the patches to cfe-commits@cs.uiuc.edu and ask for them to be reviewed / committed.

My work is based on the Scout project: They have provided quite extensive AST rewrite facilities, including cloning of the subtrees, and their approach seems quite elegant to me. They use a hybrid approach - they use pretty printing on the modified functions, the rest is copied from the original. It is a pity, that it is not possible to clone all kinds of AST nodes. – Grzegorz

And please include test cases with your patches, it makes review easier and prevents future regressions.

- Ben

My work is based on the Scout project:

https://fusionforge.zih.tu-dresden.de/scm/viewvc.php?root=hicfd&view=rev

Welcome on board :slight_smile:

They have provided quite extensive AST rewrite facilities, including
cloning of the subtrees, and their approach seems quite elegant to me.
They use a hybrid approach - they use pretty printing on the modified
functions, the rest is copied from the original.It is a pity, that it
is not possible to clone all kinds of AST nodes.

Most of the design origins from 2009. TBack then there was no TreeTransform. Today IMHO cloning could be done by deriving TreeTranform and set AlwaysRebuild to true. Actually I want to change StmtClone.cpp in that way. However TreeTranform.h isn't part of the headers yet. Thus I hesitate...

Best Olaf

So may I point you to my post

http://lists.cs.uiuc.edu/pipermail/cfe-dev/2012-July/023234.html

Nobody commented on this. OK, it is not a patch. But I don't have a clue of all the changes causing the bug. However, all worked fine before r156228.

Best Olaf