ClangToStringTemplate is a C++ to StringTemplate code generation framework. ClangToStringTemplate enables you to leverage the Clang parser with StringTemplate4. This combination allows you to generate C++ based on structurally similar C++ code generalized as custom ASTs. Designed with modularity in mind, you may also mix and match your custom ASTs and String Template Group files. This project also provides an optional Vim plugin that can be used to generate C++ files on the fly.
You must have working installations of Python3, Clang, Maven, and the JDK (
Projects using ClangToStringTemplate must have CMake (compile_commands.json
enabled. You can do so with the following CMake option:
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
This allows the tool to include the dependencies to fully parse your code with Clang.
Run ./setup.sh
in ClangToStringTemplate. The script will output the path to your jar.
Rename config-sample.json
to config.json
and place into the cpp-gen folder. Modify your config.json
in cpp-gen to include the jar path.
If you're using the Vim plugin, you'll need to place the cpp-gen folder where you store your Vim plugins. Additionally, include the config.json
in that same folder.
ClangToStringTemplate includes mock generation for interfaces as a fully functioning example demonstrating its usage.
ClangToStringTemplate also includes a Vim plugin integration. The plugin can automatically gather the necessary file paths to invoke the tool and seamlessly fit into your development workflow.
You can install the Vim plugin with vim-plug
with:
Plug 'Azul3DInc/ClangToStringTemplate', {'rtp': 'cpp-gen/'}
Remember, you'll need to add the CppGeneratorTool.jar to the Plugin's config.json
. The Vim plugin cannot be installed standalone and you must also clone CppGeneratorTool and build the jar.
The cpp-gen folder will be located in your vim plugins folder.
To see an example, first build Application
in Test_Application
with CMake. Then vim into an interface in Test_Application
and type :GenerateMock
.
If you want to generate based on your own templates and ASTs, you must implement them for the CppGeneratorTool and update its jar file.
Here is a list of steps to get started:
- Write custom AST in CppGeneratorTool that builds itself from the Clang AST.
- Add AST to jar command line arguments
- Write .stg templates
- (optional) extend Vim plugin to call cpp-gen.py with your AST and templates
Here's an example python invocation:
python3 cpp-gen.py src/Interface.cpp include/Interface.h build/compile_commands.json MockSource.stg MockImplementationFile MockHeader.stg MockSpecificationFile \
--ast-template interface --output-paths src/Mocks/Mock.cpp include/Mocks/Mock.h
- In the event of a segmentation fault during the parsing process, consider upgrading your version of clang in
config.json
. Known to fix this issue with clang++15. - If the tool is unable to resolve dependencies, you can use the
extraIncludePaths
option inconfig.json
to include absolute paths.
For licensing information, including third-party dependencies, see the LICENSES file.