Skip to content

Fix native crashes / asserts in libClangSharp and CXXRecordDecl Destructor property. #525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ private void VisitFunctionDecl(FunctionDecl functionDecl)
{
outputBuilder.Write("Base");
}

outputBuilder.Write('.');
outputBuilder.Write(name);
outputBuilder.Write('(');
Expand Down Expand Up @@ -1817,7 +1817,11 @@ private void VisitRecordDecl(RecordDecl recordDecl)
{
var cxxDestructorDecl = cxxRecordDecl.Destructor;

if (!cxxDestructorDecl.IsVirtual && !IsExcluded(cxxDestructorDecl))
if (cxxDestructorDecl == null)
{
AddDiagnostic(DiagnosticLevel.Warning, "Record has user declared destructor, but Destructor property was null. Generated bindings may be incomplete.", cxxRecordDecl);
}
else if (!cxxDestructorDecl.IsVirtual && !IsExcluded(cxxDestructorDecl))
{
Visit(cxxDestructorDecl);
_outputBuilder.WriteDivider();
Expand Down
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