CppAst provides a C/C++ parser for header files with access to the full AST, comments and macros for .NET Framework and .NET Core
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
- Using
Clang/libclang 20.1.2.4 - 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) << 1the(1 + 2) << 1will be retrievable as a binary expression from the AST) - Provides access to macro definitions, including tokens via the option
CppParserOptions.ParseMacros(default isfalse)
Check the user guide documentation from the doc/ folder.
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 --> <RuntimeIdentifierCondition="'$(RuntimeIdentifier)' == '' AND '$(PackAsTool)' != 'true'">$(NETCoreSdkRuntimeIdentifier)</RuntimeIdentifier> </PropertyGroup>You can jump-start with the CppParser.Parse method:
// Parse a C++ filesvarcompilation=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 messagesforeach(varmessageincompilation.Diagnostics.Messages)Console.WriteLine(message);// Print All enumsforeach(varcppEnumincompilation.Enums)Console.WriteLine(cppEnum);// Print All functionsforeach(varcppFunctionincompilation.Functions)Console.WriteLine(cppFunction);// Print All classes, structsforeach(varcppClassincompilation.Classes)Console.WriteLine(cppClass);// Print All typedefsforeach(varcppTypedefincompilation.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
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...)
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.
