Skip to content

Commit

Permalink
Merge pull request #525 from ceresgalax/bugfixes
Browse files Browse the repository at this point in the history
Fix native crashes / asserts in libClangSharp and CXXRecordDecl Destructor property.
  • Loading branch information
tannergooding authored Feb 5, 2024
2 parents f996709 + d8ab030 commit b9479d2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
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

0 comments on commit b9479d2

Please sign in to comment.