From 422b98a0fd85a60544ed8c382d98123ec217ddb8 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Wed, 14 Jun 2017 20:32:13 +0300 Subject: [PATCH] Enabled the tests for std::string. Signed-off-by: Dimitar Dobrev --- .../CSharp/i686-apple-darwin12.4.0/Std-symbols.cpp | 8 ++++---- .../Bindings/CSharp/i686-apple-darwin12.4.0/Std.cs | 9 +++++++++ .../CSharp/x86_64-apple-darwin12.4.0/Std-symbols.cpp | 8 ++++---- .../Bindings/CSharp/x86_64-apple-darwin12.4.0/Std.cs | 9 +++++++++ src/Generator/AST/Utils.cs | 8 ++++++-- src/Generator/Passes/SymbolsCodeGenerator.cs | 7 ++++++- src/Generator/Passes/TrimSpecializationsPass.cs | 4 ++-- src/Parser/TargetTriple.cs | 7 +++++++ tests/CSharp/AnotherUnit.cpp | 2 +- tests/CSharp/AnotherUnit.h | 4 ++-- tests/CSharp/CSharp.Tests.cs | 6 +++--- tests/Common/Common.Tests.cs | 2 +- tests/Common/Common.cpp | 10 +++++++++- tests/Common/Common.h | 4 +++- 14 files changed, 66 insertions(+), 22 deletions(-) diff --git a/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/Std-symbols.cpp b/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/Std-symbols.cpp index 4f206d7e..d25fdec2 100644 --- a/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/Std-symbols.cpp +++ b/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/Std-symbols.cpp @@ -1,6 +1,6 @@ #include -template std::__1::allocator::allocator() noexcept; -template std::__1::basic_string, std::__1::allocator>::basic_string(const std::__1::basic_string, std::__1::allocator>::value_type*, const std::__1::basic_string, std::__1::allocator>::allocator_type&); -template std::__1::basic_string, std::__1::allocator>::~basic_string(); -template const std::__1::basic_string, std::__1::allocator>::value_type* std::__1::basic_string, std::__1::allocator>::c_str() const noexcept; +template __attribute__((visibility("default"))) std::__1::allocator::allocator() noexcept; +template __attribute__((visibility("default"))) std::__1::basic_string, std::__1::allocator>::basic_string(const std::__1::basic_string, std::__1::allocator>::value_type*, const std::__1::basic_string, std::__1::allocator>::allocator_type&); +template __attribute__((visibility("default"))) std::__1::basic_string, std::__1::allocator>::~basic_string(); +template __attribute__((visibility("default"))) const std::__1::basic_string, std::__1::allocator>::value_type* std::__1::basic_string, std::__1::allocator>::c_str() const noexcept; diff --git a/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/Std.cs b/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/Std.cs index ef75c893..7a26dc2d 100644 --- a/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/Std.cs +++ b/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/Std.cs @@ -1235,6 +1235,15 @@ namespace Std [FieldOffset(0)] internal global::Std.__1.Tree.__Internal __tree_; } + + + public unsafe partial class ValueCompare + { + [StructLayout(LayoutKind.Explicit, Size = 0)] + public partial struct __Internal + { + } + } } } } diff --git a/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/Std-symbols.cpp b/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/Std-symbols.cpp index 4f206d7e..d25fdec2 100644 --- a/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/Std-symbols.cpp +++ b/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/Std-symbols.cpp @@ -1,6 +1,6 @@ #include -template std::__1::allocator::allocator() noexcept; -template std::__1::basic_string, std::__1::allocator>::basic_string(const std::__1::basic_string, std::__1::allocator>::value_type*, const std::__1::basic_string, std::__1::allocator>::allocator_type&); -template std::__1::basic_string, std::__1::allocator>::~basic_string(); -template const std::__1::basic_string, std::__1::allocator>::value_type* std::__1::basic_string, std::__1::allocator>::c_str() const noexcept; +template __attribute__((visibility("default"))) std::__1::allocator::allocator() noexcept; +template __attribute__((visibility("default"))) std::__1::basic_string, std::__1::allocator>::basic_string(const std::__1::basic_string, std::__1::allocator>::value_type*, const std::__1::basic_string, std::__1::allocator>::allocator_type&); +template __attribute__((visibility("default"))) std::__1::basic_string, std::__1::allocator>::~basic_string(); +template __attribute__((visibility("default"))) const std::__1::basic_string, std::__1::allocator>::value_type* std::__1::basic_string, std::__1::allocator>::c_str() const noexcept; diff --git a/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/Std.cs b/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/Std.cs index 7076144c..d87a0b19 100644 --- a/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/Std.cs +++ b/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/Std.cs @@ -1235,6 +1235,15 @@ namespace Std [FieldOffset(0)] internal global::Std.__1.Tree.__Internal __tree_; } + + + public unsafe partial class ValueCompare + { + [StructLayout(LayoutKind.Explicit, Size = 0)] + public partial struct __Internal + { + } + } } } } diff --git a/src/Generator/AST/Utils.cs b/src/Generator/AST/Utils.cs index 8d1f8ccd..f1a42407 100644 --- a/src/Generator/AST/Utils.cs +++ b/src/Generator/AST/Utils.cs @@ -91,9 +91,13 @@ namespace CppSharp.AST type = type.Desugar(); type = type.GetFinalPointee() ?? type; ClassTemplateSpecialization specialization; - if (!type.TryGetDeclaration(out specialization) || - specialization.IsExplicitlyGenerated) + type.TryGetDeclaration(out specialization); + if (specialization == null || specialization.IsExplicitlyGenerated) + { + if (specialization != null) + addSpecialization(specialization); return; + } TypeMap typeMap; typeMaps.FindTypeMap(specialization, out typeMap); diff --git a/src/Generator/Passes/SymbolsCodeGenerator.cs b/src/Generator/Passes/SymbolsCodeGenerator.cs index 5d767cce..d56deb0a 100644 --- a/src/Generator/Passes/SymbolsCodeGenerator.cs +++ b/src/Generator/Passes/SymbolsCodeGenerator.cs @@ -4,6 +4,7 @@ using System.Text; using CppSharp.AST; using CppSharp.AST.Extensions; using CppSharp.Generators; +using CppSharp.Parser; namespace CppSharp.Passes { @@ -30,7 +31,11 @@ namespace CppSharp.Passes { if (method.Namespace is ClassTemplateSpecialization) { - var exporting = Context.ParserOptions.IsMicrosoftAbi ? "__declspec(dllexport) " : string.Empty; + var exporting = string.Empty; + if (Context.ParserOptions.IsMicrosoftAbi) + exporting = "__declspec(dllexport) "; + else if (TargetTriple.IsMacOS(Context.ParserOptions.TargetTriple)) + exporting = "__attribute__((visibility(\"default\"))) "; WriteLine($"template {exporting}{method.Visit(cppTypePrinter)};"); return true; } diff --git a/src/Generator/Passes/TrimSpecializationsPass.cs b/src/Generator/Passes/TrimSpecializationsPass.cs index 08f6d33f..353adcde 100644 --- a/src/Generator/Passes/TrimSpecializationsPass.cs +++ b/src/Generator/Passes/TrimSpecializationsPass.cs @@ -74,8 +74,8 @@ namespace CppSharp.Passes private void CleanSpecializations(Class template) { - template.Specializations.RemoveAll(s => !s.IsExplicitlyGenerated && - !specializations.Contains(s) && !internalSpecializations.Contains(s)); + template.Specializations.RemoveAll( + s => !specializations.Contains(s) && !internalSpecializations.Contains(s)); foreach (var specialization in template.Specializations.Where( s => !s.IsExplicitlyGenerated && diff --git a/src/Parser/TargetTriple.cs b/src/Parser/TargetTriple.cs index 3e467959..0e2f2b07 100644 --- a/src/Parser/TargetTriple.cs +++ b/src/Parser/TargetTriple.cs @@ -12,5 +12,12 @@ namespace CppSharp.Parser return parts.Contains("windows") || parts.Contains("win32") || parts.Contains("win64"); } + + public static bool IsMacOS(string targetTriple) + { + var parts = targetTriple.Split('-'); + return parts.Contains("apple") || + parts.Contains("darwin") || parts.Contains("osx"); + } } } diff --git a/tests/CSharp/AnotherUnit.cpp b/tests/CSharp/AnotherUnit.cpp index 46dfad93..71d621f0 100644 --- a/tests/CSharp/AnotherUnit.cpp +++ b/tests/CSharp/AnotherUnit.cpp @@ -7,5 +7,5 @@ void functionInAnotherUnit() namespace HasFreeConstant { extern const int DLL_API FREE_CONSTANT_IN_NAMESPACE = 5; -// extern const std::string DLL_API STD_STRING_CONSTANT = "test"; + extern const std::string DLL_API STD_STRING_CONSTANT = "test"; } diff --git a/tests/CSharp/AnotherUnit.h b/tests/CSharp/AnotherUnit.h index 62216fac..778294e4 100644 --- a/tests/CSharp/AnotherUnit.h +++ b/tests/CSharp/AnotherUnit.h @@ -1,5 +1,5 @@ #include "../Tests.h" -//#include +#include void DLL_API functionInAnotherUnit(); @@ -20,5 +20,5 @@ class ForwardInOtherUnitButSameModule namespace HasFreeConstant { extern const int DLL_API FREE_CONSTANT_IN_NAMESPACE; -// extern const std::string DLL_API STD_STRING_CONSTANT; + extern const std::string DLL_API STD_STRING_CONSTANT; } diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index bf41635a..53e289d7 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -543,12 +543,12 @@ public unsafe class CSharpTests : GeneratorTestFixture } } - [Test, Ignore("We need symbols for std::string to invoke and auto-compilation of exported templates is not added yet.")] + [Test, Platform(Exclude = "Linux", Reason = "This fails on Linux for no reason at all.")] public void TestStdStringConstant() { - //Assert.That(CSharp.HasFreeConstant.AnotherUnit.STD_STRING_CONSTANT, Is.EqualTo("test")); + Assert.That(CSharp.HasFreeConstant.AnotherUnit.STD_STRING_CONSTANT, Is.EqualTo("test")); // check a second time to ensure it hasn't been improperly freed - //Assert.That(CSharp.HasFreeConstant.AnotherUnit.STD_STRING_CONSTANT, Is.EqualTo("test")); + Assert.That(CSharp.HasFreeConstant.AnotherUnit.STD_STRING_CONSTANT, Is.EqualTo("test")); } [Test, Ignore("The completion of types is temporarily suspended because of problems with QtWidgets.")] diff --git a/tests/Common/Common.Tests.cs b/tests/Common/Common.Tests.cs index 257feab6..e67f1000 100644 --- a/tests/Common/Common.Tests.cs +++ b/tests/Common/Common.Tests.cs @@ -711,7 +711,7 @@ public class CommonTests : GeneratorTestFixture Assert.That(Foo.ReadWrite, Is.EqualTo(25)); } - [Test, Ignore("We need symbols for std::string to invoke and auto-compilation of exported templates is not added yet.")] + [Test] public void TestStdString() { // when C++ memory is deleted, it's only marked as free but not immediadely freed diff --git a/tests/Common/Common.cpp b/tests/Common/Common.cpp index 98b3b56b..b21d55df 100644 --- a/tests/Common/Common.cpp +++ b/tests/Common/Common.cpp @@ -435,7 +435,15 @@ void DelegateNamespace::f2(void (*)()) { } -std::string HasStdString::testStdString(std::string s) +HasStdString::HasStdString() +{ +} + +HasStdString::~HasStdString() +{ +} + +std::string HasStdString::testStdString(const std::string& s) { return s + "_test"; } diff --git a/tests/Common/Common.h b/tests/Common/Common.h index 3887f7b2..8c36d449 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -768,7 +768,9 @@ public: class DLL_API HasStdString { public: - std::string testStdString(std::string s); + HasStdString(); + ~HasStdString(); + std::string testStdString(const std::string& s); std::string s; std::string& getStdString(); };