Skip to content

Commit

Permalink
Fix incorrect template argument kind
Browse files Browse the repository at this point in the history
  • Loading branch information
MrJul committed Nov 11, 2024
1 parent 7e211d6 commit 0b62dec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
30 changes: 29 additions & 1 deletion sources/libClangSharp/ClangSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,38 @@ using namespace clang::cxstring;
using namespace clang::cxtu;
using namespace clang::cxtype;

CXTemplateArgumentKind ConvertTemplateArgumentKind(TemplateArgument::ArgKind kind) {
switch (kind) {
case TemplateArgument::Null:
return CXTemplateArgumentKind_Null;
case TemplateArgument::Type:
return CXTemplateArgumentKind_Type;
case TemplateArgument::Declaration:
return CXTemplateArgumentKind_Declaration;
case TemplateArgument::NullPtr:
return CXTemplateArgumentKind_NullPtr;
case TemplateArgument::Integral:
return CXTemplateArgumentKind_Integral;
case TemplateArgument::StructuralValue:
// Does not exist in CXTemplateArgumentKind
return CXTemplateArgumentKind_Invalid;
case TemplateArgument::Template:
return CXTemplateArgumentKind_Template;
case TemplateArgument::TemplateExpansion:
return CXTemplateArgumentKind_TemplateExpansion;
case TemplateArgument::Expression:
return CXTemplateArgumentKind_Expression;
case TemplateArgument::Pack:
return CXTemplateArgumentKind_Pack;
default:
return CXTemplateArgumentKind_Invalid;
}
}

CX_TemplateArgument MakeCXTemplateArgument(const TemplateArgument* TA, CXTranslationUnit TU, bool needsDispose = false) {
if (TA) {
assert(TU && "Invalid arguments!");
return { static_cast<CXTemplateArgumentKind>(TA->getKind()), (needsDispose ? 1 : 0), TA, TU };
return { ConvertTemplateArgumentKind(TA->getKind()), (needsDispose ? 1 : 0), TA, TU };
}

return { };
Expand Down
22 changes: 22 additions & 0 deletions tests/ClangSharp.UnitTests/CursorTests/DeclTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,26 @@ class MyClass<int, U>
var templateParameter = classTemplatePartialSpecializationDecl.TemplateParameters.Single();
Assert.That(templateParameter.Name, Is.EqualTo("U"));
}

[Test]
public void TemplateParameterPackTest()
{
var inputContents = $@"template<class... Types>
class tuple;
tuple<int, long> SomeFunction();
";

using var translationUnit = CreateTranslationUnit(inputContents);

var functionDecl = translationUnit.TranslationUnitDecl.Decls.OfType<FunctionDecl>().Single();
var tupleDecl = functionDecl.ReturnType.AsCXXRecordDecl as ClassTemplateSpecializationDecl;
Assert.That(tupleDecl, Is.Not.Null);
Assert.That(tupleDecl!.TemplateArgs.Count, Is.EqualTo(1));

var packElements = tupleDecl.TemplateArgs[0].PackElements;
Assert.That(packElements.Count, Is.EqualTo(2));
Assert.That(packElements[0].AsType.AsString, Is.EqualTo("int"));
Assert.That(packElements[1].AsType.AsString, Is.EqualTo("long"));
}
}

0 comments on commit 0b62dec

Please sign in to comment.