CppAst 0.12.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CppAst --version 0.12.0                
NuGet\Install-Package CppAst -Version 0.12.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CppAst" Version="0.12.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CppAst --version 0.12.0                
#r "nuget: CppAst, 0.12.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CppAst as a Cake Addin
#addin nuget:?package=CppAst&version=0.12.0

// Install CppAst as a Cake Tool
#tool nuget:?package=CppAst&version=0.12.0                

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

  • Compatible with .NET Standard 2.0+
  • Using Clang/libclang 15.0.2
  • Allow to parse in-memory C/C++ text and C/C++ files from the disk
  • Simple AST model
  • Full type system
  • Provides basic access to attributes (_declspec(...) or __attribute__((...)))
  • Provides access to attached comments
  • Provides access to expressions for variable and parameter init value (e.g const int x = (1 + 2) << 1 the (1 + 2) << 1 will be retrievable as a binary expression from the AST)
  • Provides access to macro definitions, including tokens via the option CppParserOptions.ParseMacros (default is false)

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>
    
    <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:

  • Attributes are not fully exposed (e.g in function parameters, on typedefs...)
  • Generic instance types are not fully exposed (e.g used as parameters, or as base types...)

License

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

Credits

  • 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.

Author

Alexandre Mutel aka xoofx.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on CppAst:

Package Downloads
CppAst.CodeGen

CppAst.CodeGen is an extensible P/Invoke Code Generator from C++ to C# for .NET

CppPinvokeGenerator

CppPinvokeGenerator is a simple pinvoke generator based on CppAst to generate C# for C++

HexaGen.Core

Package Description

SharpRNA.Tools

SharpRNA tooling to generate DNA YAML from C headers

GitHub repositories (6)

Showing the top 5 popular GitHub repositories that depend on CppAst:

Repository Stars
mono/SkiaSharp
SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
EgorBo/SimdJsonSharp
C# bindings for lemire/simdjson (and full C# port)
amerkoleci/Vortice.Vulkan
Cross platform .NET bindings for Vulkan, VMA, SPIRV-Cross and shaderc
BepInEx/Il2CppInterop
A tool interoperate between CoreCLR and Il2Cpp at runtime
xoofx/CppAst.CodeGen
An extensible library providing C# PInvoke codegen from C/C++ files for .NET
Version Downloads Last updated
0.21.0 161 10/16/2024
0.20.2 797 8/20/2024
0.20.1 772 5/22/2024
0.20.0 423 5/19/2024
0.19.0 84 5/19/2024
0.18.0 200 5/12/2024
0.17.0 122 5/12/2024
0.16.0 377 4/9/2024
0.15.0 550 1/9/2024
0.14.0 911 10/14/2023
0.13.0 399 8/30/2023
0.12.0 547 6/5/2023
0.11.0 1,003 3/17/2023
0.10.0 640 2/16/2023
0.9.4 522 12/1/2022
0.9.3 364 11/30/2022
0.9.2 362 11/29/2022
0.9.1 394 11/24/2022
0.9.0 1,536 11/22/2022
0.8.0 4,180 2/12/2022
0.8.0-alpha-001 3,168 5/2/2020
0.7.3 4,543 3/8/2020
0.7.2 665 2/27/2020
0.7.0 885 2/12/2020
0.6.0 1,362 11/8/2019
0.5.9 1,487 9/8/2019
0.5.8 1,356 7/16/2019
0.5.7 1,508 6/18/2019
0.5.6 884 6/16/2019
0.5.5 713 6/15/2019
0.5.4 1,159 6/15/2019
0.5.3 728 6/14/2019
0.5.2 669 6/14/2019
0.5.1 692 6/13/2019
0.5.0 705 6/12/2019
0.4.0 778 6/8/2019
0.3.0 710 5/29/2019
0.2.0 694 5/27/2019
0.1.3 725 5/27/2019
0.1.2 704 5/27/2019
0.1.1 718 5/27/2019
0.1.0 777 5/27/2019