Skip to content

Commit

Permalink
Rename templates only used through aliases
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed Oct 29, 2021
1 parent 0919026 commit a2aeaed
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/Generator/Passes/StripUnusedSystemTypesPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,16 @@ private bool TryMarkType(Type desugared)
var templateType = desugared as TemplateSpecializationType;
if (templateType != null)
{
MarkAsUsed(templateType.Template);
MarkAsUsed(templateType.Template.TemplatedDecl);
var template = templateType.Template;
if (template.TemplatedDecl is TypeAlias typeAlias &&
typeAlias.Type.Desugar() is TemplateSpecializationType specializationType)
{
MarkAsUsed(template);
MarkAsUsed(template.TemplatedDecl);
template = specializationType.Template;
}
MarkAsUsed(template);
MarkAsUsed(template.TemplatedDecl);
return true;
}

Expand Down Expand Up @@ -90,6 +98,6 @@ private void RemoveUnusedStdTypes(DeclarationContext context)
}
}

private HashSet<Declaration> usedStdTypes = new HashSet<Declaration>();
private readonly HashSet<Declaration> usedStdTypes = new HashSet<Declaration>();
}
}
2 changes: 1 addition & 1 deletion src/Generator/Passes/TrimSpecializationsPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private static void TryMoveExternalSpecializations(Class template)
let module = arg.Type.Type.GetModule()
where module != null
select module).ToList().TopologicalSort(m => m.Dependencies);
if (modules.Any())
if (modules.Count > 0)
{
var module = modules.Last();
module.ExternalClassTemplateSpecializations.Add(specialization);
Expand Down
1 change: 1 addition & 0 deletions tests/NamespacesDerived/NamespacesDerived.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void TestCodeGeneration()
using (new DerivedFromSecondaryBaseInDependency()) { }
using (var der2 = new Derived2())
using (der2.LocalTypedefSpecialization) { }
Assert.That(typeof(Derived2).Assembly.GetTypes().Any(t => t.FullName.Contains("Std.Vector")), Is.True);
}

[Test]
Expand Down
5 changes: 4 additions & 1 deletion tests/NamespacesDerived/NamespacesDerived.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,13 @@ class DLL_API Ignored
std::basic_string<char, std::char_traits<char>, CustomAllocator<char>> customAllocatedString;
};

template<class T, class Alloc = CustomAllocator<T>>
using vector = ::std::vector<T, Alloc>;

class DLL_API StdFields
{
private:
std::vector<unsigned int, CustomAllocator<unsigned int>> customAllocatedVector;
vector<unsigned int, CustomAllocator<unsigned int>> customAllocatedVector;
};

DLL_API bool operator<<(const Base& b, const char* str);
Expand Down

0 comments on commit a2aeaed

Please sign in to comment.