🌐
Source Code Tales
szelei.me › code-generator
Implementing a code generator with libclang · Source Code Tales
February 7, 2014 - Since libclang ultimately calls the same bits of code that the clang C++ frontend calls, it will understand everything that is valid C++. Recent builds even support C++1y (C++14, if all goes well) features. It has one little flaw: the official documentation is pretty much only the Doxygen-generated ...
🌐
Apple Open Source
opensource.apple.com › projects › llvm-clang
Apple Open Source
These libraries are built around ... language (or port an existing compiler) to use LLVM as an optimizer and code generator. Clang is an "LLVM native" C/C++/Objective-C compiler, which aims to deliver amazingly fast compiles, extremely useful error and warning messages and to ...
Discussions

c++ - C++11 source code generation - Stack Overflow
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. ... Check out the Clang ... More on stackoverflow.com
🌐 stackoverflow.com
Using Clang to generate C++ reflection data

You should crosspost this to r/programming and r/cpp, this is sweet!

More on reddit.com
🌐 r/gamedev
31
180
September 18, 2015
Tips on getting into clang source code
skirt elastic nutty profit spark sense seed run grandiose cautious This post was mass deleted and anonymized with Redact More on reddit.com
🌐 r/LLVM
3
3
January 11, 2025
Random number generator to fill lists
See example at bottom of https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution More on reddit.com
🌐 r/cpp_questions
10
1
April 29, 2021
🌐
ACCU
accu.org › conf-docs › PDFs_2017 › Sergei_Sadovnikov_-_Clang_source_code_generation_tool_slides.pdf pdf
Using Clang for source code generation - ACCU
Using Clang for source code · generation · Sergei Sadovnikov, “Kaspersky Lab”, ACCU 2017 · 1 · Does anybody like to write boilerplate · code? 2 · Introduction · ● · Enum to string conversion (and vice versa) ● · Serialization/deserialization ·
🌐
Clang
clang.llvm.org › docs › UsersManual.html
Clang Compiler User’s Manual — Clang 23.0.0git documentation
The Clang Compiler is an open-source compiler for the C family of programming languages, aiming to be the best-in-class implementation of these languages. Clang builds on the LLVM optimizer and code generator, allowing it to provide high-quality optimization and code generation support for ...
🌐
GitHub
github.com › biojppm › regen
GitHub - biojppm/regen: Easy C++ reflection and code generation
regen is a python3 package providing C/C++ reflection and source-code generation. You provide your own code generation templates , and have full control over where the generated code goes.
Starred by 39 users
Forked by 7 users
Languages   Python 71.2% | C++ 19.4% | CMake 8.9% | Shell 0.5% | Python 71.2% | C++ 19.4% | CMake 8.9% | Shell 0.5%
🌐
Code Browser
codebrowser.dev › llvm › clang
llvm/clang/ Source Tree
Generated on 2025-Jun-11 Powered by Code Browser 2.1 Generator usage only permitted with license · Browse the source code of llvm/clang/ online
Find elsewhere
🌐
GitHub
github.com › flexferrum › autoprogrammer
GitHub - flexferrum/autoprogrammer: C++ to C++ code generation tool (enum2string conversion, serialization, reflection etc.) · GitHub
This tool helps you dramatically reduce the amount of boilerplate code in your C++ projects. Based on clang frontend, the 'autoprogrammer' parses your C++ source files and generates new set C++ sources.
Starred by 97 users
Forked by 16 users
Languages   C++ 72.5% | CMake 27.3% | Dockerfile 0.2%
🌐
Medium
issamvb.medium.com › llvm-internals-independent-code-generator-3128854ebab1
LLVM Internals: Independent Code Generator | by lahlali issam | Medium
May 7, 2021 - The front end that parses source code, checking it for errors, and builds a language-specific Abstract Syntax Tree (AST) to represent the input code. The optimizer: its goal is to do some optimization on the AST generated by the front end. The back end: that generates the final code to be executed by the machine, it depends on the target. The most important difference of its design is that Clang is based on LLVM, the idea behind LLVM is to use LLVM Intermediate Representation (IR), it’s like the bytecode for Java.
🌐
Clang
clang.llvm.org
Clang C Language Family Frontend for LLVM
The Clang project provides a language front-end and tooling infrastructure for languages in the C language family (C, C++, Objective C/C++, OpenCL, and CUDA) for the LLVM project. Both a GCC-compatible compiler driver (clang) and an MSVC-compatible compiler driver (clang-cl.exe) are provided. You can get and build the source today.
🌐
Clang
clang.llvm.org › features.html
Clang - Features and Goals
The beauty of the clang approach is that it does not restrict how you use it. In particular, it is possible to use the clang preprocessor and parser to build an extremely quick and light-weight on-the-fly code generator (similar to TCC) that does not build an AST at all.
🌐
GitHub
github.com › llvm › llvm-project
GitHub - llvm/llvm-project: The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. · GitHub
3 days ago - C-like languages use the Clang frontend. This component compiles C, C++, Objective-C, and Objective-C++ code into LLVM bitcode -- and from there into object files, using LLVM.
Starred by 39K users
Forked by 17.6K users
Languages   LLVM 41.2% | C++ 30.4% | C 12.5% | Assembly 11.2% | MLIR 1.6% | Python 0.8%
🌐
Clang
clang.llvm.org › extra › clang-doc.html
Clang-Doc — Extra Clang Tools 23.0.0git documentation - LLVM
clang-doc is a tool for generating C and C++ documentation from source code and comments.
🌐
Reddit
reddit.com › r/gamedev › using clang to generate c++ reflection data
r/gamedev on Reddit: Using Clang to generate C++ reflection data
September 18, 2015 -

