Browse Source

#2594: Improve AssemblyListSnapshot: normalize all .NET Framework versions "v4.x" to "v4"

pull/2643/head
Siegfried Pammer 3 years ago
parent
commit
1efcfe4ae4
  1. 2
      ICSharpCode.Decompiler/Metadata/UniversalAssemblyResolver.cs
  2. 2
      ILSpy/Analyzers/AnalyzerScope.cs
  3. 26
      ILSpy/Analyzers/Builtin/MethodUsedByAnalyzer.cs
  4. 8
      ILSpy/AssemblyListSnapshot.cs
  5. 5
      ILSpy/LoadedAssemblyExtensions.cs

2
ICSharpCode.Decompiler/Metadata/UniversalAssemblyResolver.cs

@ -676,7 +676,7 @@ namespace ICSharpCode.Decompiler.Metadata @@ -676,7 +676,7 @@ namespace ICSharpCode.Decompiler.Metadata
{
var gac = Path.Combine(gac_paths[i], gacs[j]);
var file = GetAssemblyFile(reference, prefixes[i], gac);
if (Directory.Exists(gac) && File.Exists(file))
if (File.Exists(file))
return file;
}
}

2
ILSpy/Analyzers/AnalyzerScope.cs

@ -97,7 +97,7 @@ namespace ICSharpCode.ILSpy.Analyzers @@ -97,7 +97,7 @@ namespace ICSharpCode.ILSpy.Analyzers
{
foreach (var module in GetModulesInScope(ct))
{
var typeSystem = new DecompilerTypeSystem(module, module.GetAssemblyResolver());
var typeSystem = new DecompilerTypeSystem(module, module.GetAssemblyResolver(assemblyListSnapshot, loadOnDemand: false));
foreach (var type in typeSystem.MainModule.TypeDefinitions)
{
yield return type;

26
ILSpy/Analyzers/Builtin/MethodUsedByAnalyzer.cs

@ -45,6 +45,7 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin @@ -45,6 +45,7 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin
Debug.Assert(analyzedSymbol is IMethod);
var analyzedMethod = (IMethod)analyzedSymbol;
var analyzedBaseMethod = (IMethod)InheritanceHelper.GetBaseMember(analyzedMethod);
var mapping = context.Language
.GetCodeMappingInfo(analyzedMethod.ParentModule.PEFile,
analyzedMethod.DeclaringTypeDefinition.MetadataToken);
@ -61,7 +62,7 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin @@ -61,7 +62,7 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin
var methods = type.GetMembers(m => m is IMethod, Options).OfType<IMethod>();
foreach (var method in methods)
{
if (IsUsedInMethod((IMethod)analyzedSymbol, method, context))
if (IsUsedInMethod(analyzedMethod, analyzedBaseMethod, method, context))
{
mapping ??= context.Language.GetCodeMappingInfo(parentModule.PEFile, type.MetadataToken);
var parent = mapping.GetParentMethod((MethodDefinitionHandle)method.MetadataToken);
@ -71,12 +72,12 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin @@ -71,12 +72,12 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin
foreach (var property in type.Properties)
{
if (property.CanGet && IsUsedInMethod((IMethod)analyzedSymbol, property.Getter, context))
if (property.CanGet && IsUsedInMethod(analyzedMethod, analyzedBaseMethod, property.Getter, context))
{
yield return property;
continue;
}
if (property.CanSet && IsUsedInMethod((IMethod)analyzedSymbol, property.Setter, context))
if (property.CanSet && IsUsedInMethod(analyzedMethod, analyzedBaseMethod, property.Setter, context))
{
yield return property;
continue;
@ -85,17 +86,17 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin @@ -85,17 +86,17 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin
foreach (var @event in type.Events)
{
if (@event.CanAdd && IsUsedInMethod((IMethod)analyzedSymbol, @event.AddAccessor, context))
if (@event.CanAdd && IsUsedInMethod(analyzedMethod, analyzedBaseMethod, @event.AddAccessor, context))
{
yield return @event;
continue;
}
if (@event.CanRemove && IsUsedInMethod((IMethod)analyzedSymbol, @event.RemoveAccessor, context))
if (@event.CanRemove && IsUsedInMethod(analyzedMethod, analyzedBaseMethod, @event.RemoveAccessor, context))
{
yield return @event;
continue;
}
if (@event.CanInvoke && IsUsedInMethod((IMethod)analyzedSymbol, @event.InvokeAccessor, context))
if (@event.CanInvoke && IsUsedInMethod(analyzedMethod, analyzedBaseMethod, @event.InvokeAccessor, context))
{
yield return @event;
continue;
@ -104,12 +105,12 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin @@ -104,12 +105,12 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin
}
}
bool IsUsedInMethod(IMethod analyzedEntity, IMethod method, AnalyzerContext context)
bool IsUsedInMethod(IMethod analyzedEntity, IMethod analyzedBaseMethod, IMethod method, AnalyzerContext context)
{
return ScanMethodBody(analyzedEntity, method, context.GetMethodBody(method));
return ScanMethodBody(analyzedEntity, method, analyzedBaseMethod, context.GetMethodBody(method));
}
static bool ScanMethodBody(IMethod analyzedMethod, IMethod method, MethodBodyBlock methodBody)
static bool ScanMethodBody(IMethod analyzedMethod, IMethod method, IMethod analyzedBaseMethod, MethodBodyBlock methodBody)
{
if (methodBody == null)
return false;
@ -117,7 +118,6 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin @@ -117,7 +118,6 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin
var mainModule = (MetadataModule)method.ParentModule;
var blob = methodBody.GetILReader();
var baseMethod = (IMethod)InheritanceHelper.GetBaseMember(analyzedMethod);
var genericContext = new Decompiler.TypeSystem.GenericContext(); // type parameters don't matter for this analyzer
while (blob.RemainingBytes > 0)
@ -139,7 +139,7 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin @@ -139,7 +139,7 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin
var member = MetadataTokenHelpers.EntityHandleOrNil(blob.ReadInt32());
if (!AnalyzerHelpers.IsPossibleReferenceTo(member, mainModule.PEFile, analyzedMethod))
{
if (baseMethod == null || !AnalyzerHelpers.IsPossibleReferenceTo(member, mainModule.PEFile, baseMethod))
if (analyzedBaseMethod == null || !AnalyzerHelpers.IsPossibleReferenceTo(member, mainModule.PEFile, analyzedBaseMethod))
{
continue;
}
@ -157,9 +157,9 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin @@ -157,9 +157,9 @@ namespace ICSharpCode.ILSpy.Analyzers.Builtin
if (m == null)
continue;
if (opCode == ILOpCode.Callvirt && baseMethod != null)
if (opCode == ILOpCode.Callvirt && analyzedBaseMethod != null)
{
if (IsSameMember(baseMethod, m))
if (IsSameMember(analyzedBaseMethod, m))
{
return true;
}

8
ILSpy/AssemblyListSnapshot.cs

@ -44,6 +44,10 @@ namespace ICSharpCode.ILSpy @@ -44,6 +44,10 @@ namespace ICSharpCode.ILSpy
public async Task<PEFile?> TryGetModuleAsync(IAssemblyReference reference, string tfm)
{
bool isWinRT = reference.IsWindowsRuntime;
if (tfm.StartsWith(".NETFramework,Version=v4.", StringComparison.Ordinal))
{
tfm = ".NETFramework,Version=v4";
}
string key = tfm + ";" + (isWinRT ? reference.Name : reference.FullName);
var lookup = LazyInit.VolatileRead(ref isWinRT ? ref asmLookupByShortName : ref asmLookupByFullName);
if (lookup == null)
@ -70,6 +74,10 @@ namespace ICSharpCode.ILSpy @@ -70,6 +74,10 @@ namespace ICSharpCode.ILSpy
if (reader == null || !reader.IsAssembly)
continue;
string tfm = await loaded.GetTargetFrameworkIdAsync().ConfigureAwait(false);
if (tfm.StartsWith(".NETFramework,Version=v4.", StringComparison.Ordinal))
{
tfm = ".NETFramework,Version=v4";
}
string key = tfm + ";"
+ (shortNames ? module.Name : module.FullName);
if (!result.ContainsKey(key))

5
ILSpy/LoadedAssemblyExtensions.cs

@ -31,6 +31,11 @@ namespace ICSharpCode.ILSpy @@ -31,6 +31,11 @@ namespace ICSharpCode.ILSpy
return GetLoadedAssembly(file).GetAssemblyResolver(loadOnDemand);
}
internal static IAssemblyResolver GetAssemblyResolver(this PEFile file, AssemblyListSnapshot snapshot, bool loadOnDemand = true)
{
return GetLoadedAssembly(file).GetAssemblyResolver(snapshot, loadOnDemand);
}
public static IDebugInfoProvider GetDebugInfoOrNull(this PEFile file)
{
return GetLoadedAssembly(file).GetDebugInfoOrNull();

Loading…
Cancel
Save