Toward the AST cloning

Hi @all,

while my AST XML patch still seems to be in the pipeline (I hope it is in someones pipeline) I worked on at AST level source-to-source-transformations. Quickly I came at the point where I need the AST cloning feature too which was already discussed here. However I choosed a non-intrusive approach using the StmtVisitor. I attached a almost working sketch (it's a cut-out, just add the includes and namespaces and yes, I know, the DenseMap again :-). The advantage is that you get a mapping between original and cloned AST nodes (in a productive version this would be an optional feature), which is needed for rechaining gotos to cloned labels, DeclRefs to other variables aso. Indeed someone could compute such a mapping by hand but this would be overly complex and needs to traverse the AST a second time.
As you can see, the decl-cloning issue isn't resolved yet. I will work on it next week.
Opinions? Suggestions?

Best
Olaf Krzikalla

BTW: do we have a macro for stack-based variable-sized arrays? In my former company I introduced such a thing, which internally either used the gcc extension or on msvc the alloca approach (which however was somewhat tricky in the presence of loops).

cut-out.txt (2.12 KB)

Use SmallString or SmallVector.

— Gordon

I like the idea of non-intrusive approach. However, IMHO, I don’t think that it always have to be non-intrusive. I think the clone should be part of the API of the AST nodes. When I look at a clone function, I expect that the object from which clone is called is the one that is cloned. In the non-intrusive approach, the clone is called from the visitor.

One solution to the last point is to rename the method in the visitor from Clone to something like doCopy.
Or the Clone can just be implemented in the AST nodes and pass it a visitor.

On the declaration statements/ declaration references, it might be important in some cases when the declaration is local to rename the variable and all references to it. or replace the declaration statement with another statement. I suggest that a configuration object to be passed to the visitor.

Moataz

Olaf Krzikalla wrote:

Hi @all,

while my AST XML patch still seems to be in the pipeline (I hope it is
in someones pipeline)

Yes, in mine. Sorry for the delay, but I won't get to work on Clang
until summer holidays start. End of year at the university; you probably
know how it is.

Sebastian