Skip to content

Commit 716ae31

Browse files
committed
Fix crash when accessing property TemplateTypeParmDecl.DefaultArgument
* hasDefaultArgType() must be called first. * Since the type may not have a default arg type, make the property nullable.
1 parent b9479d2 commit 716ae31

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

sources/ClangSharp/Cursors/Decls/TemplateTypeParmDecl.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
using ClangSharp.Interop;
66
using static ClangSharp.Interop.CXCursorKind;
77
using static ClangSharp.Interop.CX_DeclKind;
8+
using static ClangSharp.Interop.CXTypeKind;
89

910
namespace ClangSharp;
1011

1112
public sealed class TemplateTypeParmDecl : TypeDecl
1213
{
1314
private readonly Lazy<IReadOnlyList<Expr>> _associatedConstraints;
14-
private readonly Lazy<Type> _defaultArgument;
15+
private readonly Lazy<Type?> _defaultArgument;
1516

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

2829
return associatedConstraints;
2930
});
30-
_defaultArgument = new Lazy<Type>(() => TranslationUnit.GetOrCreate<Type>(Handle.DefaultArgType));
31+
_defaultArgument = new Lazy<Type?>(() => {
32+
CXType defaultArgType = Handle.DefaultArgType;
33+
return defaultArgType.kind == CXType_Invalid ? null : TranslationUnit.GetOrCreate<Type>(defaultArgType);
34+
});
3135
}
3236

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

35-
public Type DefaultArgument => _defaultArgument.Value;
39+
public Type? DefaultArgument => _defaultArgument.Value;
3640

3741
public bool DefaultArgumentWasInherited => Handle.HasInheritedDefaultArg;
3842

sources/libClangSharp/ClangSharp.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,15 +1001,19 @@ CXCursor clangsharp_Cursor_getDefaultArg(CXCursor C) {
10011001
}
10021002

10031003
CXType clangsharp_Cursor_getDefaultArgType(CXCursor C) {
1004+
QualType QT;
1005+
10041006
if (isDeclOrTU(C.kind)) {
10051007
const Decl* D = getCursorDecl(C);
10061008

10071009
if (const TemplateTypeParmDecl* TTPD = dyn_cast<TemplateTypeParmDecl>(D)) {
1008-
return MakeCXType(TTPD->getDefaultArgument(), getCursorTU(C));
1010+
if (TTPD->hasDefaultArgument()) {
1011+
QT = TTPD->getDefaultArgument();
1012+
}
10091013
}
10101014
}
10111015

1012-
return MakeCXType(QualType(), getCursorTU(C));
1016+
return MakeCXType(QT, getCursorTU(C));
10131017
}
10141018

10151019
CXCursor clangsharp_Cursor_getDefinition(CXCursor C) {

0 commit comments

Comments
 (0)