• Stars
    star
    352
  • Rank 116,935 (Top 3 %)
  • Language
    C#
  • License
    BSD 2-Clause "Sim...
  • Created about 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CppAst is a .NET library providing a C/C++ parser for header files powered by Clang/libclang with access to the full AST, comments and macros

CppAst.NET Build Status Coverage Status NuGet

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

  • 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

Related

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

Author

Alexandre Mutel aka xoofx.

More Repositories

1

markdig

A fast, powerful, CommonMark compliant, extensible Markdown processor for .NET
C#
4,037
star
2

zio

A cross-platform abstract/virtual filesystem framework with many built-ins filesystems for .NET
C#
707
star
3

dotnet-releaser

Easily build, run tests and coverage, cross-compile, package and publish your .NET library or application to NuGet and GitHub.
C#
615
star
4

UnityNuGet

Provides a service to install NuGet packages into a Unity project via the Unity Package Manager
C#
572
star
5

SharpYaml

SharpYaml is a .NET library for YAML compatible with CoreCLR
C#
312
star
6

Tomlyn

Tomlyn is a TOML parser, validator and authoring library for .NET Framework and .NET Core
C#
290
star
7

kalk

kalk is a powerful command line calculator app for developers.
C#
243
star
8

JitBuddy

Disassemble to x86/x64 ASM listing the native code generated by the JIT of a managed method.
C#
219
star
9

Varena

Varena is a .NET library that provides a fast and lightweight arena allocator using virtual memory.
C#
121
star
10

Blake3.NET

Blake3.NET is a fast managed wrapper around the SIMD Rust implementations of the BLAKE3 cryptographic hash function.
C#
116
star
11

LibObjectFile

LibObjectFile is a .NET library to read, manipulate and write linker and executable object files (e.g ELF, DWARF, ar...)
C#
91
star
12

CppAst.CodeGen

An extensible library providing C# PInvoke codegen from C/C++ files for .NET
C#
91
star
13

git-rocket-filter

Rewrite git branches in a powerful way
C#
82
star
14

gcix

A cross-platform implementation of the Immix Garbage Collector
C++
70
star
15

ManagedJit

Prototype of writing a managed JIT in C# with CoreCLR
C++
68
star
16

NWindows

NWindows is a cross-platform window creation and management library for .NET
C#
67
star
17

grpc-curl

grpc-curl is a command line tool for interacting with gRPC servers
C#
66
star
18

Broslyn

A lightweight utility library to create a Roslyn AdhocWorkspace from an existing solution or a csproj
C#
55
star
19

SharpScss

A portable cross-platform pinvoke .NET wrapper around libsass to convert SCSS to CSS
C#
52
star
20

Antlr4Ast

Antlr4Ast is a .NET library that provides a parser and abstract syntax tree (AST) for ANTLR4/g4 files.
C#
43
star
21

Monocypher.NET

.NET wrapper around the cryptographic library Monocypher https://monocypher.org/
C#
40
star
22

SharpDoc

SharpDoc is a documentation tool to generate HTML documentation for .NET projects
C#
39
star
23

SharpNng

SharpNng is a lightweight low-level managed wrapper around the Lightweight Messaging Library NNG
C#
37
star
24

SuperluminalPerf

SuperluminalPerf is a small .NET managed wrapper around the Superluminal PerformanceAPI.
C#
33
star
25

NPlug

Develop VST3 audio native plugins with .NET
C#
30
star
26

GitLib.NET

GitLib.NET is a bare metal P/Invoke wrapper around libgit2 for .NET
C#
28
star
27

jsonite

A lightweight, fast and compliant JSON serializer/deserializer/validator for .NET
C#
20
star
28

SharpRazor

A lightweight templating system using Razor
C#
19
star
29

smash

Smash provides a collection of non-cryptographic hashes for .NET
C#
15
star
30

XelaBuild

Playground for testing msbuild in a "server" mode
C#
15
star
31

SharpDiff

A fork of https://difflib.codeplex.com/
C#
13
star
32

NTar

A single file C# file to untar file entries from a tar stream.
C#
11
star
33

SharpPdbPatcher

A library/console program to replace source paths stored in a .NET PDB file
C#
9
star
34

AnalyzerPack

Repackage your current project as a Diagnostic Analyzer NuGet package
PowerShell
9
star
35

YamlSerializer

Fork of https://yamlserializer.codeplex.com/
C#
8
star
36

xoofx.github.io

Repository of my blog
HTML
7
star
37

NPlug.Samples

Samples for creating VST3 audio native plugins with NPlug and .NET7+/NativeAOT.
C#
7
star
38

BenchNativeApp

A small suite of micro-benchmarks for testing .NET Native and RyuJIT
C#
4
star
39

MarkdigAutoEmail

C#
3
star
40

dotnet-repo-xoofx

My .NET project/solution template.
C#
3
star
41

draytek-watcher

Daemon to reboot the WAN interface of a DrayTek router if it looks down
C#
2
star
42

scoop-kalk

Scoop repository for https://github.com/xoofx/kalk.
1
star
43

RoslynErrorApp

Repro project failing to run with Roslyn + .NET 5 installed
C#
1
star
44

homebrew-grpc-curl

Homebrew repository for https://github.com/xoofx/grpc-curl
Ruby
1
star
45

SharpDX.Toolkit.Game.Direct2D

Provides Direct2D and DirectWrite to Toolkit's Game class
C#
1
star
46

scoop-grpc-curl

Scoop repository for https://github.com/xoofx/grpc-curl.
1
star
47

atlassian.net-sdk

Fork of atlassian.net-sdk from bitbucket to test the JIRA REST API
C#
1
star