[cfe-dev] Is ASTPrinter supposed to emit valid C++ code?

Richard Smith via cfe-dev cfe-dev at lists.llvm.org
Mon Oct 2 19:43:03 PDT 2017

On 29 September 2017 at 13:01, Roman Popov via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hello,
> Is it possible to emit a valid C++ from AST?
> I've tried ASTPrinter in Clang-5.0, but it seems to emit some internal
> Clang representations. For example for template specialization I got:
> template <typename T> struct test<T, int> : sc_core::sc_module {
>     typedef test<type-parameter-0-0, int> this_t;
> ...
> This "type-parameter-0-0"  seem to be some Clang specifics.

We generally try to preserve type sugar, and printing "type-parameter-0-0"
here is undesirable. If you could figure out how we're losing the type
sugar and fix it, we would probably take the fix. However, while we make a
best-effort attempt to produce valid code from the AST printer, there is in
general no guarantee that the output is valid C++; it is intended for
consumption primarily by humans, not by a compiler.

Usually, people asking about the AST printer are trying to perform some
kind of refactoring by mutating the AST and pretty-printing the output. We
do not recommend this approach. Instead, we recommend that you treat the
AST as an immutable representation of the original code, decide on what
transformations you want to perform, and then produce a set of edits to the
original source code that have the desired effect. Clang's Tooling library
provides support utilities for doing this.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20171002/dee32f40/attachment.html>

More information about the cfe-dev mailing list