Skip to content

Commit

Permalink
Make Decl.DescribedTemplate property nullable.
Browse files Browse the repository at this point in the history
Obviously, many Decl cursors will not describe a template. Make this property null to reflect that an avoid an exception by TranslationUnit.GetOrCreate<TemplateDecl>().

Additionally, DescribedTemplate will be null for partial template specializations. Added a doc comment to the property as this is less obvious.
  • Loading branch information
ceresgalax committed Feb 12, 2024
1 parent b9479d2 commit 7a0bac9
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions sources/ClangSharp/Cursors/Decls/Decl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Decl : Cursor
private readonly Lazy<Stmt?> _body;
private readonly Lazy<Decl> _canonicalDecl;
private readonly Lazy<IReadOnlyList<Decl>> _decls;
private readonly Lazy<TemplateDecl> _describedTemplate;
private readonly Lazy<TemplateDecl?> _describedTemplate;
private readonly Lazy<Decl> _mostRecentDecl;
private readonly Lazy<Decl> _nextDeclInContext;
private readonly Lazy<Decl> _nonClosureContext;
Expand Down Expand Up @@ -62,7 +62,11 @@ private protected Decl(CXCursor handle, CXCursorKind expectedCursorKind, CX_Decl
return decls;
});
;
_describedTemplate = new Lazy<TemplateDecl>(() => TranslationUnit.GetOrCreate<TemplateDecl>(Handle.DescribedTemplate));
_describedTemplate = new Lazy<TemplateDecl?>(() =>
{
CXCursor describedTemplate = Handle.DescribedTemplate;
return describedTemplate.IsNull ? null : TranslationUnit.GetOrCreate<TemplateDecl>(describedTemplate);
});
_mostRecentDecl = new Lazy<Decl>(() => TranslationUnit.GetOrCreate<Decl>(Handle.MostRecentDecl));
_nextDeclInContext = new Lazy<Decl>(() => TranslationUnit.GetOrCreate<Decl>(Handle.NextDeclInContext));
_nonClosureContext = new Lazy<Decl>(() => TranslationUnit.GetOrCreate<Decl>(Handle.NonClosureContext));
Expand Down Expand Up @@ -90,7 +94,11 @@ private protected Decl(CXCursor handle, CXCursorKind expectedCursorKind, CX_Decl

public IReadOnlyList<Decl> Decls => _decls.Value;

public TemplateDecl DescribedTemplate => _describedTemplate.Value;
/// <summary>
/// Per clang documentation: This returns null for partial specializations, because they are not modeled as TemplateDecls.
/// Use DescribedTemplateParams to handle those cases.
/// </summary>
public TemplateDecl? DescribedTemplate => _describedTemplate.Value;

public bool HasAttrs => Handle.HasAttrs;

Expand Down

0 comments on commit 7a0bac9

Please sign in to comment.