diff --git a/sources/ClangSharp/Cursors/Decls/TemplateTypeParmDecl.cs b/sources/ClangSharp/Cursors/Decls/TemplateTypeParmDecl.cs index f68532c1..ca0f4b1f 100644 --- a/sources/ClangSharp/Cursors/Decls/TemplateTypeParmDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/TemplateTypeParmDecl.cs @@ -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> _associatedConstraints; - private readonly Lazy _defaultArgument; + private readonly Lazy _defaultArgument; internal TemplateTypeParmDecl(CXCursor handle) : base(handle, CXCursor_TemplateTypeParameter, CX_DeclKind_TemplateTypeParm) { @@ -27,12 +28,15 @@ internal TemplateTypeParmDecl(CXCursor handle) : base(handle, CXCursor_TemplateT return associatedConstraints; }); - _defaultArgument = new Lazy(() => TranslationUnit.GetOrCreate(Handle.DefaultArgType)); + _defaultArgument = new Lazy(() => { + CXType defaultArgType = Handle.DefaultArgType; + return defaultArgType.kind == CXType_Invalid ? null : TranslationUnit.GetOrCreate(defaultArgType); + }); } public IReadOnlyList AssociatedConstraints => _associatedConstraints.Value; - public Type DefaultArgument => _defaultArgument.Value; + public Type? DefaultArgument => _defaultArgument.Value; public bool DefaultArgumentWasInherited => Handle.HasInheritedDefaultArg; diff --git a/sources/libClangSharp/ClangSharp.cpp b/sources/libClangSharp/ClangSharp.cpp index 02c76f2d..372b54d0 100644 --- a/sources/libClangSharp/ClangSharp.cpp +++ b/sources/libClangSharp/ClangSharp.cpp @@ -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(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) {