Skip to content

Commit df0ab63

Browse files
committed
Fix native crashes / asserts in libClangSharp and CXXRecordDecl Destructor 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.
1 parent 356fea3 commit df0ab63

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

sources/ClangSharp/Cursors/Decls/CXXRecordDecl.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class CXXRecordDecl : RecordDecl
1414
private readonly Lazy<IReadOnlyList<CXXConstructorDecl>> _ctors;
1515
private readonly Lazy<FunctionTemplateDecl> _dependentLambdaCallOperator;
1616
private readonly Lazy<ClassTemplateDecl> _describedClassTemplate;
17-
private readonly Lazy<CXXDestructorDecl> _destructor;
17+
private readonly Lazy<CXXDestructorDecl?> _destructor;
1818
private readonly Lazy<IReadOnlyList<FriendDecl>> _friends;
1919
private readonly Lazy<CXXRecordDecl> _instantiatedFromMemberClass;
2020
private readonly Lazy<CXXMethodDecl> _lambdaCallOperator;
@@ -63,7 +63,10 @@ private protected CXXRecordDecl(CXCursor handle, CXCursorKind expectedCursorKind
6363

6464
_dependentLambdaCallOperator = new Lazy<FunctionTemplateDecl>(() => TranslationUnit.GetOrCreate<FunctionTemplateDecl>(Handle.DependentLambdaCallOperator));
6565
_describedClassTemplate = new Lazy<ClassTemplateDecl>(() => TranslationUnit.GetOrCreate<ClassTemplateDecl>(Handle.DescribedCursorTemplate));
66-
_destructor = new Lazy<CXXDestructorDecl>(() => TranslationUnit.GetOrCreate<CXXDestructorDecl>(Handle.Destructor));
66+
_destructor = new Lazy<CXXDestructorDecl?>(() => {
67+
CXCursor destructor = Handle.Destructor;
68+
return destructor.IsNull ? null : TranslationUnit.GetOrCreate<CXXDestructorDecl>(Handle.Destructor);
69+
});
6770

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

127130
public ClassTemplateDecl DescribedClassTemplate => _describedClassTemplate.Value;
128131

129-
public CXXDestructorDecl Destructor => _destructor.Value;
132+
public CXXDestructorDecl? Destructor => _destructor.Value;
130133

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

sources/libClangSharp/ClangSharp.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,6 +2813,11 @@ CXCursor clangsharp_Cursor_getLambdaStaticInvoker(CXCursor C) {
28132813
const Decl* D = getCursorDecl(C);
28142814

28152815
if (const CXXRecordDecl* CRD = dyn_cast<CXXRecordDecl>(D)) {
2816+
CXXMethodDecl *CallOp = CRD->getLambdaCallOperator();
2817+
// Work around a Clang bug: CRD->getLambdaStaticInvoker will crash if getLambdaCallOperator returns null.
2818+
if (CallOp == nullptr) {
2819+
return clang_getNullCursor();
2820+
}
28162821
return MakeCXCursor(CRD->getLambdaStaticInvoker(), getCursorTU(C));
28172822
}
28182823
}
@@ -3088,6 +3093,9 @@ int clangsharp_Cursor_getNumAssociatedConstraints(CXCursor C) {
30883093
int clangsharp_Cursor_getNumAttrs(CXCursor C) {
30893094
if (isDeclOrTU(C.kind)) {
30903095
const Decl* D = getCursorDecl(C);
3096+
if (!D->hasAttrs()) {
3097+
return 0;
3098+
}
30913099
return D->getAttrs().size();
30923100
}
30933101

0 commit comments

Comments
 (0)