C++ Reflection using clang
The executables in the “bin” directory can be run directly but require the MSVC 2017 x86 redistributables.
clReflectTest is an up-to-date test of the clReflect library, showing how to build a database and load it at runtime.
C++ primitives are not reflected by default and need to be marked by clcpp_reflect
Reflection Specs or using reflect attributes. More details can be found here.
First use clscan
to parse your C++ files and output readable databases of type information:
clscan.exe test.cpp --output test.csv
Each of these databases can then be merged into one for your module using clmerge
:
clmerge.exe output.csv input0.csv input1.csv input2.csv ...
Finally you can use clexport
to convert this text database to a binary, memory-mapped database that can be quickly loaded by your C++ code:
clexport output.csv -cpp output.cppbin
This will give you a runtime loadable database with two limitations:
Make sure you pay attention to all reported warnings and inspect all output log files if you suspect there is a problem!
All platforms support parsing the output of your compiler’s MAP file and matching function address to their reflected equivalents in the database. Modify your clexport
calls to achieve this:
clexport output.csv -cpp output.cppbin -map module.map
To use the constant-time, stringless GetType
and GetTypeNameHash
functions you need to ask clmerge
to generate their implementations for you:
clmerge.exe output.csv -cpp_codegen gengettype.cpp input0.csv input1.csv input2.csv ...
Compile and link this generated C++ file with the rest of your code, load your database, call the generated initialisation function to perform one-time setup and all features of clReflect are available to you.
clscan
can forward compiler options to the underlying Clang compiler. Add --
at the end of your clscan
command-line and pass any options you want. For example, these are the advised command-line options for generating databases are Windows MSVC compatible:
clscan test.cpp --spec_log spec_log.txt --ast_log ast_log.txt --output test.csv -- \
-D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS -D__clcpp_parse__ \
-fdiagnostics-format=msvc -fms-extensions -fms-compatibility -mms-bitfields -fdelayed-template-parsing -std=c++17 -fno-rtti