Skip to content

Fix template-argument accessors for function and variable template specializations#721

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-fix-template-argument-accessors
Jul 13, 2026
Merged

Fix template-argument accessors for function and variable template specializations#721
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-fix-template-argument-accessors

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Fixes two correctness bugs in the native libClangSharp shim, both in the template-argument accessors and both the same class of bug.


clangsharp_Cursor_getNumTemplateArguments returned TAL && TAL->size() for a FunctionDecl template specialization -- i.e. a bool (0/1), not the count. So a specialization with more than one template argument wrongly reported 1. Every sibling in the function returns the real .size(); this now does too via a guard-then-size (TAL ? TAL->size() : 0), matching the guard in the sibling getTemplateArgument.

clangsharp_Cursor_getTemplateArgument only handled the derived VarTemplatePartialSpecializationDecl, where-as the sibling getNumTemplateArguments handles the base VarTemplateSpecializationDecl. For a full (non-partial) variable template specialization, getNum returned N but getTemplateArgument(i) returned a null CX_TemplateArgument for every i. Handling the base subsumes the partial case (partial derives from it), so partial behavior is unchanged and full specializations now return real arguments.


Adds regression tests in ClangSharp.UnitTests (FunctionTemplateSpecializationArgsTest, VarTemplateSpecializationArgsTest) covering a function-template specialization with >1 argument and a full variable-template specialization. Confirmed both fail against the current prebuilt native package exactly as the bugs predict (count 1 vs 2; args IsNull for the var spec) and are structurally correct.

The tests are gated behind a runtime libClang-version check that Assert.Ignores while the pinned 21.1 prebuilt native package is in use, since that package predates this native fix. Rebuilding the native lib off 21.1 auto-unskips them.

…ecializations

clangsharp_Cursor_getNumTemplateArguments returned a bool (TAL && TAL->size()) rather than the actual count for a FunctionDecl template specialization, so any specialization with more than one argument wrongly reported 1. Return the real size, matching the sibling branches.

clangsharp_Cursor_getTemplateArgument only handled the derived VarTemplatePartialSpecializationDecl, whereas getNumTemplateArguments handles the base VarTemplateSpecializationDecl. A full (non-partial) variable template specialization therefore reported N arguments but returned a null CX_TemplateArgument for each. Handle the base class, which subsumes the partial case.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding
tannergooding merged commit 2d7abf9 into dotnet:main Jul 13, 2026
14 checks passed
@tannergooding
tannergooding deleted the tannergooding-fix-template-argument-accessors branch July 13, 2026 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant