C++11 source code generation

2019-05-23 05:27发布

问题:

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.

回答1:

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.



回答2:

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)