From 7a0bac9dfc8b5df249c18c74c40821ac9eadcf5d Mon Sep 17 00:00:00 2001 From: ceresgalax Date: Sun, 11 Feb 2024 15:47:51 -0800 Subject: [PATCH] Make Decl.DescribedTemplate property nullable. Obviously, many Decl cursors will not describe a template. Make this property null to reflect that an avoid an exception by TranslationUnit.GetOrCreate(). Additionally, DescribedTemplate will be null for partial template specializations. Added a doc comment to the property as this is less obvious. --- sources/ClangSharp/Cursors/Decls/Decl.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/sources/ClangSharp/Cursors/Decls/Decl.cs b/sources/ClangSharp/Cursors/Decls/Decl.cs index e509e4a5..7274bae8 100644 --- a/sources/ClangSharp/Cursors/Decls/Decl.cs +++ b/sources/ClangSharp/Cursors/Decls/Decl.cs @@ -15,7 +15,7 @@ public class Decl : Cursor private readonly Lazy _body; private readonly Lazy _canonicalDecl; private readonly Lazy> _decls; - private readonly Lazy _describedTemplate; + private readonly Lazy _describedTemplate; private readonly Lazy _mostRecentDecl; private readonly Lazy _nextDeclInContext; private readonly Lazy _nonClosureContext; @@ -62,7 +62,11 @@ private protected Decl(CXCursor handle, CXCursorKind expectedCursorKind, CX_Decl return decls; }); ; - _describedTemplate = new Lazy(() => TranslationUnit.GetOrCreate(Handle.DescribedTemplate)); + _describedTemplate = new Lazy(() => + { + CXCursor describedTemplate = Handle.DescribedTemplate; + return describedTemplate.IsNull ? null : TranslationUnit.GetOrCreate(describedTemplate); + }); _mostRecentDecl = new Lazy(() => TranslationUnit.GetOrCreate(Handle.MostRecentDecl)); _nextDeclInContext = new Lazy(() => TranslationUnit.GetOrCreate(Handle.NextDeclInContext)); _nonClosureContext = new Lazy(() => TranslationUnit.GetOrCreate(Handle.NonClosureContext)); @@ -90,7 +94,11 @@ private protected Decl(CXCursor handle, CXCursorKind expectedCursorKind, CX_Decl public IReadOnlyList Decls => _decls.Value; - public TemplateDecl DescribedTemplate => _describedTemplate.Value; + /// + /// Per clang documentation: This returns null for partial specializations, because they are not modeled as TemplateDecls. + /// Use DescribedTemplateParams to handle those cases. + /// + public TemplateDecl? DescribedTemplate => _describedTemplate.Value; public bool HasAttrs => Handle.HasAttrs;