Home

Awesome

CppAst.NET Build Status Coverage Status NuGet

<img align="right" width="160px" height="160px" src="https://raw.githubusercontent.com/xoofx/CppAst.NET/main/img/cppast.png">

CppAst provides a C/C++ parser for header files with access to the full AST, comments and macros for .NET Framework and .NET Core

Purpose

The target primary usage of this library is to serve as a simple foundation for domain oriented PInvoke/Interop codegen

Features

Documentation

Check the user guide documentation from the doc/ folder.

Usage Example

Setup

After installing the NuGet package, you need to modify your csproj to select a Platform RID via the RuntimeIdentifier property:

  <PropertyGroup>
    <!-- Workaround for issue https://github.com/microsoft/ClangSharp/issues/129 -->
    <RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' AND '$(PackAsTool)' != 'true'">$(NETCoreSdkRuntimeIdentifier)</RuntimeIdentifier>
  </PropertyGroup>

Code

You can jump-start with the CppParser.Parse method:

// Parse a 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

Binaries

This library is distributed as a NuGet package NuGet

Known issues

The library libclang used by this project has some known issues and limitations:

License

This software is released under the BSD-Clause 2 license.

Credits

Related

The C++ project cppast serves similar purpose although CppAst.NET does not share API or any implementation details.

Author

Alexandre Mutel aka xoofx.