Skip to content

Commit 0296dbf

Browse files
Merge pull request #528 from ceresgalax/bug_ttpd_da
Fix crash when accessing property TemplateTypeParmDecl.DefaultArgument
2 parents af2a428 + 716ae31 commit 0296dbf

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
@@ -1037,15 +1037,19 @@ CXCursor clangsharp_Cursor_getDefaultArg(CXCursor C) {
10371037
}
10381038

10391039
CXType clangsharp_Cursor_getDefaultArgType(CXCursor C) {
1040+
QualType QT;
1041+
10401042
if (isDeclOrTU(C.kind)) {
10411043
const Decl* D = getCursorDecl(C);
10421044

10431045
if (const TemplateTypeParmDecl* TTPD = dyn_cast<TemplateTypeParmDecl>(D)) {
1044-
return MakeCXType(TTPD->getDefaultArgument(), getCursorTU(C));
1046+
if (TTPD->hasDefaultArgument()) {
1047+
QT = TTPD->getDefaultArgument();
1048+
}
10451049
}
10461050
}
10471051

1048-
return MakeCXType(QualType(), getCursorTU(C));
1052+
return MakeCXType(QT, getCursorTU(C));
10491053
}
10501054

10511055
CXCursor clangsharp_Cursor_getDefinition(CXCursor C) {

0 commit comments

Comments
 (0)