Skip to content

Commit 6e7a238

Browse files
committed
Generate valid C# when std::string is only used for variables
Fixes #1209. Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 1d28807 commit 6e7a238

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

src/Generator/Passes/TrimSpecializationsPass.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public TrimSpecializationsPass()
2121
VisitOptions.VisitNamespaceEvents = false;
2222
VisitOptions.VisitNamespaceTemplates = false;
2323
VisitOptions.VisitNamespaceTypedefs = false;
24-
VisitOptions.VisitNamespaceVariables = false;
2524
VisitOptions.VisitTemplateArguments = false;
2625
}
2726

@@ -92,6 +91,21 @@ public override bool VisitFieldDecl(Field field)
9291
return true;
9392
}
9493

94+
public override bool VisitVariableDecl(Variable variable)
95+
{
96+
if (!base.VisitVariableDecl(variable))
97+
return false;
98+
99+
if (variable.Access == AccessSpecifier.Public)
100+
{
101+
ASTUtils.CheckTypeForSpecialization(variable.Type,
102+
variable, AddSpecialization, Context.TypeMaps);
103+
return true;
104+
}
105+
106+
return true;
107+
}
108+
95109
private void AddSpecialization(ClassTemplateSpecialization specialization)
96110
{
97111
if (specializations.Contains(specialization))

tests/Encodings/Encodings.Tests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ public class EncodingsTests : GeneratorTestFixture
77
[Test]
88
public void TestFoo()
99
{
10-
var foo = new Foo();
1110
const string georgia = "საქართველო";
12-
foo.Unicode = georgia;
13-
Assert.That(foo.Unicode, Is.EqualTo(georgia));
11+
Foo.Unicode = georgia;
12+
Assert.That(Foo.Unicode, Is.EqualTo(georgia));
1413

1514
// TODO: move this, it has nothing to do with Unicode, it's here only not to break the CLI branch
16-
Assert.That(foo[0], Is.EqualTo(5));
15+
using (var foo = new Foo())
16+
{
17+
Assert.That(foo[0], Is.EqualTo(5));
18+
}
1719
}
1820
}

tests/Encodings/Encodings.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
#include "Encodings.h"
22

3+
std::string Foo::Unicode = "";
4+
5+
Foo::Foo()
6+
{
7+
}
8+
9+
Foo::~Foo()
10+
{
11+
}
12+
313
// TODO: move this, it has nothing to do with Unicode, it's here only not to break the CLI branch
414
int Foo::operator[](int i) const
515
{
@@ -9,4 +19,4 @@ int Foo::operator[](int i) const
919
int Foo::operator[](int i)
1020
{
1121
return 5;
12-
}
22+
}

tests/Encodings/Encodings.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#include "../Tests.h"
2+
#include <string>
23

34
class DLL_API Foo
45
{
56
public:
6-
const char* Unicode;
7+
Foo();
8+
~Foo();
9+
10+
static std::string Unicode;
711

812
// TODO: VC++ does not support char16
913
// char16 chr16;

0 commit comments

Comments
 (0)