The entry point for parsing a C/C++ header files with CppAst is the class CppParser.Parse
methods:
CppParser.Parse
allows to parse directly C++ contentCppParser.ParseFile
allows to parse a single C++ file from a specified file pathCppParser.ParseFiles
allows to parse a multiple C++ file from the disk
These methods return a CppCompilation
object which contains:
- A property
HasErrors
sets totrue
if anyCppParser.Parse
methods failed to compile the header files. - A
Diagnostics
property to fetch any compilation errors - Several list of C/C++ elements via the
CppCompilation
properties:Macros
Classes
Enums
Fields
Functions
Typedefs
Namespaces
- An access to all C++ elements parsed coming from system includes via the
System
property which is itself a container for macros, enums, fields, functions...etc.
For example to print all the struct and fields define in the global scope:
// Print all structs with all fields
foreach(var cppStruct in compilation.Classes)
{
// Skip non struct
if (cppStruct.ClassKind != CppClassKind.Struct) continue;
Console.WriteLine($"struct {cppStruct.Name}");
// Print all fields
foreach(var cppField in cppStruct.Fields)
Console.WriteLine($" {cppField}");
Console.WriteLine("}");
}
You can configure the behavior of the parser by passing a CppParserOptions
object:
var options = new CppParserOptions()
{
// Pass the defines -DMYDEFINE to the C++ parser
Defines = {
"MYDEFINE"
}
};
var compilation = CppParser.ParseFile("...", options);
All elements inherit from the base class CppElement
that provides precise source span/location information via the property CppElement.Span
A few C/C++ elements can be container of other C++ elements:
- A
CppCompilation
root container for all global scope C/C++ elements\ - A
CppClass
can contain fields, classes/structs/unions, methods... - A
CppNamespace
can contain fields, classes/structs/unions, methods, nested namespaces
All the type class in CppAst inherit from the class CppType
:
CppPrimitiveType
for all primitive types (e.gint
,char
,unsigned int
)CppClass
for struct, class and union. Use the propertyCppClass.ClassKind
to detect which type is the underlying classCppEnum
for enum types (C++ scoped and regular enums)CppTypedef
for a typedef (e.gtypedef int MyInteger
)CppPointerType
for pointer types (e.gint*
)CppReferenceType
for reference types (e.gint&
)CppArrayType
for array types (e.gint[5]
)CppQualifiedType
for qualified types (e.gconst int
)CppFunctionType
for function types (e.gvoid (*)(int, int)
)
By default, CppAst doesn't parse macros. This can be enabled via CppParserOptions.ParseMacros = true
If you are looking to parse Windows headers with the behavior of the MSVC C++ compiler, you can configure
var options = new CppParserOptions().ConfigureForWindowsMsvc();
By default, CppAst will squash a typedef to an un-named struct/union, rename the struct and discard the typedef:
typedef struct { int a; int b; } MyStruct;
will be parsed as named C++ struct and available via CppCompilation.Classes
struct MyStruct { int a; int b; };
To disable this feature you should setup CppParserOptions.AutoSquashTypedef = false