Skip to content
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

Fix crash when accessing property TemplateTypeParmDecl.DefaultArgument #528

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions sources/ClangSharp/Cursors/Decls/TemplateTypeParmDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
using ClangSharp.Interop;
using static ClangSharp.Interop.CXCursorKind;
using static ClangSharp.Interop.CX_DeclKind;
using static ClangSharp.Interop.CXTypeKind;

namespace ClangSharp;

public sealed class TemplateTypeParmDecl : TypeDecl
{
private readonly Lazy<IReadOnlyList<Expr>> _associatedConstraints;
private readonly Lazy<Type> _defaultArgument;
private readonly Lazy<Type?> _defaultArgument;

internal TemplateTypeParmDecl(CXCursor handle) : base(handle, CXCursor_TemplateTypeParameter, CX_DeclKind_TemplateTypeParm)
{
Expand All @@ -27,12 +28,15 @@ internal TemplateTypeParmDecl(CXCursor handle) : base(handle, CXCursor_TemplateT

return associatedConstraints;
});
_defaultArgument = new Lazy<Type>(() => TranslationUnit.GetOrCreate<Type>(Handle.DefaultArgType));
_defaultArgument = new Lazy<Type?>(() => {
CXType defaultArgType = Handle.DefaultArgType;
return defaultArgType.kind == CXType_Invalid ? null : TranslationUnit.GetOrCreate<Type>(defaultArgType);
});
}

public IReadOnlyList<Expr> AssociatedConstraints => _associatedConstraints.Value;

public Type DefaultArgument => _defaultArgument.Value;
public Type? DefaultArgument => _defaultArgument.Value;

public bool DefaultArgumentWasInherited => Handle.HasInheritedDefaultArg;

Expand Down
8 changes: 6 additions & 2 deletions sources/libClangSharp/ClangSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,15 +1001,19 @@ CXCursor clangsharp_Cursor_getDefaultArg(CXCursor C) {
}

CXType clangsharp_Cursor_getDefaultArgType(CXCursor C) {
QualType QT;

if (isDeclOrTU(C.kind)) {
const Decl* D = getCursorDecl(C);

if (const TemplateTypeParmDecl* TTPD = dyn_cast<TemplateTypeParmDecl>(D)) {
return MakeCXType(TTPD->getDefaultArgument(), getCursorTU(C));
if (TTPD->hasDefaultArgument()) {
QT = TTPD->getDefaultArgument();
}
}
}

return MakeCXType(QualType(), getCursorTU(C));
return MakeCXType(QT, getCursorTU(C));
}

CXCursor clangsharp_Cursor_getDefinition(CXCursor C) {
Expand Down
Loading