<div dir="ltr">So here is a reproducer for original email letter:<div><br></div><div><div> template <typename Type1, typename Type2></div><div> struct test {</div><div>     typedef test this_t;  // <font color="#6fa8dc">ASTPrint output : typedef test<Type1, Type2> this_t;</font></div><div> };</div><div><br></div><div> template <typename TT></div><div> struct  test<TT, int>  {</div><div>      typedef test this_t;  // <font color="#6fa8dc">ASTPrint output : typedef test<type-parameter-0-0, int> this_t;</font></div><div> };</div></div><div><br></div><div><br></div><div>And here is AST Dump:</div><div><br></div><div><div>|-ClassTemplateDecl 0x4f6ad80 <simple.cpp:1:2, line:4:2> line:2:9 test</div><div>| |-TemplateTypeParmDecl 0x4f6ab88 <line:1:12, col:21> col:21 typename depth 0 index 0 Type1</div><div>| |-TemplateTypeParmDecl 0x4f6ac28 <col:28, col:37> col:37 typename depth 0 index 1 Type2</div><div>| `-CXXRecordDecl 0x4f6ace8 <line:2:2, line:4:2> line:2:9 struct test definition</div><div>|   |-CXXRecordDecl 0x4f6b020 <col:2, col:9> col:9 implicit referenced struct test</div><div>|   `-TypedefDecl 0x4f6b0c8 <line:3:6, col:19> col:19 this_t 'test<Type1, Type2>'</div><div>|     `-InjectedClassNameType 0x4f6af90 'test<Type1, Type2>' dependent</div><div>|       `-CXXRecord 0x4f6ace8 'test'</div><div>`-ClassTemplatePartialSpecializationDecl 0x4f6b320 <line:6:2, line:9:2> line:7:10 struct test definition</div><div>  |-TemplateArgument type 'type-parameter-0-0'</div><div>  |-TemplateArgument type 'int'</div><div>  |-<b><font color="#990000">TemplateTypeParmDecl 0x4f6b140 <line:6:12, col:21> col:21 referenced typename depth 0 index 0 TT</font></b></div><div>  |-CXXRecordDecl 0x4f6b5b0 <line:7:2, col:10> col:10 implicit referenced struct test</div><div>  `-TypedefDecl 0x4f6b658 <line:8:7, col:20> col:20 this_t 'test<type-parameter-0-0, int>'</div><div>    `-InjectedClassNameType 0x4f6b440 'test<type-parameter-0-0, int>' dependent</div><div>      `-<b><font color="#990000">ClassTemplatePartialSpecialization 0x4f6b320 'test'</font></b></div></div><div><br></div><div><br></div><div>Unfortunately I've just started to learn Clang, so I'm not yet able to improve it.</div><div><br></div><div><span style="background-color:rgb(255,255,255)"><font color="#000000">typedef here has a reference to ClassTemplatePartialSpecializationDecl, and it has a TemplateTypeParmDecl that has original parameter name "TT". So it looks like information to print valid code is available in AST.</font></span></div><div><br></div><div>I have not encountered yet other cases when ASTPrinter emits invalid code. I hope it is the only one.</div><div><br></div><div>Thanks,</div><div>Roman</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-10-03 15:06 GMT-07:00 Roman Popov <span dir="ltr"><<a href="mailto:ripopov@gmail.com" target="_blank">ripopov@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">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.<div><br></div><div><div><div>Hopefully I will be able to extend AST printer to work reliably in those scenarios;<br><div><div><br></div></div></div></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">2017-10-03 0:04 GMT-07:00 Roman Popov <span dir="ltr"><<a href="mailto:ripopov@gmail.com" target="_blank">ripopov@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks for response!<div>No, I don't really need to mutate the AST. </div><div>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.</div><div><br></div><div>-Roman<br><div><br></div><div><br><br>понедельник, 2 октября 2017 г. пользователь Richard Smith  написал:<div><div class="m_-6277452547895191120h5"><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 29 September 2017 at 13:01, Roman Popov via cfe-dev <span dir="ltr"><<a>cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hello,</div><div><br></div><div>Is it possible to emit a valid C++ from AST?</div><div><br></div><div>I've tried ASTPrinter in Clang-5.0, but it seems to emit some internal Clang representations. For example for template specialization I got:</div><div><br></div><div><div>template <typename T> struct test<T, int> : sc_core::sc_module {</div><div>    typedef test<type-parameter-0-0, int> this_t; </div></div><div>...</div><div><br></div><div><br></div><div>This "type-parameter-0-0"  seem to be some Clang specifics.</div></div></blockquote><div><br></div><div>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.</div><div><br></div><div>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.</div></div></div></div>
</blockquote></div></div></div></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>