Skip to content

Commit

Permalink
Add support for ClangSharp 15.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Nov 22, 2022
1 parent fa835b9 commit 24ccb48
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 15 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CppAst provides a C/C++ parser for header files with access to the full AST, com
## Features

- Compatible with `.NET Standard 2.0+`
- Using `Clang/libclang 10.0.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
Expand Down
6 changes: 3 additions & 3 deletions src/CppAst.Tests/CppAst.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="nunit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1">
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/CppAst/CppAst.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
<None Include="../../img/cppast.png" Pack="true" PackagePath="/logo.png" />
<None Include="../../readme.md" Pack="true" PackagePath="/" />

<PackageReference Include="ClangSharp" Version="10.0.0-beta" />
<PackageReference Include="ClangSharp" Version="15.0.2" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MinVer" Version="2.5.0">
<PackageReference Include="MinVer" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
75 changes: 68 additions & 7 deletions src/CppAst/CppModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private CppContainerContext GetOrCreateDeclarationContainer(CXCursor cursor, voi
}
else
{
var templateParameters = ParseTemplateParameters(cursor, cursor.Type, new CXClientData((IntPtr)data));
var templateParameters = ParseTemplateArguments(cursor, cursor.Type, new CXClientData((IntPtr)data));
if (templateParameters != null)
{
cppClass.TemplateParameters.AddRange(templateParameters);
Expand Down Expand Up @@ -237,9 +237,13 @@ private CXChildVisitResult VisitMember(CXCursor cursor, CXCursor parent, void* d
break;

case CXCursorKind.CXCursor_TypedefDecl:
element = VisitTypeDefDecl(cursor, data);
break;

case CXCursorKind.CXCursor_TypeAliasDecl:
case CXCursorKind.CXCursor_TypeAliasTemplateDecl:
element = VisitTypeDefDecl(cursor, data);

element = VisitTypeAliasDecl(cursor, data);
break;

case CXCursorKind.CXCursor_FunctionTemplate:
Expand Down Expand Up @@ -1538,6 +1542,43 @@ private bool ParseAttribute(TokenIterator tokenIt, out CppAttribute attribute)
return true;
}

private CppType VisitTypeAliasDecl(CXCursor cursor, void* data)
{
var fulltypeDefName = clang.getCursorUSR(cursor).CString;
if (_typedefs.TryGetValue(fulltypeDefName, out var type))
{
return type;
}

var contextContainer = GetOrCreateDeclarationContainer(cursor.SemanticParent, data);
var underlyingTypeDefType = GetCppType(cursor.TypedefDeclUnderlyingType.Declaration, cursor.TypedefDeclUnderlyingType, cursor, data);

var typedefName = GetCursorSpelling(cursor);

if (AutoSquashTypedef && underlyingTypeDefType is ICppMember cppMember && (string.IsNullOrEmpty(cppMember.Name) || typedefName == cppMember.Name))
{
cppMember.Name = typedefName;
type = (CppType)cppMember;
}
else
{
var typedef = new CppTypedef(GetCursorSpelling(cursor), underlyingTypeDefType) { Visibility = contextContainer.CurrentVisibility };
contextContainer.DeclarationContainer.Typedefs.Add(typedef);
type = typedef;
}

// The type could have been added separately as part of the GetCppType above
if (_typedefs.TryGetValue(fulltypeDefName, out var cppPreviousCppType))
{
Debug.Assert(cppPreviousCppType.GetType() == type.GetType());
}
else
{
_typedefs.Add(fulltypeDefName, type);
}
return type;
}

private CppType VisitTypeDefDecl(CXCursor cursor, void* data)
{
var fulltypeDefName = clang.getCursorUSR(cursor).CString;
Expand Down Expand Up @@ -1739,7 +1780,7 @@ private CppType GetCppTypeInternal(CXCursor cursor, CXType type, CXCursor parent
}

var cppUnexposedType = new CppUnexposedType(type.ToString()) { SizeOf = (int)type.SizeOf };
var templateParameters = ParseTemplateParameters(cursor, type, new CXClientData((IntPtr)data));
var templateParameters = ParseTemplateArguments(cursor, type, new CXClientData((IntPtr)data));
if (templateParameters != null)
{
cppUnexposedType.TemplateParameters.AddRange(templateParameters);
Expand Down Expand Up @@ -1820,17 +1861,37 @@ protected void WarningUnhandled(CXCursor cursor, CXCursor parent)
RootCompilation.Diagnostics.Warning($"Unhandled declaration: {cursor.Kind}/{cursor} in {parent}.", cppLocation);
}

private List<CppType> ParseTemplateParameters(CXCursor cursor, CXType type, CXClientData data)
private List<CppType> ParseTemplateArguments(CXCursor cursor, CXType type, CXClientData data)
{
var numTemplateArguments = type.NumTemplateArguments;
if (numTemplateArguments < 0) return null;

var templateCppTypes = new List<CppType>();
for (var templateIndex = 0; templateIndex < numTemplateArguments; ++templateIndex)
{
var templateArg = type.GetTemplateArgumentAsType((uint)templateIndex);
var templateCppType = GetCppType(templateArg.Declaration, templateArg, cursor, data);
templateCppTypes.Add(templateCppType);
var templateArg = type.GetTemplateArgument((uint)templateIndex);

switch (templateArg.kind)
{
case CXTemplateArgumentKind.CXTemplateArgumentKind_Type:
var templateArgType = templateArg.AsType;
//var templateArg = type.GetTemplateArgumentAsType((uint)templateIndex);
var templateCppType = GetCppType(templateArgType.Declaration, templateArgType, cursor, data);
templateCppTypes.Add(templateCppType);
break;
case CXTemplateArgumentKind.CXTemplateArgumentKind_Null:
case CXTemplateArgumentKind.CXTemplateArgumentKind_Declaration:
case CXTemplateArgumentKind.CXTemplateArgumentKind_NullPtr:
case CXTemplateArgumentKind.CXTemplateArgumentKind_Integral:
case CXTemplateArgumentKind.CXTemplateArgumentKind_Template:
case CXTemplateArgumentKind.CXTemplateArgumentKind_TemplateExpansion:
case CXTemplateArgumentKind.CXTemplateArgumentKind_Expression:
case CXTemplateArgumentKind.CXTemplateArgumentKind_Pack:
case CXTemplateArgumentKind.CXTemplateArgumentKind_Invalid:
break;
default:
throw new ArgumentOutOfRangeException();
}
}

return templateCppTypes;
Expand Down
2 changes: 1 addition & 1 deletion src/CppAst/CppParserOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public virtual CppParserOptions Clone()
/// Configure this instance with Windows and MSVC.
/// </summary>
/// <returns>This instance</returns>
public CppParserOptions ConfigureForWindowsMsvc(CppTargetCpu targetCpu = CppTargetCpu.X86, CppVisualStudioVersion vsVersion = CppVisualStudioVersion.VS2019)
public CppParserOptions ConfigureForWindowsMsvc(CppTargetCpu targetCpu = CppTargetCpu.X86, CppVisualStudioVersion vsVersion = CppVisualStudioVersion.VS2022)
{
// 1920
var highVersion = ((int)vsVersion) / 100; // => 19
Expand Down
2 changes: 1 addition & 1 deletion src/CppAst/CppVisualStudioVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ public enum CppVisualStudioVersion
///// <summary>
///// Visual Studio 2022 RTW (17.0)
///// </summary>
//VS2022 = 1930
VS2022 = 1930
}
}

0 comments on commit 24ccb48

Please sign in to comment.