[cfe-dev] AST manipulation from 2 cpp files

Andrey Tarasevich tarasevich at cs.uni-saarland.de
Thu Nov 29 06:16:20 PST 2012


Hi!

I'm using clang for some source-to-source transformations of the arbitrary C code. Until now it was pretty enough for me to use rewriter API for such transformation, but now I want to perform manipulations on AST directly, i.e. I want to take AST node from one source file and insert it into another. For instance I want to copy some arbitrary for-loop from file test_1.cpp, then resolve all the variable and type conflicts and insert that loop into another AST in file test_2.cpp. 

I'm aware, that there is no AST copy mechanism in clang, but I can implement it on top of TreeTransform class, which will give me flexibility to some degree. At this point I have only one problem - how to use AST from different cpp files in one context? As far as I know for each compiler instance and thus cpp file there is separate ASTContext and AST node can't be used outside its context. 

To sum it up - how can I use AST node from one source file in context of another cpp file, i.e. in another ASTContext? Is it possible? Is there any workarounds?

Thanks in advance,
Andrey Tarasevich



More information about the cfe-dev mailing list