2

Is there some sort of library for C++ which contains classes which represents C++ source code? It should of course be able to output the source code to a stream. I'm looking for something which supports most C++11 features and is also capable of generating templated constructions.

When I google for this, I'm not getting the results I want. I'm sure though, that there is something available. Maybe I'm just using the wrong terms.

0

2 Answers 2

5

Check out the Clang compiler which uses a library-based architecture to support implementing IDEs and other tools that need to process the AST. Clang fully supports all C++11 features (and some C++1y features)

More information can be found in the Clang documentation. Or in this blog which describes basic source to source transformations using clang::Rewriter.

Sign up to request clarification or add additional context in comments.

4 Comments

OP means "...able to output the source code to a stream", so the question is not about generating AST or machine-code at run-time but generating c++ code at runtime.
Thanks! Seems like it's capable of representing the whole AST. But I can't find anything about generating source code. And I don't mean IR.
@eznme He asked about "classes which represents C++ source code", i.e., an AST. And Clang does support converting the AST back to source code.
@Joe Thanks, very interesting! I didn't know that Clang supported rewriting AST representations. Just perfect.
0

This is still in a very experimental phase, but in our lab we are developing a tool (Clava), which is based on Clang and uses a Javascript-like language to query and transform a given C/C++ code. It will be made open-source by the end of August 2017.

For instance, the current default example in the demo website reads all classes and structures in the C/C++ code and creates a C++/H pair with HDF5 wrappers for those classes (however, that code generation is a mix between AST and String literals).

(Clava - Online Demo)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.