Skip to content

Commit b9479d2

Browse files
Merge pull request #525 from ceresgalax/bugfixes
Fix native crashes / asserts in libClangSharp and CXXRecordDecl Destructor property.
2 parents f996709 + d8ab030 commit b9479d2

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl)
717717
{
718718
outputBuilder.Write("Base");
719719
}
720-
720+
721721
outputBuilder.Write('.');
722722
outputBuilder.Write(name);
723723
outputBuilder.Write('(');
@@ -1817,7 +1817,11 @@ private void VisitRecordDecl(RecordDecl recordDecl)
18171817
{
18181818
var cxxDestructorDecl = cxxRecordDecl.Destructor;
18191819

1820-
if (!cxxDestructorDecl.IsVirtual && !IsExcluded(cxxDestructorDecl))
1820+
if (cxxDestructorDecl == null)
1821+
{
1822+
AddDiagnostic(DiagnosticLevel.Warning, "Record has user declared destructor, but Destructor property was null. Generated bindings may be incomplete.", cxxRecordDecl);
1823+
}
1824+
else if (!cxxDestructorDecl.IsVirtual && !IsExcluded(cxxDestructorDecl))
18211825
{
18221826
Visit(cxxDestructorDecl);
18231827
_outputBuilder.WriteDivider();

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)