[cfe-dev] using clang for C++ parsing and source-to-source translation
cdavis at mymail.mines.edu
Fri Feb 26 17:22:16 PST 2010
> I am interested in using clang libraries for parsing a subset
> of C++ and then doing source-to-source translation.
> Is there an example of how this can be achieved with clang libraries?
> (parsing C++, constructing AST, resolving names, etc. and then walking AST tree and writing it out in, say, C)
Clang and LLVM can already turn C++ into C:
clang -emit-llvm source.cpp -o - | llc -march=c
If you really want to roll your own, though, here are some tips:
- Use the Frontend library to set up your environment. In particular,
take a look at the CompilerInstance/CompilerInvocation classes.
- You want to derive a class from ASTConsumer (from the AST library).
This interface accepts a fully parsed and (possibly) annotated AST as input.
- The Rewriter library does what you want. It allows you to rewrite
source code on the fly. Take a look at lib/Frontend/RewriteObjC.cpp as
an example (for ObjC->C++).
Hope that helps.
More information about the cfe-dev