From 0b62dece8446a33fe9cec6c6bc594277abfee440 Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Mon, 11 Nov 2024 11:35:52 +0100 Subject: [PATCH 1/2] Fix incorrect template argument kind --- sources/libClangSharp/ClangSharp.cpp | 30 ++++++++++++++++++- .../CursorTests/DeclTest.cs | 22 ++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/sources/libClangSharp/ClangSharp.cpp b/sources/libClangSharp/ClangSharp.cpp index 5880900c..8e5078ed 100644 --- a/sources/libClangSharp/ClangSharp.cpp +++ b/sources/libClangSharp/ClangSharp.cpp @@ -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(TA->getKind()), (needsDispose ? 1 : 0), TA, TU }; + return { ConvertTemplateArgumentKind(TA->getKind()), (needsDispose ? 1 : 0), TA, TU }; } return { }; diff --git a/tests/ClangSharp.UnitTests/CursorTests/DeclTest.cs b/tests/ClangSharp.UnitTests/CursorTests/DeclTest.cs index 067280f1..a8a49d9a 100644 --- a/tests/ClangSharp.UnitTests/CursorTests/DeclTest.cs +++ b/tests/ClangSharp.UnitTests/CursorTests/DeclTest.cs @@ -74,4 +74,26 @@ class MyClass var templateParameter = classTemplatePartialSpecializationDecl.TemplateParameters.Single(); Assert.That(templateParameter.Name, Is.EqualTo("U")); } + + [Test] + public void TemplateParameterPackTest() + { + var inputContents = $@"template +class tuple; + +tuple SomeFunction(); +"; + + using var translationUnit = CreateTranslationUnit(inputContents); + + var functionDecl = translationUnit.TranslationUnitDecl.Decls.OfType().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")); + } } From 74b88738093a82b94da92fd7ed873b744944c5cd Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Wed, 20 Nov 2024 16:10:43 +0100 Subject: [PATCH 2/2] Disable TemplateParameterPackTest for now --- tests/ClangSharp.UnitTests/CursorTests/DeclTest.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ClangSharp.UnitTests/CursorTests/DeclTest.cs b/tests/ClangSharp.UnitTests/CursorTests/DeclTest.cs index a8a49d9a..e216b73b 100644 --- a/tests/ClangSharp.UnitTests/CursorTests/DeclTest.cs +++ b/tests/ClangSharp.UnitTests/CursorTests/DeclTest.cs @@ -76,6 +76,7 @@ class MyClass } [Test] + [Ignore("TODO: LibClangSharp needs to be recompiled first")] public void TemplateParameterPackTest() { var inputContents = $@"template