Skip to content

Commit

Permalink
Fix native crashes / asserts in libClangSharp and CXXRecordDecl Destr…
Browse files Browse the repository at this point in the history
…uctor property.

* Work around a clang crash in clangsharp_Cursor_getLambdaStaticInvoker when
  CRD->getLambdaCallOperator() returns null and
  CRD->getLambdaStaticInvoker() is called.

* clangsharp_Cursor_getNumAttrs: Calling D->getAttrs() will assert if
  D->hasAttrs() returns false. Fix by checking D->hasAttrs() first.

* A CXXRecordDecl may return null for getDestructor. Change the C# CXXRecordDecl
  Destructor property to be nullable to support this case.
  • Loading branch information
ceresgalax committed Feb 2, 2024
1 parent 356fea3 commit df0ab63
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 6 additions & 3 deletions sources/ClangSharp/Cursors/Decls/CXXRecordDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CXXRecordDecl : RecordDecl
private readonly Lazy<IReadOnlyList<CXXConstructorDecl>> _ctors;
private readonly Lazy<FunctionTemplateDecl> _dependentLambdaCallOperator;
private readonly Lazy<ClassTemplateDecl> _describedClassTemplate;
private readonly Lazy<CXXDestructorDecl> _destructor;
private readonly Lazy<CXXDestructorDecl?> _destructor;
private readonly Lazy<IReadOnlyList<FriendDecl>> _friends;
private readonly Lazy<CXXRecordDecl> _instantiatedFromMemberClass;
private readonly Lazy<CXXMethodDecl> _lambdaCallOperator;
Expand Down Expand Up @@ -63,7 +63,10 @@ private protected CXXRecordDecl(CXCursor handle, CXCursorKind expectedCursorKind

_dependentLambdaCallOperator = new Lazy<FunctionTemplateDecl>(() => TranslationUnit.GetOrCreate<FunctionTemplateDecl>(Handle.DependentLambdaCallOperator));
_describedClassTemplate = new Lazy<ClassTemplateDecl>(() => TranslationUnit.GetOrCreate<ClassTemplateDecl>(Handle.DescribedCursorTemplate));
_destructor = new Lazy<CXXDestructorDecl>(() => TranslationUnit.GetOrCreate<CXXDestructorDecl>(Handle.Destructor));
_destructor = new Lazy<CXXDestructorDecl?>(() => {
CXCursor destructor = Handle.Destructor;
return destructor.IsNull ? null : TranslationUnit.GetOrCreate<CXXDestructorDecl>(Handle.Destructor);
});

_friends = new Lazy<IReadOnlyList<FriendDecl>>(() => {
var numFriends = Handle.NumFriends;
Expand Down Expand Up @@ -126,7 +129,7 @@ private protected CXXRecordDecl(CXCursor handle, CXCursorKind expectedCursorKind

public ClassTemplateDecl DescribedClassTemplate => _describedClassTemplate.Value;

public CXXDestructorDecl Destructor => _destructor.Value;
public CXXDestructorDecl? Destructor => _destructor.Value;

public IReadOnlyList<FriendDecl> Friends => _friends.Value;

Expand Down
8 changes: 8 additions & 0 deletions sources/libClangSharp/ClangSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2813,6 +2813,11 @@ CXCursor clangsharp_Cursor_getLambdaStaticInvoker(CXCursor C) {
const Decl* D = getCursorDecl(C);

if (const CXXRecordDecl* CRD = dyn_cast<CXXRecordDecl>(D)) {
CXXMethodDecl *CallOp = CRD->getLambdaCallOperator();
// Work around a Clang bug: CRD->getLambdaStaticInvoker will crash if getLambdaCallOperator returns null.
if (CallOp == nullptr) {
return clang_getNullCursor();
}
return MakeCXCursor(CRD->getLambdaStaticInvoker(), getCursorTU(C));
}
}
Expand Down Expand Up @@ -3088,6 +3093,9 @@ int clangsharp_Cursor_getNumAssociatedConstraints(CXCursor C) {
int clangsharp_Cursor_getNumAttrs(CXCursor C) {
if (isDeclOrTU(C.kind)) {
const Decl* D = getCursorDecl(C);
if (!D->hasAttrs()) {
return 0;
}
return D->getAttrs().size();
}

Expand Down

0 comments on commit df0ab63

Please sign in to comment.