Hi guys! I made a quick post outlining how I used to libclang to generate reflection data in C++. Source is included in the respository.

You can find the GitHub respository here.

Below is a copy and paste of the post (excuse any minor markdown translation errors).

Preface

I set out this summer (2015) to implement a flexible reflection system for the game project I'm working on. This repository contains a skeleton for parts of the system that I prototyped throughout the summer. With the proper dependencies and build system setup, you should have enough to integrate into your engine / application without much fuss.

Quick Intro

As a statically typed language, C++ wasn't designed to facilitate runtime type information. Instead, it's crazy fast and optimization friendly. Games are performance critical applications - it is for this reason that C++ is basically the standard backend.

Type introspection is crucial for complex / large code bases that need to interface with tools (i.e. a game editor). Unless you're a team of all programmers (I'm sorry if that's the case) it is effectively impossible to iterate upon a larger game without some set of tools to abstract away code (especially in 3D). Without type introspection, you can expect to copy and paste a lot of boilerplate code. This is absurdly tedious and undesirable.

The good news is that there are tons and tons of great resources out there for "extending" C++ to include meta information within your code base. The most common approaches you'll find are as follows:

  • Using macros and templates to simplify the craziness that is writing the aforementioned boilerplate code.

  • Parsing your code to generate the crazy boilerplate code.

The latter technique isn't adopted nearly as much as the former, but feels like it's becoming much more common. With that said, I chose to use the generation technique. I'm pretty glad I did.

The purpose of this repository is to be a simple jumpstart reference for those interested in implementing the generation method in their own code base.

