I am working on a source-to-source translator plugin for clang to transform CUDA into OpenCL. 
I have made my own ASTConsumer and am using the Rewriter class to perform the rewrites. The way I traverse the AST is manually, stopping to rewrite the AST nodes of interest.<br><br>Much of the rewriting involves changing function calls into other ones (CUDA API calls to equivalent OpenCL ones), along with rewriting some data structures. My issue is with nested expressions in which there may be multiple portions that must be rewritten at separate levels. The simplest way I could think of is to rewrite the portions I have to in a string and combine those with clang's statement printer so as to get a final string. I would then pass the string to the Rewriter and replace the existing Expr at the top level.<br>
<br>For example, let's look at creating a dim3 in CUDA:<br><br>dim3 a(1, 2, 3);<br>a = dim3(a.y, a.z, a.x);<br><br>In OpenCL, the dim3s would be rewritten as size_t arrays, and would be initialized as arrays normally are:<br>
<br>size_t a[3] = {1, 2, 3};<br>a = {a[1], a[2], a[0]};<br><br>The issue here is that, in rewriting the dim3 constructor expression on the second line, I do not know how to recursively rewrite the argument expressions. I currently rewrite the constructor to use braces, but then just use the Stmt class's printPretty call to print the argument expressions. As a result, the argument expressions are not rewritten. This is what I get, then:<br>
<br>size_t a[3] = {1, 2, 3};<br>
a = {a.y, a.z, a.x};<br><br>The reason for this is that I use clang's existing printPretty call for each argument (to pass to the Rewriter), which allows me to avoid creating my own printing methods for every possible Expr type. In order to make use of this but allow for my own printing when rewrites are necessary, my idea is to create a new class that extends clang's StmtPrinter. In that way, I could overwrite the methods that print Expr classes of interest (say, MemberExprs that reference members of dim3s). As for the rest, I would allow the existing Visit* implementations to do what they normally do. However, StmtPrinter is an internal class, so extending it outside of clang isn't really possible.<br>
<br>So, my questions are:<br><br>1. Is this a good approach, trying to rewrite expressions recursively by outputting the rewrites to a string?<br>2. Would it be possible to extend the StmtPrinter?<br>3. If not, is there a good alternative?<br>
<br><br>Thanks,<br>Gabriel<br>