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

Roman Popov via cfe-dev cfe-dev at lists.llvm.org
Tue Oct 3 15:06:17 PDT 2017


Well... it turned out I actually need to generate valid code from AST.  In
some cases I need to generate replacements for particular template
instantiations. But template instantiations exist only in AST, but not in
the original source code.

Hopefully I will be able to extend AST printer to work reliably in those
scenarios;


2017-10-03 0:04 GMT-07:00 Roman Popov <ripopov at gmail.com>:

> Thanks for response!
> No, I don't really need to mutate the AST.
> I just had a wrong impression that AST printer is supposed to emit valid
> code, since it was doing it nicely in most cases. I can certainly grab
> everything I need to modify from original code.
>
> -Roman
>
>
>
> понедельник, 2 октября 2017 г. пользователь Richard Smith написал:
>
> 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/20171003/9f37c3d3/attachment.html>


More information about the cfe-dev mailing list