CppAst provides a C/C++ parser for header files with access to a managed AST model, comments and macros for .NET.
The target primary usage of this library is to serve as a simple foundation for domain oriented PInvoke/Interop codegen
- Compatible with
net8.0- For
netstandard2.0use0.14.0version.
- For
- Uses
Clang/libclang 21.1.8.xthrough ClangSharp - Parses in-memory C/C++ text and C/C++ files from disk
- Supports C, C++ and Objective-C language modes via
CppParserOptions.ParserKind - Simple managed AST model with declarations for namespaces, classes/structs/unions, Objective-C interfaces, enums, fields/global variables, functions/methods, typedefs/using aliases and include directives
- Broad type system for primitive, record, enum, typedef, pointer, reference, array, function, block-function, qualified, template and unexposed/dependent types
- C++ support for common class, inheritance, constructor/destructor, operator, conversion, friend, inline/final/defaulted/deleted, template and specialization metadata
- Provides access to system/annotate attributes, optional token-level attributes (
CppParserOptions.ParseTokenAttributes) and comment-based attributes (CppParserOptions.ParseCommentAttribute) - Provides access to attached comments, including Doxygen comments and parameter comment commands
- Provides access to expressions for variable initializers and default parameter values (e.g.
const int x = (1 + 2) << 1exposes the initializer expression) - Provides function body source spans with
CppParserOptions.ParseFunctionBodies(not a full statement-body AST) - Provides access to macro definitions, parameters and tokens via
CppParserOptions.ParseMacros(default isfalse) - Provides target configuration helpers such as
ConfigureForWindowsMsvc(...)andParseSystemIncludesfiltering for system headers
Check the user guide documentation from the doc/ folder.
After installing the NuGet package, configure your project to select a platform RID via the RuntimeIdentifier property so the native libclang asset is restored:
<PropertyGroup>
<!-- Workaround for issue https://github.com/microsoft/ClangSharp/issues/129 -->
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' AND '$(PackAsTool)' != 'true'">$(NETCoreSdkRuntimeIdentifier)</RuntimeIdentifier>
</PropertyGroup>You can jump-start with the CppParser.Parse method:
// Parse C++ files
var compilation = CppParser.Parse(@"
enum MyEnum { MyEnum_0, MyEnum_1 };
void function0(int a, int b);
struct MyStruct { int field0; int field1;};
typedef MyStruct* MyStructPtr;
"
);
// Print diagnostic messages
foreach (var message in compilation.Diagnostics.Messages)
Console.WriteLine(message);
// Print All enums
foreach (var cppEnum in compilation.Enums)
Console.WriteLine(cppEnum);
// Print All functions
foreach (var cppFunction in compilation.Functions)
Console.WriteLine(cppFunction);
// Print All classes, structs
foreach (var cppClass in compilation.Classes)
Console.WriteLine(cppClass);
// Print All typedefs
foreach (var cppTypedef in compilation.Typedefs)
Console.WriteLine(cppTypedef);Prints the following result:
enum MyEnum {...}
void function0(int a, int b)
struct MyStruct { ... }
typedef MyStruct* MyStructPtr
This library is distributed as a NuGet package
CppAst is a lightweight model over libclang and intentionally does not expose every Clang cursor or statement node. Some known limitations are:
- Function bodies are exposed as source spans when requested, not as a full statement AST.
- Template/dependent/unexposed types are represented on a best-effort basis and may require inspecting
CppUnexposedType, display names or diagnostics for advanced C++ constructs. - Token-level attributes are still available for compatibility but are obsolete; prefer system/annotate attributes when possible.
- Type sizes and built-in aliases such as
size_tfollow the configured target triple/ABI.
This software is released under the BSD-Clause 2 license.
- ClangSharp: .NET managed wrapper around Clang/libclang
The C++ project cppast serves similar purpose although CppAst.NET does not share API or any implementation details.
Alexandre Mutel aka xoofx.
