Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

DCE assembly white list #1024

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Compiler/Analyzers/DeadCodeAnalyzer/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public class Configuration {
private readonly bool _DeadCodeElimination;
private readonly bool _NonAggressiveVirtualMethodElimination;
private readonly IList<string> _WhiteList;

private readonly IList<string> _AssembliesWhiteList;

public Configuration(IDictionary<string, object> configuration) {
_DeadCodeElimination = configuration.ContainsKey("DeadCodeElimination") &&
configuration["DeadCodeElimination"] is bool &&
Expand All @@ -23,6 +24,11 @@ public Configuration(IDictionary<string, object> configuration) {
configuration["WhiteList"] is IList) {
_WhiteList = ((IList) configuration["WhiteList"]).Cast<string>().ToList();
}

if (configuration.ContainsKey("AssembliesWhiteList") && configuration["AssembliesWhiteList"] is IList)
{
_AssembliesWhiteList = ((IList)configuration["AssembliesWhiteList"]).Cast<string>().ToList();
}
}

public bool DeadCodeElimination
Expand All @@ -39,5 +45,10 @@ public IList<string> WhiteList
{
get { return _WhiteList; }
}

public IList<string> AssembliesWhiteList
{
get { return _AssembliesWhiteList; }
}
}
}
19 changes: 19 additions & 0 deletions Compiler/Analyzers/DeadCodeAnalyzer/DeadCodeInfoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public bool IsIncluded(TypeDefinition typeToTest)
private readonly HashSet<EventDefinition> Events = new HashSet<EventDefinition>();

private readonly TypeMapStep TypeMapStep = new TypeMapStep();
private readonly List<Regex> AssembliesWhiteListCache;
private readonly List<Regex> WhiteListCache;
private readonly Configuration Configuration;

Expand All @@ -130,6 +131,16 @@ public DeadCodeInfoProvider(Configuration configuration) {
WhiteListCache.Add(compiledRegex);
}
}

if (configuration.AssembliesWhiteList != null && configuration.AssembliesWhiteList.Count > 0)
{
AssembliesWhiteListCache = new List<Regex>(configuration.AssembliesWhiteList.Count);
foreach (var pattern in configuration.AssembliesWhiteList)
{
var compiledRegex = new Regex(pattern, RegexOptions.ECMAScript | RegexOptions.Compiled);
AssembliesWhiteListCache.Add(compiledRegex);
}
}
}

internal TypeInfoProvider TypeInfoProvider { get; set; }
Expand Down Expand Up @@ -892,6 +903,14 @@ private void AddField(FieldReference field) {

private bool IsMemberWhiteListed(MemberReference member)
{
if (AssembliesWhiteListCache != null)
{
if (AssembliesWhiteListCache.Any(regex => regex.IsMatch(member.Module.Assembly.FullName)))
{
return true;
}
}

if (WhiteListCache != null) {
if (WhiteListCache.Any(regex => regex.IsMatch(member.FullName))) {
return true;
Expand Down