Skip to content

Commit b5c17d9

Browse files
committed
Verified that unused STD types are removed.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent ae6a373 commit b5c17d9

File tree

4 files changed

+44
-23
lines changed

4 files changed

+44
-23
lines changed

src/AST/Namespace.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,11 @@ public T FindType<T>(string name) where T : Declaration
387387

388388
public Enumeration FindEnumWithItem(string name)
389389
{
390-
var result = Enums.Find(e => e.ItemsByName.ContainsKey(name));
391-
if (result == null)
392-
result = Namespaces.Select(ns => ns.FindEnumWithItem(name)).FirstOrDefault();
393-
if (result == null)
394-
result = Classes.Select(c => c.FindEnumWithItem(name)).FirstOrDefault();
395-
return result;
390+
return Enums.Find(e => e.ItemsByName.ContainsKey(name)) ??
391+
(from declContext in Namespaces.Union<DeclarationContext>(Classes)
392+
let @enum = declContext.FindEnumWithItem(name)
393+
where @enum != null
394+
select @enum).FirstOrDefault();
396395
}
397396

398397
public virtual IEnumerable<Function> FindOperator(CXXOperatorKind kind)

src/Generator.Tests/Passes/TestPasses.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,31 @@ public void TestAbstractOperator()
231231
var @class = AstContext.FindDecl<Class>("ClassWithAbstractOperator").First();
232232
Assert.AreEqual(@class.Operators.First().GenerationKind, GenerationKind.None);
233233
}
234+
235+
[Test]
236+
public void TestRemovalOfUnusedStdTypes()
237+
{
238+
passBuilder.AddPass(new IgnoreSystemDeclarationsPass());
239+
passBuilder.RunPasses(pass => pass.VisitASTContext(AstContext));
240+
if (Platform.IsWindows)
241+
{
242+
Assert.That(AstContext.GetEnumWithMatchingItem("_ALLOC_MASK").Ignore, Is.True);
243+
Assert.That(AstContext.FindClass("_Ctypevec").First().Ignore, Is.True);
244+
return;
245+
}
246+
if (Platform.IsLinux)
247+
{
248+
Assert.That(AstContext.GetEnumWithMatchingItem("PTHREAD_RWLOCK_PREFER_READER_NP").Ignore, Is.True);
249+
Assert.That(AstContext.FindClass("pthread_mutex_t").First().Ignore, Is.True);
250+
return;
251+
252+
}
253+
if (Platform.IsMacOS)
254+
{
255+
Assert.That(AstContext.GetEnumWithMatchingItem("__n_words").Ignore, Is.True);
256+
Assert.That(AstContext.FindClass("__darwin_fp_control").First().Ignore, Is.True);
257+
return;
258+
}
259+
}
234260
}
235261
}

src/Generator/Library.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,22 @@ public static void SetNameOfEnumWithName(this ASTContext context, string enumNam
7777

7878
public static Enumeration GetEnumWithMatchingItem(this ASTContext context, string pattern)
7979
{
80-
foreach (var module in context.TranslationUnits)
81-
{
82-
Enumeration @enum = module.FindEnumWithItem(pattern);
83-
if (@enum == null) continue;
84-
return @enum;
85-
}
86-
87-
return null;
80+
return (from unit in context.TranslationUnits
81+
let @enum = unit.FindEnumWithItem(pattern)
82+
where @enum != null
83+
select @enum).FirstOrDefault();
8884
}
8985

9086
public static Enumeration.Item GenerateEnumItemFromMacro(this Enumeration @enum,
9187
MacroDefinition macro)
9288
{
93-
var item = new Enumeration.Item
94-
{
95-
Name = macro.Name,
96-
Expression = macro.Expression,
97-
Value = ParseMacroExpression(macro.Expression),
98-
Namespace = @enum
99-
};
100-
101-
return item;
89+
return new Enumeration.Item
90+
{
91+
Name = macro.Name,
92+
Expression = macro.Expression,
93+
Value = ParseMacroExpression(macro.Expression),
94+
Namespace = @enum
95+
};
10296
}
10397

10498
static bool ParseToNumber(string num, out long val)

tests/Native/Passes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <string>
2+
13
enum FlagEnum
24
{
35
A = 1 << 0,

0 commit comments

Comments
 (0)