The entry point for parsing a C/C++ header files with CppAst is the class CppParser.Parse methods:
CppParser.Parseallows to parse directly C++ contentCppParser.ParseFileallows to parse a single C++ file from a specified file pathCppParser.ParseFilesallows to parse a multiple C++ file from the disk
These methods return a CppCompilation object which contains:
- A property
HasErrorssets totrueif anyCppParser.Parsemethods failed to compile the header files. - A
Diagnosticsproperty to fetch any compilation errors - Several list of C/C++ elements via the
CppCompilationproperties:MacrosClassesEnumsFieldsFunctionsTypedefsNamespaces
- An access to all C++ elements parsed coming from system includes via the
Systemproperty 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
CppCompilationroot container for all global scope C/C++ elements\ - A
CppClasscan contain fields, classes/structs/unions, methods... - A
CppNamespacecan contain fields, classes/structs/unions, methods, nested namespaces
All the type class in CppAst inherit from the class CppType:
CppPrimitiveTypefor all primitive types (e.gint,char,unsigned int)CppClassfor struct, class and union. Use the propertyCppClass.ClassKindto detect which type is the underlying classCppEnumfor enum types (C++ scoped and regular enums)CppTypedeffor a typedef (e.gtypedef int MyInteger)CppPointerTypefor pointer types (e.gint*)CppReferenceTypefor reference types (e.gint&)CppArrayTypefor array types (e.gint[5])CppQualifiedTypefor qualified types (e.gconst int)CppFunctionTypefor 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