Here are a few links that cover more specifics on the concept (specifically in the realm of C/C++)

  • GDC: Physics for Game Programmers - Debugging (it's relevant, I swear!)

  • GDC: Robustification Through Introspection and Analysis Tools (Avoiding Developer Taxes)

  • Game Engine Metadata Creation with Clang

  • Parsing C++ in Python with Clang

Goals

Make the pipeline as hands off as possible.

Specifically, you shouldn't have to jump through a bunch of hoops just to expose your code to the reflection runtime library. Make changes to your code, recompile, and the changes are reflected immediately (yep, it was intended).

No extra button clicks or steps to synchronize the reflection data.

Provide rich functionality in the runtime library.

If we're going through all this trouble in the first place, might as well make it worth while!

Avoid huge build times.

We're effectively compiling our code twice. First to parse our code base and understand it as intricately as a compiler does, then to actually compile it as per usual (with the addition of our generated source).

This is one of the downsides to the generation approach. Instead of manually writing these macros inline with our source, we're using the brains of a compiler. However, we would much rather sacrifice a little bit shorter build times for the luxury of cleaner, less cluttered code.

Unfortunately, this also implies creating a much less intuitive build pipeline. Don't worry though! I have some nifty diagrams for you.

Pipeline

In our engine (we call it Ursine Engine, because we're dangerously clever and played on the fact that our team name is Bear King), we use CMake for managing most aspects of the overall build pipeline. CMake is a horribly wonderful tool that I've come to love despite hating it at the same time. It allows us to generate solutions for most IDEs that anyone on the team likes to use, although currently, everyone is using Visual Studio 2015 (finally some C++14, baby!).

CMake makes this pipeline surprisingly simple which was a relief. I won't go into much specific detail, but I'll provide relevant snippets of the integration into our engine a little later when I describe the code in this repository.

Here's a diagram of the entire pipeline from writing the source, to building your game / application.

Diagram

Code

The respository has two parts - Parser and Runtime.

  • Parser is for the command line source parsing tool. (requires Boost 1.59.0 and libclang 3.7.0)

  • Runtime is for the reflection runtime library.

CMake Prebuild Example

This is basic example of adding the prebuild step to an existing target in CMake.

Note: this assumes you have created empty files for ${META_GENERATED_HEADER} and ${META_GENERATED_SOURCE} and added it to the list of sources when calling add_executable( ). Otherwise, CMake will complain about the files not existing initially.

# assume a target has been created, with add_executable( ... )
set(PROJECT_NAME "Example")

# path to the reflection parser executable
set(PARSE_TOOL_EXE "ReflectionParser.exe")

# input source file to pass to the reflection parser compiler
# in this file, include any files that you want exposed to the parser
set(PROJECT_META_HEADER "Reflection.h")

# generated file names
set(META_GENERATED_HEADER "Meta.Generated.h")
set(META_GENERATED_SOURCE "Meta.Generated.cpp")

# fetch all include directories for the project target
get_property(DIRECTORIES TARGET ${PROJECT_NAME} PROPERTY INCLUDE_DIRECTORIES)

# flags that will eventually be based to the reflection parser compiler
set(META_FLAGS "")

# build the include directory flags
foreach (DIRECTORY ${DIRECTORIES})
    set(META_FLAGS ${meta_flags} "\\-I${DIRECTORY}")
endforeach ()

# include the system directories
if (MSVC)
    # assume ${VS_VERSION} is the version of Visual Studio being used to compile
    set(META_FLAGS ${META_FLAGS} 
        "\\-IC:/Program Files (x86)/Microsoft Visual Studio ${VS_VERSION}/VC/include"
    )
else ()
    # you can figure it out for other compilers :)
    message(FATAL_ERROR "System include directories not implemented for this compiler.")
endif ()

# add the prebuild command that invokes the reflection parser executable
add_custom_command(
    TARGET ${PROJECT_NAME}
    PRE_BUILD
    COMMAND call "${PARSE_TOOL_EXE}"
    --target-name "${PROJECT_NAME}"
    --in-source "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_SOURCE_DIR}/${PROJECT_META_HEADER}"
    --out-header "${META_GENERATED_HEADER}"
    --out-source "${META_GENERATED_SOURCE}"
    --flags ${META_FLAGS}
)

String Templates

Generating code is usually a pretty ugly process.

Instead of writing the characters manually (i.e. output += "REGISTER_FUNCTION(" + name + ")"), I wanted to use "String Templates". That is why I chose Mustache. I found a simple header only implemenation, which is included in the Parser section.

In the Generate Source Files section of the pipeline diagram, you'll notice two steps. "Compile" and "Render". Compile simply takes all of the types that we've extracted and compiles the data to be referenced in Mustache. Render actually renders the templates and writes them to the configured output files.

In the Templates folder of the repository, you'll find the mustache template files referenced in the reflection parser.

Type Meta Data

One of the biggest features that I wanted to implement in the runtime library is being able to add meta data to types at compile time.

If you've ever used C#, you know they have a pretty groovy reflection system built into the language. I really like their syntax for Attributes, which is a way to add meta data to language types / constructs.

The closest I could get to this style, was with the use of Macros. C++11 introduced Attribute Specifiers as a way to hint compilers on intended behavior or add language extensions. Unfortunately, compiler support varies widely, and as mentioned it's only managed at compile time.

Luckily for us, Clang supports the attribute annotate( ). You can extract the contents of an annotation with libclang.

The syntax for this attribute look something like this.

__attribute__(( annotate( "Hey look at this annotation!" ) ))

You might be thinking, "But it's only for Clang.. how will this work in MSVC?"

More good news! libclang preprocesses source files, so we can use preprocessor directives. In the source parsing tool, I define __REFLECTION_PARSER__ before compiling. We can use this to make a nice solution for all compilers.

#if defined(__REFLECTION_PARSER__)
#   define Meta(...) __attribute__((annotate(#__VA_ARGS__)))
#else
#   define Meta(...)
#endif

We would use it like so.

Meta(Mashed)
int potatoes;

Now that I could annotate code, I needed to define how I would interact with it. Initially I assumed key value pairs separated by commas, like so.

Meta(Key = Value, Key2, Key = "Yep!")

But after reviewing this approach with my teammate Jordan, he came up with the brilliant idea of doing exactly what C# does, and that is using user defined types as annotations, queryable at runtime. So I came up with this.

struct Mashed : public MetaProperty { };

Meta(Mashed)
int potatoes;

Heres how it works - I treat all values delimited by commas as constructors. If a value doesn't have parenthases, it's assumed to be a default constructor.

For each constructor, I then extract the arguments provided. When I generate the source, I simply paste the extracted arguments as a constructor call of the provided type. The value is converted to a Variant and accessible at runtime. This allows us to do some really awesome things.

enum class SliderType 
{
    Horizontal,
    Vertical
};

struct Slider : public MetaProperty 
{
    SliderType type;
    
    Slider(SliderType type) 
        : type( type ) { }
};

struct Range : public MetaProperty 
{
    float min, max;
    
    Range(float min, float max) 
        : min( min )
        , max( max ) { }
};

Meta(Range(0.0f, 1.0f), Slider(SliderType::Horizontal))
float someIntensityField;

One of the coolest things about this, aside from type safety, is that Visual Studio correctly syntax highlights the contents of the macro and also provides intellisense! It's a beautiful thing. Here's a more complete example of interfacing with it at runtime using the runtime library.

struct SoundEffect 
{
    Meta(Range(0.0f, 100.0f))
    float volume;
};

int main(void)
{
    // you can also use type meta::Type::Get( "SoundEffect" ) based on a string name
    Field volumeField = typeof( SoundEffect ).GetField( "volume" );
    
    // meta data for the volume field
    MetaManager &volumeMeta = soundEffectType.GetMeta( );
    
    // getting the "Range" property, then casting the variant as a range
    Range volumeRange = volumeMeta.GetProperty( typeof( Range ) ).GetValue<Range>( );
    
    // 0.0f
    float min = volumeRange.min;
    
    // 100.0f
    float max = volumeRange.max;
    
    return 0;
}

Function Binding

Another notoriously difficult or convoluted process in managing reflection info in C++ is dynamically invoking functions / methods.

The most common approach is to store raw function / method pointers and calculate the offsets of arguments. The result is a ton of templates and difficult to follow operations. Not to mention, I can't imagine it's fun to debug!

In libclang, you're able to easily extract signatures from functions. With this in mind, I came up with the most simple approach that I could think of. Wrapping function calls in generated lambdas.

Here's a simple demonstration of the concept.

int foo(int a, float b, double c) 
{ 
    return 0;
}

auto fooWrapper = [](int a, float b, double c) 
{
    return foo( a, b, c );
};

// same behavior as foo( 0, 1.0f, 2.0 );
fooWrapper( 0, 1.0f, 2.0 );

In the context of our runtime library, here's an example of something that might be generated for a class method.

class DemoClass 
{
public:
    int someMethod(int a)
    {
        return a;
    }
};

auto someMethodWrapper = [](Variant &obj, ArgumentList &args)
{ 
    auto &instance = obj.GetValue<DemoClass>( );
    
    return Variant {
        instance.someMethod(
            args[ 0 ].GetValue<int>( )
        )
    };
};

That's it! It's much less compilicated than the previously mentioned approach. This concept is also applied to fields / globals with their getters and setters.

There are some downsides though:

  • Larger code size. For each generated lambda, the compiler has to generate a bunch of symbols behind the scenes.

  • Larger compile times.

  • Decent amount of indirection just for one function call.

You shouldn't have to worry too much however. If like most people, you use reflection for editor functionality, not a physics simulation. Performance in most cases is not critical.

Here's a more complete example of dynamically calling functions with the runtime library.

struct SoundEffect 
{
    float volume;

    void Load(const char *filename);
};

int main(void)
{
	Type soundEffectType = typeof( SoundEffect );
	Field volumeField = soundEffectType.GetField( "volume" );

	// the runtime supports overloading, but by default returns the first overload
	Method loadMethod = soundEffectType.GetMethod( "Load" );

	// creates an instance of a sound effect
	Variant effect = soundEffectType.Create( );

	// effect.volume is now 85
	volumeField.SetValue( effect, 85.0f );

	// 85
	volumeField.GetValue( effect );

	// effect.Load is called
	loadMethod.Invoke( effect, { "Explosion.wav" } );

	return 0;
}
🌐
Clang
clang.llvm.org › docs › CommandGuide › clang.html
clang - the Clang C, C++, and Objective-C compiler — Clang 23.0.0git documentation
The Clang Static Analyzer is a tool that scans source code to try to find bugs through code analysis. This tool uses many parts of Clang and is built into the same driver. Please see <https://clang-analyzer.llvm.org> for more details on how to use the static analyzer. ... Run the preprocessor stage. ... Run the preprocessor, parser and semantic analysis stages. ... Run the previous stages as well as LLVM generation and optimization stages and target-specific code generation, producing an assembly file.
🌐
MaskRay
maskray.me › blog › 2023-09-24-a-deep-dive-into-clang-source-file-compilation
A deep dive into Clang's source file compilation | MaskRay
September 24, 2023 - This means that Clang generates a function at a time. BackendConsumer::HandleTranslationUnit invokes EmitBackendOutput to create an LLVM IR file, an assembly file, or a relocatable object file. EmitBackendOutput establishes an optimization pipeline and a machine code generation pipeline.
🌐
Clang
clang.llvm.org › get_started.html
Getting Started: Building and Running Clang - LLVM.org
But if someone later becomes a contributor, since they can't push code from a shallow clone, it needs to be converted into a full clone: ... This builds both LLVM and Clang in release mode. Alternatively, if you need a debug build, switch Release to Debug. See frequently used cmake variables for more options. cmake -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" ../llvm ... Note: For subsequent Clang development, you can just run make clang. CMake allows you to generate project files for several IDEs: Xcode, Eclipse CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3.
🌐
GitHub
github.com › SebastianBach › clang-format-generator
GitHub - SebastianBach/clang-format-generator: Command line tool that creates a clang-format file from a formatted C++ source code file. · GitHub
Command line tool that creates a clang-format file from a formatted C++ source code file. - SebastianBach/clang-format-generator
Author   SebastianBach
🌐
ScienceDirect
sciencedirect.com › science › article › pii › S2352711019302122
Clava: C/C++ source-to-source compilation using LARA - ScienceDirect
July 21, 2020 - This article presents Clava, a Clang-based source-to-source compiler, that accepts scripts written in LARA, a JavaScript-based DSL with special constructs for code queries, analysis and transformations. Clava improves Clang’s source-to-source capabilities by providing a more convenient and flexible way to analyze, transform and generate C/C++ code, and provides support for building strategies that capture run-time behavior.